/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JDialog.java to edit this template
 */
package Formularios;

import Clases.Datos;
import Clases.Utilidades;
import java.util.Date;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 1032480985
 */
public class frmBusquedaReserva extends javax.swing.JDialog {
    
    /**
     * Creates new form frmBusquedaReserva
     */
     private Datos misDatos;
    private DefaultTableModel miTabla;
    private String respuesta = "";
   
    
    
    public frmBusquedaReserva(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        
        buttonGroup1.add(rbtID);
       buttonGroup1.add(rbtNombres);
        buttonGroup1.add(rbtApellidos);
        buttonGroup1.add(rbtDescripcion);
         buttonGroup1.add(rbtTipoReserva);
    }
    
    public void setDatos(Datos misDatos) {
        this.misDatos = misDatos;
    }
    
    
     private void llenarTabla() {
        
        String titulos[] = { "ID Cliente", "Nombres", "Apellidos" ,"Descripcion" ,"Tipo Reserva" };
        Object registro[] = new Object[5];
    
        miTabla = new DefaultTableModel(null, titulos);
        miTabla.setRowCount(0);
        
         
        String criterioBusqueda = txtCriterio.getText();
        
        for (int i = 0; i < misDatos.numeroReserva(); i++) {
            if (txtCriterio.getText().equals("")) {
                registro[0] = misDatos.getReserva()[i].getIdReserva();
                registro[1] = misDatos.getReserva()[i].getNombres();
                registro[2] = misDatos.getReserva()[i].getApellidos();
                registro[3] = misDatos.getReserva()[i].getDescripcion();
                registro[4] = misDatos.getReserva()[i].getTipoReserva();
                
                
                miTabla.addRow(registro);
            } else {
                if (rbtNombres.isSelected()) {
                    if (misDatos.getReserva()[i].getNombres().startsWith(txtCriterio.getText())) {
                        registro[0] = misDatos.getReserva()[i].getIdReserva();
                        registro[1] = misDatos.getReserva()[i].getNombres();
                        registro[2] = misDatos.getReserva()[i].getApellidos();
                        registro[3] = misDatos.getReserva()[i].getDescripcion();
                           registro[4] = misDatos.getReserva()[i].getTipoReserva();
                       
                        miTabla.addRow(registro);
                    }
                }
                else if (rbtApellidos.isSelected()) {
                    if (misDatos.getReserva()[i].getApellidos().startsWith(txtCriterio.getText())) {
                        registro[0] = misDatos.getReserva()[i].getIdReserva();
                        registro[1] = misDatos.getReserva()[i].getNombres();
                        registro[2] = misDatos.getReserva()[i].getApellidos();
                        registro[3] = misDatos.getReserva()[i].getDescripcion();
                           registro[4] = misDatos.getReserva()[i].getTipoReserva();
                        
                        miTabla.addRow(registro);
                    }
                }
                else if (rbtID.isSelected()) {
                    if (misDatos.getReserva()[i].getIdReserva().startsWith(txtCriterio.getText())) {
                        registro[0] = misDatos.getReserva()[i].getIdReserva();
                        registro[1] = misDatos.getReserva()[i].getNombres();
                        registro[2] = misDatos.getReserva()[i].getApellidos();
                        registro[3] = misDatos.getReserva()[i].getDescripcion();
                        registro[4] = misDatos.getReserva()[i].getTipoReserva();
                        
                        miTabla.addRow(registro);
                    }
                }
                
           /*  
                 // Si el RadioButton de Descripción está seleccionado
         else if (rbtDescripcion.isSelected()) {
            if (misDatos.getReserva()[i].getDescripcion().startsWith(criterioBusqueda) {
            registro[0] = misDatos.getReserva()[i].getIdReserva();
            registro[1] = misDatos.getReserva()[i].getNombres();
            registro[2] = misDatos.getReserva()[i].getApellidos();
            registro[3] = misDatos.getReserva()[i].getDescripcion();
            miTabla.addRow(registro);
        }
       */     
   }
             
            
       
        tblTabla.setModel(miTabla);
        }
     }

    public String getRespuesta() {
        return respuesta;
    }

