/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JInternalFrame.java to edit this template
 */
package Formularios;
import Clases.Reserva;
import Clases.Datos;
import Clases.Utilidades;
import java.io.PrintWriter;
import java.util.Date;
import Clases.Opcion;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 1032480985
 */
public class frmReserva extends javax.swing.JInternalFrame {

    private Datos misDatos;
    private int cliAct = 0;
    private boolean nuevo = false;
    private DefaultTableModel miTabla;
   
    
    
    public frmReserva() {
        initComponents();
    }

    public void setDatos(Datos misDatos) {
        this.misDatos = misDatos;
    }
    
      private void mostrarResgistro() {
      
        txtIdReserva.setText(misDatos.getReserva()[cliAct].getIdReserva());
        cboReserva.setSelectedIndex(misDatos.getReserva()[cliAct].getTipoReserva());
        txtNombres.setText(" "+ misDatos.getReserva()[cliAct].getNombres());
        txtApellidos.setText(misDatos.getReserva()[cliAct].getApellidos());
        dchFechaReserva.setDate(misDatos.getReserva()[cliAct].getFechaReserva());
        cboProducto.setSelectedItem(misDatos.getProductos()[cliAct].getDescripcion());
    }
     private void llenarTabla() {
         
         
        String titulos[] = { "ID Reserva", "Tipo ID", "Nombres", "Apellidos",  "F. Reserva" , "Hora Reserva", "Descripcion"};
        String registro[] = new String[7];
        miTabla = new DefaultTableModel(null, titulos);
        
         
        
         for (int i = 0; i < misDatos.numeroReserva(); i++) {
            registro[0] = misDatos.getReserva()[i].getIdReserva();
            registro[1] = tipoReserva(misDatos.getReserva()[i].getTipoReserva());
            registro[2] = misDatos.getReserva()[i].getNombres();
            registro[3] = misDatos.getReserva()[i].getApellidos();
            registro[4] = "" + misDatos.getReserva()[i].getFechaReserva();
            registro[5] = "" + misDatos.getReserva()[i].getHoraReserva();
            registro[6] = Descripcion(misDatos.getReserva()[i].getDescripcion());
            miTabla.addRow(registro);
        }
            tblReserva.setModel(miTabla); 
            
            
        // Alinear campos numericos a la derecha
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.RIGHT);
        tblReserva.getColumnModel().getColumn(2).setCellRenderer(tcr);
        tblReserva.getColumnModel().getColumn(3).setCellRenderer(tcr);
        tblReserva.getColumnModel().getColumn(4).setCellRenderer(tcr);
        tblReserva.getColumnModel().getColumn(5).setCellRenderer(tcr);
        tblReserva.getColumnModel().getColumn(6).setCellRenderer(tcr);
            
            
            
    }

    private String tipoReserva(int id) {
       switch(id) {
           case 0: 
           case 1 : return "Disponible";
           case 2 : return "Programada";
           case 3: return "Pendiente";
           default: return "No definido";
       }       
    }
  
     private String Descripcion(int id) {
       switch(id) {
           case 0: return "  ";
           case 1 : return "Consola xbox One";
            case 2 : return "Consola Xbox 360";
            case 3 : return "Controles";
             default: return "No definido";
       }
     }
    /**
     * Creates new form NewJInternalFrame
     */

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtIdReserva = new javax.swing.JTextField();
        cboReserva = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        dchFechaReserva = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtApellidos = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReserva = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnPrimero = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnSiguiente = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        dchHoraReserva = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        btnBuscarReserva = new javax.swing.JButton();
        cboProducto = new javax.swing.JComboBox<>();

        setClosable(true);
        setTitle("Reserva");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setText("ID Reserva*");

        jLabel2.setText("Tipo de Reserva*");

        txtIdReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdReservaActionPerformed(evt);
            }
        });

        cboReserva.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Disponible", "Programada", "Pendiente" }));
        cboReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboReservaActionPerformed(evt);
            }
        });

        jLabel3.setText("Fecha Reserva*");

        dchFechaReserva.setDateFormatString("yyyy/MM/dd HH:mm:ss");
        dchFechaReserva.setEnabled(false);

        jLabel4.setText("Nombres*");

        txtNombres.setEnabled(false);
        txtNombres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombresActionPerformed(evt);
            }
        });

        jLabel5.setText("Apellidos *");

        txtApellidos.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtApellidos.setEnabled(false);

        tblReserva.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "ID Reserva", "Tipo Reserva", "Nombre", "Apellido", "Fecha Reserva", "Hora Reserva"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblReserva);

        jLabel7.setForeground(java.awt.Color.blue);
        jLabel7.setText("* Campos Obligatorios");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/guardar24x24.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar registro");
        btnGuardar.setEnabled(false);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/cancelar24x24.png"))); // NOI18N
        btnCancelar.setToolTipText("Guardar registro");
        btnCancelar.setEnabled(false);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/editar24x24.png"))); // NOI18N
        btnEditar.setToolTipText("Editar registro");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/buscar24x24.png"))); // NOI18N
        btnBuscar.setToolTipText("Buscar registro");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/borrar24x24.png"))); // NOI18N
        btnBorrar.setToolTipText("Borrar registro");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/nuevo24x24.png"))); // NOI18N
        btnNuevo.setToolTipText("Nuevo registro");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/primero.png"))); // NOI18N
        btnPrimero.setToolTipText("Va al primer registro");
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/anterior.png"))); // NOI18N
        btnAnterior.setToolTipText("Va al anterior registro");
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/siguiente.png"))); // NOI18N
        btnSiguiente.setToolTipText("Va al siguiente registro");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ultimo.png"))); // NOI18N
        btnUltimo.setToolTipText("Va al ultimo registro");
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });

        dchHoraReserva.setDateFormatString("HH mm ss");
        dchHoraReserva.setEnabled(false);

        jLabel6.setText("Hora Reserva*");

        jLabel8.setText("Descripcion*");

        btnBuscarReserva.setText("...");
        btnBuscarReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarReservaActionPerformed(evt);
            }
        });

        cboProducto.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "CONSOLA XBOX ONE", "CONSOLA XBOX 360", "CONTROLES" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtIdReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboReserva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(127, 127, 127))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addGap(18, 18, 18)
                                                .addComponent(dchHoraReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(41, 41, 41)
                                                .addComponent(jLabel5)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(37, 37, 37)
                                        .addComponent(btnBuscarReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(259, 259, 259)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(cboProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dchFechaReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnPrimero)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnAnterior)
                                    .addGap(9, 9, 9)
                                    .addComponent(btnSiguiente)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnUltimo)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnNuevo)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnGuardar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnCancelar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnEditar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnBuscar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnBorrar))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addGap(520, 520, 520))))
                        .addGap(0, 24, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtIdReserva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboReserva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(dchFechaReserva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(dchHoraReserva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addComponent(btnBuscarReserva))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cboProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jLabel7)
                .addGap(36, 36, 36)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnPrimero)
                    .addComponent(btnAnterior)
                    .addComponent(btnUltimo)
                    .addComponent(btnSiguiente)
                    .addComponent(btnNuevo)
                    .addComponent(btnBuscar)
                    .addComponent(btnEditar)
                    .addComponent(btnBorrar)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtIdReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdReservaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdReservaActionPerformed

    private void cboReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboReservaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboReservaActionPerformed

    private void txtNombresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombresActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed

        //validaciones
        if (txtIdReserva.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Debe digitar un ID");
            txtIdReserva.requestFocusInWindow();
            return;
        }
        if (cboReserva.getSelectedIndex() == 1) {
            JOptionPane.showMessageDialog(rootPane, "Debe seleccionar un Tipo de Reserva");
            cboReserva.requestFocusInWindow();
            return;
        }
        if (txtNombres.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Debe digitar un Nombre");
            txtNombres.requestFocusInWindow();
            return;
        }
        if (txtApellidos.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Debe digitar un Apellido");
            txtApellidos.requestFocusInWindow();
            return;
        }
      
        
        if (dchFechaReserva.getDate().equals(new Date())) {
            JOptionPane.showMessageDialog(rootPane, "La Fecha de reserva debe ser diferente");
            dchFechaReserva.requestFocusInWindow();
            return;
        }
       else
            if (dchFechaReserva.getDate().before(new Date())) {
            JOptionPane.showMessageDialog(rootPane, "La Fecha de reserva debe ser diferente");
            dchFechaReserva.requestFocusInWindow();
            return;
      }
        
        
        if (cboProducto.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(rootPane, "Debe seleccionar una Descripcion");
            cboProducto.requestFocusInWindow();
            return;
        }
        
        
 
        
        
        
        /*
         if (dchHoraReserva.getDateFormat().before(new Date())) {
            JOptionPane.showMessageDialog(rootPane, "La Hora de reserva debe ser diferente");
            dchHoraReserva.requestFocusInWindow();
            return;
        }*/
        
        //si es nuevo, validamos que la reserva no exista
        int pos = misDatos.posicionReserva(txtIdReserva.getText());
        if (nuevo) {
            if (pos != -1) {
                JOptionPane.showMessageDialog(rootPane, "Reserva ya existe");
                txtIdReserva.requestFocusInWindow();
                return;
            }
        } else {
            if (pos == -1) {
                JOptionPane.showMessageDialog(rootPane, "Reserva no existe");
                txtNombres.requestFocusInWindow();
                return;
            }
        }

        //creamos el objeto Reserva y lo agregamos a datos
        Reserva miReserva = new Reserva(txtIdReserva.getText(), cboReserva.getSelectedIndex(),
            txtNombres.getText(), txtApellidos.getText(), dchFechaReserva.getDate(), cboProducto.getSelectedIndex());
        String  msg;

        if (nuevo) {
            msg = misDatos.agregarReserva(miReserva);
        } else {
            msg = misDatos.modificarReserva(miReserva, pos);
        }

        JOptionPane.showMessageDialog(rootPane, msg);

        btnPrimero.setEnabled(true);
        btnAnterior.setEnabled(true);
        btnSiguiente.setEnabled(true);
        btnUltimo.setEnabled(true);

        btnNuevo.setEnabled(true);
        btnPrimero.setEnabled(true);
        btnBorrar.setEnabled(true);
        btnBuscar.setEnabled(true);
        btnEditar.setEnabled(true);

        btnCancelar.setEnabled(false);
        btnGuardar.setEnabled(false);

        //deshabilitar campos
        txtIdReserva.setEnabled(false);
        cboReserva.setEnabled(false);
        txtNombres.setEnabled(false);
        txtApellidos.setEnabled(false);
        cboProducto.setEnabled(false);
        dchFechaReserva.setEnabled(false);
         dchHoraReserva.setEnabled(false);

        //limpiar campos
        txtIdReserva.setText("");
        cboReserva.setSelectedIndex(0);
        txtNombres.setText("");
        txtApellidos.setText("");
        cboProducto.setSelectedIndex(0);

        //actualizamos los cambios en la tabla
        llenarTabla();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        btnPrimero.setEnabled(true);
        btnAnterior.setEnabled(true);
        btnSiguiente.setEnabled(true);
        btnUltimo.setEnabled(true);

        btnNuevo.setEnabled(true);
        btnPrimero.setEnabled(true);
        btnBorrar.setEnabled(true);
        btnBuscar.setEnabled(true);
        btnEditar.setEnabled(true);

        btnCancelar.setEnabled(false);
        btnGuardar.setEnabled(false);

        //deshabilitar campos
        txtIdReserva.setEnabled(false);
        cboReserva.setEnabled(false);
        txtNombres.setEnabled(false);
        txtApellidos.setEnabled(false);
        cboProducto.setEnabled(false);
        dchFechaReserva.setEnabled(false);
        dchHoraReserva.setEnabled(false);

        //limpiar campos
        txtIdReserva.setText("");
        cboReserva.setSelectedIndex(0);
        txtNombres.setText("");
        txtApellidos.setText("");
        
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        //habilitar botones
        btnPrimero.setEnabled(false);
        btnAnterior.setEnabled(false);
        btnSiguiente.setEnabled(false);
        btnUltimo.setEnabled(false);

        btnNuevo.setEnabled(true);
        btnPrimero.setEnabled(false);
        btnBorrar.setEnabled(false);
        btnBuscar.setEnabled(false);
        btnEditar.setEnabled(false);

        btnCancelar.setEnabled(true);
        btnGuardar.setEnabled(true);

        //habilitar campos
        txtIdReserva.setEnabled(false);
        cboReserva.setEnabled(true);
        txtNombres.setEnabled(true);
        txtApellidos.setEnabled(true);
        cboProducto.setEnabled(true);
        dchFechaReserva.setEnabled(true);
        dchHoraReserva.setEnabled(true);

        //desactivamos el flag de registro nuevo
        nuevo = false;

        txtNombres.requestFocusInWindow();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String reserva= JOptionPane.showInputDialog("Ingrese código de Reserva");
        if (reserva.equals("")) {
            return;
        }
        int pos = misDatos.posicionReserva(reserva);
        if (pos == -1) {
            JOptionPane.showMessageDialog(rootPane, "Reserva no existe");
            return;
        }
        cliAct = pos;
        mostrarResgistro();
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        int rta = JOptionPane.showConfirmDialog(rootPane, "¿Esta seguro de borrar el registro?");
        if (rta !=  0) {
            return;
        }
        String msg;
        msg = misDatos.borrarReserva(cliAct);
        JOptionPane.showMessageDialog(rootPane, msg);
        cliAct = 0;
        mostrarResgistro();

        //actualizamos los cambios en la tabla
        llenarTabla();
    }//GEN-LAST:event_btnBorrarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        //habilitar botones
        btnPrimero.setEnabled(false);
        btnAnterior.setEnabled(false);
        btnSiguiente.setEnabled(false);
        btnUltimo.setEnabled(false);

        btnNuevo.setEnabled(false);
        btnPrimero.setEnabled(false);
        btnBorrar.setEnabled(false);
        btnBuscar.setEnabled(false);
        btnEditar.setEnabled(false);

        btnCancelar.setEnabled(true);
        btnGuardar.setEnabled(true);

        //habilitar campos
        txtIdReserva.setEnabled(true);
        cboReserva.setEnabled(true);
        txtNombres.setEnabled(true);
        txtApellidos.setEnabled(true);
        cboProducto.setEnabled(true);
        dchFechaReserva.setEnabled(true);
        dchHoraReserva.setEnabled(true);

        //limpiar campos
        txtIdReserva.setText("");
        cboReserva.setSelectedIndex(0);
        txtNombres.setText("");
        txtApellidos.setText("");
        cboProducto.setSelectedIndex(0);
        dchFechaReserva.setDate(new Date());
        /*dchHoraReserva.setDateFormat(new DateFormat());*/
       

        //activamos el flag de registro nuevo
        nuevo = true;

        txtIdReserva.requestFocusInWindow();

    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        cliAct = 0;
        mostrarResgistro();
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        cliAct--;
        if (cliAct == -1) {
            cliAct = misDatos.numeroReserva()- 1;;
        }
        mostrarResgistro();
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        cliAct++;
        if (cliAct == misDatos.numeroReserva()) {
            cliAct = 0;
        }
        mostrarResgistro();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        cliAct = misDatos.numeroReserva()- 1;
        mostrarResgistro();
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void btnBuscarReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarReservaActionPerformed
        frmBusquedaReserva miBusquedaReserva = new frmBusquedaReserva(null, closable);
        miBusquedaReserva.setDatos(misDatos);
        miBusquedaReserva.setLocationRelativeTo(null);
        miBusquedaReserva.setVisible(true);
        String rpta = miBusquedaReserva.getRespuesta();

        if (rpta.equals(""))
        return;

        /*for (int i = 0; i < cboProducto.getItemCount(); i++) {
            if (((Opcion)cboProducto.getItemAt(i)).getValor().equals(rpta)) {
                cboProducto.setSelectedIndex(i);
                return;
            }
        }*/
    }//GEN-LAST:event_btnBuscarReservaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscarReserva;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox<String> cboProducto;
    private javax.swing.JComboBox<String> cboReserva;
    private com.toedter.calendar.JDateChooser dchFechaReserva;
    private com.toedter.calendar.JDateChooser dchHoraReserva;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblReserva;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtIdReserva;
    private javax.swing.JTextField txtNombres;
    // End of variables declaration//GEN-END:variables

    
}
