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

/*
 * jfrmUltimosMovimientos.java
 *
 * Created on 11-dic-2009, 23:29:23
 */

package dia4g3java;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Ro
 */

public class jfrmUltimosMovimientos extends javax.swing.JFrame {
    Sucursal auxsuc;
    javax.swing.JFrame auxjf;
    /** Creates new form jfrmUltimosMovimientos */
    public jfrmUltimosMovimientos(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() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jbtnUltimaCuenta = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtxtcuenta = new javax.swing.JTextField();
        jbtnSalir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstMovimientos = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtxtFechaInicio = new javax.swing.JTextField();
        jtxtFechaFin = new javax.swing.JTextField();
        jbtnValida = new javax.swing.JButton();
        jbtnDetalle = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel3.setFont(new java.awt.Font("Papyrus", 1, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 102));
        jLabel3.setText("Banco ALROMA");

        jLabel1.setFont(new java.awt.Font("Papyrus", 0, 24));
        jLabel1.setText("Ultimos Movimientos");

        jbtnUltimaCuenta.setText("Buscar Ultima Cuenta");
        jbtnUltimaCuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnUltimaCuentaActionPerformed(evt);
            }
        });

        jLabel2.setText("Numero de Cuenta");

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

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

        jLabel4.setText("Desde");

        jLabel5.setText("Hasta");

        jtxtFechaInicio.setText("12-09-09");

        jtxtFechaFin.setText("11-12-09");

        jbtnValida.setText("Valida");
        jbtnValida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnValidaActionPerformed(evt);
            }
        });

        jbtnDetalle.setText("Muestra en Detalle");
        jbtnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDetalleActionPerformed(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()
                .addGap(142, 142, 142)
                .addComponent(jLabel1))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jtxtFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtxtFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jbtnValida))
                                    .addComponent(jtxtcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(jbtnDetalle))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(jbtnSalir))))
                    .addComponent(jbtnUltimaCuenta)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnUltimaCuenta)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtcuenta, 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(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jtxtFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnValida))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jbtnDetalle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                        .addComponent(jbtnSalir)
                        .addGap(68, 68, 68)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

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

    private void jbtnUltimaCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnUltimaCuentaActionPerformed
        jtxtcuenta.setText(auxsuc.getUltimoNumcta());
    }//GEN-LAST:event_jbtnUltimaCuentaActionPerformed

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

    private void jbtnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDetalleActionPerformed
        
        String cadena=(String) jlstMovimientos.getSelectedValue();

        SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
        java.util.Date fi = null;
        try {
            fi = sdf.parse(jtxtFechaInicio.getText());
        } catch (ParseException ex) {
            Logger.getLogger(jfrmUltimosMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.util.Date ff = null;
        try {
            ff = sdf.parse(jtxtFechaFin.getText());
        } catch (ParseException ex) {
            Logger.getLogger(jfrmUltimosMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
        Producto p = new Producto();
        p=auxsuc.devuelveProducto(jtxtcuenta.getText());

        int x=Integer.parseInt(cadena.substring(0, 1));

        ArrayList<Movimiento> am = p.getMovimiento();        
        Iterator iterador = am.listIterator();
        int n=0;
        while( iterador.hasNext() ) {
            Movimiento m = (Movimiento) iterador.next();
            n++;
            if ((m.getFechaOperacion().before(ff))&&(m.getFechaOperacion().after(fi))){
                if(n==x){
                    this.setEnabled(false);
                    new jfrmDetalle(this,m).setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_jbtnDetalleActionPerformed

    private void jbtnValidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnValidaActionPerformed
        Producto p = new Producto();

        SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
        java.util.Date fi = null;
        try {
            fi = sdf.parse(jtxtFechaInicio.getText());
        } catch (ParseException ex) {
            Logger.getLogger(jfrmUltimosMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.util.Date ff = null;
        try {
            ff = sdf.parse(jtxtFechaFin.getText());
        } catch (ParseException ex) {
            Logger.getLogger(jfrmUltimosMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
        p=auxsuc.devuelveProducto(jtxtcuenta.getText());

        ArrayList<Movimiento> am = p.getMovimiento();
        DefaultListModel dlm = new DefaultListModel();
        jlstMovimientos.setModel(dlm);
        jlstMovimientos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//para que solo coja un valor
        Iterator iterador = am.listIterator();
        int n=0;
        while( iterador.hasNext() ) {
            Movimiento m = (Movimiento) iterador.next();
            n++;
            if ((m.getFechaOperacion().before(ff))&&(m.getFechaOperacion().after(fi))){                
                dlm.addElement(n+"      "+m.getImporte());
            }
        }
    }//GEN-LAST:event_jbtnValidaActionPerformed


    // 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.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnDetalle;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JButton jbtnUltimaCuenta;
    private javax.swing.JButton jbtnValida;
    private javax.swing.JList jlstMovimientos;
    private javax.swing.JTextField jtxtFechaFin;
    private javax.swing.JTextField jtxtFechaInicio;
    private javax.swing.JTextField jtxtcuenta;
    // End of variables declaration//GEN-END:variables

}