    /**
     * 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() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTabla = new javax.swing.JTable();
        txtCriterio = new javax.swing.JTextField();
        rbtApellidos = new javax.swing.JRadioButton();
        rbtNombres = new javax.swing.JRadioButton();
        rbtID = new javax.swing.JRadioButton();
        btnCancelar6 = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        rbtDescripcion = new javax.swing.JRadioButton();
        rbtTipoReserva = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tblTabla.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}
            },
            new String [] {
                "ID Reserva", "Nombres", "Apellidos", "Descripcion", "Tipo Reserva"
            }
        ));
        jScrollPane1.setViewportView(tblTabla);

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

        rbtApellidos.setText("Apellidos");
        rbtApellidos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtApellidosActionPerformed(evt);
            }
        });

        rbtNombres.setText("Nombres");
        rbtNombres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtNombresActionPerformed(evt);
            }
        });

        rbtID.setSelected(true);
        rbtID.setText("ID");
        rbtID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtIDActionPerformed(evt);
            }
        });

        btnCancelar6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/cancelar24x24.png"))); // NOI18N
        btnCancelar6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelar6ActionPerformed(evt);
            }
        });

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/aceptar24x24.png"))); // NOI18N
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        rbtDescripcion.setText("Descripcion");
        rbtDescripcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtDescripcionActionPerformed(evt);
            }
        });

        rbtTipoReserva.setText("Tipo Reserva");
        rbtTipoReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtTipoReservaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCriterio)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbtID)
                        .addGap(18, 18, 18)
                        .addComponent(rbtNombres)
                        .addGap(18, 18, 18)
                        .addComponent(rbtApellidos)
                        .addGap(18, 18, 18)
                        .addComponent(rbtDescripcion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbtTipoReserva)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar6, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtID)
                    .addComponent(rbtNombres)
                    .addComponent(rbtApellidos)
                    .addComponent(rbtDescripcion)
                    .addComponent(rbtTipoReserva))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCriterio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void txtCriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCriterioActionPerformed
        llenarTabla();
    }//GEN-LAST:event_txtCriterioActionPerformed

    private void rbtApellidosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtApellidosActionPerformed
        txtCriterio.setText("");
        txtCriterio.requestFocusInWindow();
    }//GEN-LAST:event_rbtApellidosActionPerformed

    private void rbtNombresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtNombresActionPerformed
        txtCriterio.setText("");
        txtCriterio.requestFocusInWindow();
    }//GEN-LAST:event_rbtNombresActionPerformed

    private void rbtIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtIDActionPerformed
        txtCriterio.setText("");
        txtCriterio.requestFocusInWindow();
    }//GEN-LAST:event_rbtIDActionPerformed

    private void btnCancelar6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelar6ActionPerformed
        respuesta = "";
        dispose();
    }//GEN-LAST:event_btnCancelar6ActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        int num = tblTabla.getRowCount();

        if (num  == 0) {
            respuesta = "";
            dispose();
            return;
        }
        for (int i = 0; i < num; i++) {
            if (tblTabla.isRowSelected(i)) {
                respuesta = Utilidades.objectToString(tblTabla.getValueAt(i, 0));
                dispose();
                return;
            }
        }

        respuesta = Utilidades.objectToString(tblTabla.getValueAt(0, 0));
        dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void rbtDescripcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtDescripcionActionPerformed
        // TODO add your handling code here:
        
           txtCriterio.setText("");
        txtCriterio.requestFocusInWindow();
    }//GEN-LAST:event_rbtDescripcionActionPerformed

    private void rbtTipoReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtTipoReservaActionPerformed
        // TODO add your handling code here:
        
           txtCriterio.setText("");
        txtCriterio.requestFocusInWindow();
    }//GEN-LAST:event_rbtTipoReservaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmBusquedaReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmBusquedaReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmBusquedaReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmBusquedaReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmBusquedaReserva dialog = new frmBusquedaReserva(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar6;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtApellidos;
    private javax.swing.JRadioButton rbtDescripcion;
    private javax.swing.JRadioButton rbtID;
    private javax.swing.JRadioButton rbtNombres;
    private javax.swing.JRadioButton rbtTipoReserva;
    private javax.swing.JTable tblTabla;
    private javax.swing.JTextField txtCriterio;
    // End of variables declaration//GEN-END:variables

   
}