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

public class jfrmMovimientos extends javax.swing.JFrame {

    JFrame auxJf;
    Sucursal sucAux = new Sucursal();

    /** Creates new form jfrmMovimientos */
    public jfrmMovimientos(Sucursal suc,JFrame jf) {
        auxJf = jf;
        initComponents();
        sucAux = suc;
    }

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

        jLabel6 = new javax.swing.JLabel();
        label2 = new java.awt.Label();
        jLabel1 = new javax.swing.JLabel();
        jtxtNumeroCC = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstMov = new javax.swing.JList();
        jbtnBuscar = new javax.swing.JButton();
        jbtnDetalles = new javax.swing.JButton();
        jlblDetalles = new javax.swing.JLabel();

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

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

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

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        jLabel1.setText("Movimientos de una cuenta");

        jtxtNumeroCC.setText("20984002161000015263");

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        jLabel5.setText("Cuenta:");

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

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

        jbtnDetalles.setText("Detalles");
        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)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(84, 84, 84)
                .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnDetalles)
                .addGap(39, 39, 39))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlblDetalles)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addGap(132, 132, 132))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jtxtNumeroCC, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(jbtnBuscar)
                                .addGap(117, 117, 117))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(jLabel1))
                    .addComponent(jLabel6))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jtxtNumeroCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnBuscar))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbtnDetalles))
                .addGap(18, 18, 18)
                .addComponent(jlblDetalles)
                .addContainerGap(35, Short.MAX_VALUE))
        );

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

    private void jbtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBuscarActionPerformed
        
        Producto auxPro = sucAux.devuelveProducto(jtxtNumeroCC.getText());
        ArrayList<Movimiento> auxMov = auxPro.getMovimiento();
        
        DefaultListModel dlm = new DefaultListModel();
        jlstMov.setModel(dlm);
        jlstMov.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        Iterator iterador = auxMov.listIterator();
        
        while( iterador.hasNext() ) {
            Movimiento m = (Movimiento) iterador.next();
            dlm.addElement( m.getStConcBreve() + "   " + m.getDImporte() + " €");
        }
    }//GEN-LAST:event_jbtnBuscarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
                auxJf.setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

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

        if(jlstMov.getSelectedIndex() == -1)
        {
            jlblDetalles.setText("Debe seleccionar un movimiento.");
        } else
            {
            Producto auxPro = sucAux.devuelveProducto(jtxtNumeroCC.getText());
            ArrayList<Movimiento> auxMov = auxPro.getMovimiento();

            Iterator iterador = auxMov.listIterator();

            Movimiento m = new Movimiento();
            for (int i = -1 ; i < jlstMov.getSelectedIndex() ; i++)
            {
                iterador.hasNext();
                m = (Movimiento) iterador.next();
            }

            jlblDetalles.setText( m.getStConcBreve() + "   " + m.getDImporte() + " €" + m.getDatOpe());
            }
    }//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 jfrmMovimientos().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnBuscar;
    private javax.swing.JButton jbtnDetalles;
    private javax.swing.JLabel jlblDetalles;
    private javax.swing.JList jlstMov;
    private javax.swing.JTextField jtxtNumeroCC;
    private java.awt.Label label2;
    // End of variables declaration//GEN-END:variables

}
