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

/*
 * JFUltimosMovimientos.java
 *
 * Created on 13-oct-2010, 13:20:18
 */

package vistas;

//import java.util.ArrayList;
import javabbuva.Producto;
import java.util.List;
import javabbuva.*;
import javax.swing.DefaultListModel;

/**
 *
 * @author Adrian
 */
public class JFUltimosMovimientos extends javax.swing.JFrame {

    Sucursal su;
    JFPrincipal p;
    JFVerCuentas ver; //Creamos aqui el ver cuentas para tener acceso a el

    //Para movimiento en detalle
    public boolean detalle;

    /** Creates new form JFUltimosMovimientos */
    public JFUltimosMovimientos(Sucursal su, JFPrincipal p) {
        this.su = su;
        this.p = p;
        initComponents();
        p.setEnabled(false);
        detalle = false;
        ver = new JFVerCuentas(su,p);
    }

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

        jLTitulo = new javax.swing.JLabel();
        jBSeleccionar = new javax.swing.JButton();
        jSMovimientos = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jBTerminar = new javax.swing.JButton();
        jBActualizar = new javax.swing.JButton();
        jLError = new javax.swing.JLabel();
        jSIdentificador = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jBSelec = new javax.swing.JButton();
        jLSaldo1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLTitulo.setText("Ver movimientos de una cuenta.");

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

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setEnabled(false);
        jSMovimientos.setViewportView(jList1);

        jBTerminar.setText("Terminar");
        jBTerminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTerminarActionPerformed(evt);
            }
        });

        jBActualizar.setText("Actualizar");
        jBActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBActualizarActionPerformed(evt);
            }
        });

        jLError.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLError.setForeground(new java.awt.Color(153, 0, 0));

        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jSIdentificador.setViewportView(jList2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setText("ID:");

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

        jLSaldo1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLSaldo1.setForeground(new java.awt.Color(51, 51, 51));
        jLSaldo1.setText("0");
        jLSaldo1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Saldo actual en caja:");

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBTerminar)
                        .addGap(278, 278, 278)
                        .addComponent(jBSelec))
                    .addComponent(jLTitulo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLSaldo1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBSeleccionar)
                        .addGap(47, 47, 47)
                        .addComponent(jBActualizar))
                    .addComponent(jSMovimientos, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLError, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSIdentificador, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(127, 127, 127))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(403, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(143, 143, 143))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLTitulo)
                        .addGap(33, 33, 33))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLSaldo1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSeleccionar)
                    .addComponent(jBActualizar)
                    .addComponent(jLError, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jSIdentificador, 0, 0, Short.MAX_VALUE)
                    .addComponent(jSMovimientos, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBTerminar)
                    .addComponent(jBSelec))
                .addContainerGap())
        );

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

    private void jBTerminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTerminarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBTerminarActionPerformed

    private void jBSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSeleccionarActionPerformed
        // TODO add your handling code here:
        if(!ver.isVisible()){ //Si no hay uno abierto permitimos otro
        jLError.setText("");
        ver.show();
        ver.ingreso = true;
        DefaultListModel m = new DefaultListModel();
        jLError.setText("");
        jList1.setModel(m);
        jList2.setModel(m);
        }
    }//GEN-LAST:event_jBSeleccionarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        if(detalle == false)
            p.setEnabled(true);
        else
            detalle = false;
    }//GEN-LAST:event_formWindowClosed

    private void jBActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBActualizarActionPerformed
        // TODO add your handling code here:
        jLError.setText("");
        if(p.escogida){
            Producto selec = su.dameCuenta(p.auxiliar);
            List <Movimientos> m = selec.getMovimientos();
            if(m.size()!=0){
                DefaultListModel mod = new DefaultListModel();
                DefaultListModel mod2 = new DefaultListModel();
                String mostrar = "";
                String mostrar2 = "";
                int i=0;
                while(i<m.size()){
                    mostrar += m.get(i).getConcepto() + "    " + String.valueOf(m.get(i).getDinero())+ " € ";
                    mod.addElement(mostrar);
                    mostrar2 = m.get(i).getIdentificador();
                    mod2.addElement(mostrar2);
                    mostrar = "";
                    mostrar2 = "";
                    i++;
                }
                jList1.setModel(mod);
                jList2.setModel(mod2);
                p.escogida = false;
            }
            else{
                jLError.setText("No hay movimientos");
            }
        }
        else{
            jLError.setText("Seleccione primero");
        }
    }//GEN-LAST:event_jBActualizarActionPerformed

    private void jBSelecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSelecActionPerformed
        // TODO add your handling code here:
        if(detalle == false){
            jLError.setText("Accion no aplicable");
        }
        else{
            if(jList2.getSelectedIndex() == -1){
                jLError.setText("Seleccione un movimiento");
            }
            else{
                p.auxiliarmov = String.valueOf(jList2.getSelectedValue());
                p.escogidamov = true;
                this.dispose();
            }
        }





    }//GEN-LAST:event_jBSelecActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        DefaultListModel m = new DefaultListModel();
        jLError.setText("");
        jList1.setModel(m);
        jList2.setModel(m);
        jLSaldo1.setText(String.valueOf(su.getCajaActual().getSaldo()));
    }//GEN-LAST:event_formWindowActivated

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBActualizar;
    private javax.swing.JButton jBSelec;
    private javax.swing.JButton jBSeleccionar;
    private javax.swing.JButton jBTerminar;
    private javax.swing.JLabel jLError;
    private javax.swing.JLabel jLSaldo1;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jSIdentificador;
    private javax.swing.JScrollPane jSMovimientos;
    // End of variables declaration//GEN-END:variables

}
