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

/*
 * jfrmMovimientos.java
 *
 * Created on 02-dic-2009, 18:14:52
 */

package interfaz;

import daial.Caja;
import daial.Movimiento;
import daial.Producto;
import daial.Sucursal;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Alberto
 */
public class jfrmMovimientos extends javax.swing.JFrame {

    private Sucursal s = new Sucursal();
    private String ctaActiva = "";
    jfrmPrincipal p;
    private Producto produc = new Producto();
    ArrayList<Movimiento> lista_mov;
    String elemento_list = "";

    /** Creates new form jfrmMovimientos */
    public jfrmMovimientos(Sucursal suc, String cta, jfrmPrincipal principal, Caja caja_activa) {
        initComponents();
        this.setLocationRelativeTo(null);
        s = suc;
        ctaActiva = cta;
        p = new jfrmPrincipal(suc,caja_activa);
        p = principal;

        mostrar_ult_mov();
    }

    private void mostrar_ult_mov()
    {
        int contador = 0;
        produc = s.buscarCuenta(ctaActiva);
        lista_mov = produc.getMovimiento();

        DefaultListModel dlm = new DefaultListModel();
        jL_lista.setModel(dlm);
        jL_lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        if(!lista_mov.isEmpty())
        {
            for(int i=lista_mov.size(); i>0 && contador<10 ;i--)
            {
                contador++;
                elemento_list = (contador) + " // " + lista_mov.get(i-1).getFechaOperacion().toString() + " // " + lista_mov.get(i-1).getImporte() + " // " + lista_mov.get(i-1).getConcepto();
                dlm.addElement(elemento_list);
            }
        }
        else
        {
            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "No hay movimientos en esta cuenta", "Aviso", JOptionPane.INFORMATION_MESSAGE);
        }

    }

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

        jLabel1 = new javax.swing.JLabel();
        jBtn_Atras = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jL_lista = new javax.swing.JList();
        jBtn_Seleccionar = new javax.swing.JButton();

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

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Últimos movimientos");

        jBtn_Atras.setText("Atrás");
        jBtn_Atras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtn_AtrasActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jL_lista);

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(170, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(161, 161, 161))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(156, Short.MAX_VALUE)
                .addComponent(jBtn_Seleccionar)
                .addGap(56, 56, 56)
                .addComponent(jBtn_Atras)
                .addGap(138, 138, 138))
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(36, 36, 36)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtn_Seleccionar)
                    .addComponent(jBtn_Atras))
                .addGap(44, 44, 44))
        );

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

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

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

    private void jBtn_SeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_SeleccionarActionPerformed
        // TODO add your handling code here:
        String cadena;
        int j=0;
        cadena = jL_lista.getSelectedValue().toString();
        cadena = cadena.substring(5);

        while(!cadena.substring(j,j+1).equals("/"))
        {
            j++;
        }

        cadena = cadena.substring(0,j-1);

        jfrmDetallesMov D_mov = new jfrmDetallesMov(this, s, cadena, lista_mov);
        this.setEnabled(false);
        D_mov.show();

}//GEN-LAST:event_jBtn_SeleccionarActionPerformed

    /**
    * @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.JButton jBtn_Atras;
    private javax.swing.JButton jBtn_Seleccionar;
    private javax.swing.JList jL_lista;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
