package Vistas;

import Modelo.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jfrmSeleccionarCuenta.java
 *
 * Created on 16-dic-2009, 18:15:48
 */

/**
 *
 * @author acc_local
 */
public class jfrmSeleccionarCuentaCliente extends javax.swing.JFrame implements Observer{

    //String StNumCuenta;
    Sucursal AuxSuc;
    JFrame auxjf;
    JTextField jtxtNumeroCC;
    boolean bGuardar;
    /** Creates new form jfrmSeleccionarCuenta */
    public jfrmSeleccionarCuentaCliente(Sucursal suc, JFrame jf,JTextField jtxt,boolean b) {
        AuxSuc=suc;
        auxjf=jf;
        initComponents();
        jtxtNumeroCC = jtxt;
        bGuardar = b;
        
        jtxtSaldoActualCaja.setText(String.valueOf(AuxSuc.saldoCaja(AuxSuc.dameDNIEmpleado())));
        Caja c = AuxSuc.busquedaCajaSucursal(AuxSuc.dameIdCaja());
        c.addObserver(this);
    }

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

        jLabel7 = new javax.swing.JLabel();
        label1 = new java.awt.Label();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jrbDniCif = new javax.swing.JRadioButton();
        jrbNombre = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstCuentas = new javax.swing.JList();
        jtxtCadenaBusqueda = new javax.swing.JTextField();
        jbtnSeleccionar = new javax.swing.JButton();
        jbtnBuscar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jlblSeleccion = new javax.swing.JLabel();
        jlblDoc1 = new javax.swing.JLabel();
        jtxtSaldoActualCaja = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vistas/EstafadorLogo2.JPG"))); // NOI18N

        label1.setFont(new java.awt.Font("Comic Sans MS", 1, 48));
        label1.setText("SwindlerBank");

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        jLabel1.setText("Cuentas Cliente");

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jLabel2.setText("Seleccione el método de búsqueda:");

        jrbDniCif.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jrbDniCif.setText("por NIF/CIF");

        jrbNombre.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jrbNombre.setText("por Nombre");

        jlstCuentas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jlstCuentas);

        jtxtCadenaBusqueda.setText("76895436F");
        jtxtCadenaBusqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtCadenaBusquedaActionPerformed(evt);
            }
        });

        jbtnSeleccionar.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jbtnSeleccionar.setText("Seleccionar");
        jbtnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSeleccionarActionPerformed(evt);
            }
        });

        jbtnBuscar.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jbtnBuscar.setText("Buscar");
        jbtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBuscarActionPerformed(evt);
            }
        });

        jbtnCancelar.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jbtnCancelar.setText("Salir");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jlblDoc1.setFont(new java.awt.Font("Comic Sans MS", 0, 10));
        jlblDoc1.setText("Saldo Caja:");

        jtxtSaldoActualCaja.setEditable(false);
        jtxtSaldoActualCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtSaldoActualCajaActionPerformed(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()
                .addComponent(jLabel7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jLabel1)
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblDoc1))
                        .addGap(60, 60, 60))))
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlblSeleccion, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addComponent(jtxtCadenaBusqueda, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jrbNombre)
                                        .addComponent(jrbDniCif))
                                    .addGap(31, 31, 31)
                                    .addComponent(jbtnBuscar)
                                    .addGap(44, 44, 44))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(64, 64, 64)
                                    .addComponent(jbtnSeleccionar)
                                    .addContainerGap(78, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addComponent(jbtnCancelar)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                        .addComponent(jLabel1))
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jrbNombre))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jrbDniCif)
                            .addComponent(jtxtCadenaBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jbtnBuscar)))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jbtnSeleccionar)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnCancelar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(jlblSeleccion, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(jlblDoc1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(282, Short.MAX_VALUE))
        );

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

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

        if(jlstCuentas.getSelectedIndex() == -1)
        {jlblSeleccion.setText("Debes seleccionar cuenta");}
        else{

            String cadena=(String) jlstCuentas.getSelectedValue();
        jlblSeleccion.setText(cadena.substring(0, 20));
        
        if (bGuardar)
        {AuxSuc.setStCCOpera(cadena.substring(0, 20));}

        }
}//GEN-LAST:event_jbtnSeleccionarActionPerformed

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

        if(jtxtCadenaBusqueda.getText().equals(""))
        {jlblSeleccion.setText("Debes introducir Nombre/DNI");}
        else
        {
            int tipoBusqueda=0;
            if(jrbDniCif.isSelected()){
                tipoBusqueda=1;
            }else{
                if(jrbNombre.isSelected()){
                    tipoBusqueda=2;
                }
            }
            List<Producto> a=AuxSuc.busquedaProductosCliente(jtxtCadenaBusqueda.getText(), tipoBusqueda);
            DefaultListModel dlm = new DefaultListModel();
            jlstCuentas.setModel(dlm);
            jlstCuentas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            Iterator iterador = a.listIterator();

            while( iterador.hasNext() ) {
                Producto p = (Producto) iterador.next();
                dlm.addElement(p.getStNumPro()+"  "+p.getDSaldo());
            }
        }
    }//GEN-LAST:event_jbtnBuscarActionPerformed

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

        if(jtxtNumeroCC != null)
        {jtxtNumeroCC.setText(jlblSeleccion.getText());}
        
        auxjf.setEnabled(true);
        this.dispose();
}//GEN-LAST:event_jbtnCancelarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        auxjf.setEnabled(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

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

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

    /**
    * @param args the command line arguments
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmSeleccionarCuentaCliente().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnBuscar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnSeleccionar;
    private javax.swing.JLabel jlblDoc1;
    private javax.swing.JLabel jlblSeleccion;
    private javax.swing.JList jlstCuentas;
    private javax.swing.JRadioButton jrbDniCif;
    private javax.swing.JRadioButton jrbNombre;
    private javax.swing.JTextField jtxtCadenaBusqueda;
    private javax.swing.JTextField jtxtSaldoActualCaja;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        jtxtSaldoActualCaja.setText(String.valueOf(AuxSuc.saldoCaja(AuxSuc.dameDNIEmpleado())));
    }

}
