package visual.frame;

import javax.swing.*;
import javax.swing.table.*;

import control.ControlAdmin;

import java.awt.*;
import java.awt.event.*;

/**
 * Ventana del modo administrador la cual permite la modificacion y creacion de
 * usuarios por parte de un administrador con la clave
 */

public class FrameAdministrador extends JFrame {

	private JButton bCrear;
	private JButton bModificar;
	private JScrollPane jScrollPane1;
	private JPanel panel;
	private JTable tabla;

	/**
	 * Constructor que inicia la ventana y todos sus componentes
	 */

	public FrameAdministrador() {
		initComponents();
		actTabla();
	}

	/**
	 * Inicia los componentes de la ventana
	 */

	private void initComponents() {

		panel = new JPanel();
		jScrollPane1 = new JScrollPane();
		tabla = new JTable();
		bModificar = new JButton();
		bCrear = new JButton();

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				new FrameLogin();
				setVisible(false);
				dispose();
			}
		});

		panel.setBackground(new Color(0, 128, 128));

		jScrollPane1.setBackground(new Color(128, 203, 196));

		tabla.setBackground(new Color(178, 223, 219));
		tabla.setFont(new Font("Candara", 0, 13)); // NOI18N
		tabla.setForeground(new Color(21, 21, 21));
		tabla.setModel(
				new DefaultTableModel(new Object[][] { { null, null } }, new String[] { "Usuario", "Contraseņa" }) {
					boolean[] canEdit = new boolean[] { false, false };

					public boolean isCellEditable(int rowIndex, int columnIndex) {
						return canEdit[columnIndex];
					}
				});
		tabla.setGridColor(new Color(255, 255, 255));
		tabla.setOpaque(false);
		tabla.setSelectionForeground(new Color(0, 120, 215));
		jScrollPane1.setViewportView(tabla);

		bModificar.setBackground(new Color(33, 150, 243));
		bModificar.setText("Modificar usuario");
		bModificar.setContentAreaFilled(false);
		bModificar.setOpaque(true);
		bModificar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {

				bModificarActionPerformed(evt);

			}

		});

		bCrear.setBackground(new Color(33, 150, 243));
		bCrear.setText("Crear Usuario");
		bCrear.setContentAreaFilled(false);
		bCrear.setOpaque(true);
		bCrear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {

				bCrearActionPerformed(evt);

			}

		});

		GroupLayout panelLayout = new GroupLayout(panel);
		panel.setLayout(panelLayout);
		panelLayout
				.setHorizontalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup().addContainerGap()
								.addComponent(bCrear, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(bModificar, GroupLayout.PREFERRED_SIZE, 177, GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(panelLayout.createSequentialGroup().addContainerGap()
										.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
										.addContainerGap())));
		panelLayout.setVerticalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(GroupLayout.Alignment.TRAILING,
						panelLayout.createSequentialGroup().addContainerGap(447, Short.MAX_VALUE)
								.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(bModificar).addComponent(bCrear))
								.addContainerGap())
				.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(panelLayout.createSequentialGroup()
								.addContainerGap().addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(43, Short.MAX_VALUE))));

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGap(0, 0, 0)));
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
	}

	/**
	 * Inicia la ventana crear y cierra la actual
	 */
	
	private void bCrearActionPerformed(ActionEvent evt) {
		new FrameCrear();
		setVisible(false);
		dispose();

	}

	/**
	 * Inicia la ventana modificar y cierra la actual
	 */
	
	private void bModificarActionPerformed(ActionEvent evt) {
		new FrameModificar();
		setVisible(false);
		dispose();

	}

	/**
	 * Llena la tabla de usuarios
	 */
	
	public void actTabla() {

		ControlAdmin control = new ControlAdmin();
		DefaultTableModel modelo = (DefaultTableModel) tabla.getModel();

		tabla.setModel(modelo);
		modelo.setRowCount(0);

		for (int x = 0; x < control.getRowCount(); x++) {

			Object[] fila = control.getRow(x);
			modelo.addRow(fila);

		}

	}

}
