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

/*
 * jfrmDetalleMovimiento.java
 *
 * Created on 11-dic-2009, 18:14:17
 */

package dia4g3java;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;

/**
 *
 * @author pelujl
 */
public class jfrmDetalleMovimiento extends javax.swing.JFrame {
    Sucursal AuxSuc;
    javax.swing.JFrame auxjf;

    ArrayList<Movimiento> m;

    /** Creates new form jfrmDetalleMovimiento */
    public jfrmDetalleMovimiento(Sucursal suc, javax.swing.JFrame jf) {
        AuxSuc = suc;
        auxjf=jf;
        initComponents();
    }

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

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jrbNombre = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jlistamovimientos = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstCuentas = new javax.swing.JList();
        jbtnSeleccionar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jbtnBuscar = new javax.swing.JButton();
        jrbDniCif = new javax.swing.JRadioButton();
        jtxtCadenaBusqueda = new javax.swing.JTextField();
        jbtnSalir = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jlstDetalle = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jbtnDetalles = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ultimos movimientos de cuenta.");

        jLabel2.setText("Realizar busqueda por:");

        jrbNombre.setText("por Nombre");

        jScrollPane2.setViewportView(jlistamovimientos);

        jScrollPane1.setViewportView(jlstCuentas);

        jbtnSeleccionar.setText("Seleccionar Cuenta");
        jbtnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSeleccionarActionPerformed(evt);
            }
        });

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Ultimos movimientos de la cuenta");

        jbtnBuscar.setText("Buscar");
        jbtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBuscarActionPerformed(evt);
            }
        });

        jrbDniCif.setText("por NIF/CIF");

        jbtnSalir.setText("Salir");
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(jlstDetalle);

        jLabel4.setText("Detalles de Movimiento:");

        jbtnDetalles.setText("+ Detalles Movimiento");
        jbtnDetalles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDetallesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 727, Short.MAX_VALUE)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 727, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(566, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jbtnDetalles, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jbtnSeleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jbtnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                                    .addComponent(jtxtCadenaBusqueda, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jrbNombre)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jrbDniCif))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .addComponent(jbtnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jrbNombre)
                    .addComponent(jrbDniCif)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtxtCadenaBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addComponent(jbtnSeleccionar)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jbtnSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jbtnDetalles)))
                .addContainerGap())
        );

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

    private void jbtnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSeleccionarActionPerformed
        // TODO add your handling code here:
        String cta = new String();
        String cadena=(String) jlstCuentas.getSelectedValue();
        cta = cadena.substring(0,20);

        Producto p = AuxSuc.devuelveProducto(cta);
        m = p.getMovimiento();
        Iterator iterador = m.listIterator();
        DefaultListModel dlm = new DefaultListModel();
        jlistamovimientos.setModel(dlm);
        jlistamovimientos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

         int a = 1;
        while(iterador.hasNext()){

            Movimiento mov = (Movimiento) iterador.next();
            dlm.addElement(a+"  .-"+mov.getFechaOperacion()+"       ->        "+mov.getImporte()+"€");
            a++;
        }
    }//GEN-LAST:event_jbtnSeleccionarActionPerformed

    private void jbtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBuscarActionPerformed
        // TODO add your handling code here:
        int tipoBusqueda=0;
        if(jrbDniCif.isSelected()){
            tipoBusqueda=1;
        }else{
            if(jrbNombre.isSelected()){
                tipoBusqueda=2;
            }
        }
        ArrayList<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.getNumeroProducto()+"  "+p.getSaldo());
        }
    }//GEN-LAST:event_jbtnBuscarActionPerformed

    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        // TODO add your handling code here:
        auxjf.setEnabled(true);
        this.dispose();
}//GEN-LAST:event_jbtnSalirActionPerformed

    private void jbtnDetallesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDetallesActionPerformed
        // TODO add your handling code here:
        String cadena = (String) jlistamovimientos.getSelectedValue();
        String valor = (String) cadena.substring(0,3);
      
        int indice = Integer.parseInt(valor.trim());
        Iterator iterador = m.listIterator();
        DefaultListModel dlm2 = new DefaultListModel();
        jlstDetalle.setModel(dlm2);
        int contador=0;
        Movimiento mov2 = new Movimiento();
        while(contador != indice){
            iterador.hasNext();
             mov2 = (Movimiento) iterador.next();
            contador++;
        }
        dlm2.addElement("Concepto: " + mov2.getConcepto());
        dlm2.addElement("Fecha:    " + mov2.getFechaOperacion());
        dlm2.addElement("Importe:  " + mov2.getImporte()+"€");
    }//GEN-LAST:event_jbtnDetallesActionPerformed

    /**
    * @param args the command line arguments
    */
  /*  public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmDetalleMovimiento().setVisible(true);
            }
        });
    }
*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtnBuscar;
    private javax.swing.JButton jbtnDetalles;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JButton jbtnSeleccionar;
    private javax.swing.JList jlistamovimientos;
    private javax.swing.JList jlstCuentas;
    private javax.swing.JList jlstDetalle;
    private javax.swing.JRadioButton jrbDniCif;
    private javax.swing.JRadioButton jrbNombre;
    private javax.swing.JTextField jtxtCadenaBusqueda;
    // End of variables declaration//GEN-END:variables

}
