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

/*
 * jfrmDomiciliaciones.java
 *
 * Created on 27-ene-2010, 16:24:01
 */

package Vistas;

import Modelo.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.ListSelectionModel;

/**
 *
 * @author acc_local
 */
public class jfrmDomiciliaciones extends javax.swing.JFrame implements Observer{
    Sucursal sucAux = new Sucursal();
    JFrame auxJf;
    /** Creates new form jfrmDomiciliaciones */
    public jfrmDomiciliaciones(Sucursal suc, JFrame jf) {
        auxJf=jf;
        initComponents();
        sucAux=suc;

        jtxtNumeroCC.setText(sucAux.getStCCOpera());

        jtxtSaldoActualCaja.setText(String.valueOf(sucAux.saldoCaja(sucAux.dameDNIEmpleado())));
        Caja c = sucAux.busquedaCajaSucursal(sucAux.dameIdCaja());
        c.addObserver(this);
    }

    /** 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();
        jtxtSaldoActualCaja = new javax.swing.JTextField();
        jlblDoc1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtxtNumeroCC = new javax.swing.JTextField();
        jbtnBuscar = new javax.swing.JButton();
        jbtnSeleccionarCuenta = new javax.swing.JButton();
        jlblDetalles = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstDom = new javax.swing.JList();

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

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vistas/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("Domiciliaciones");

        jtxtSaldoActualCaja.setEditable(false);
        jtxtSaldoActualCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtSaldoActualCajaActionPerformed(evt);
            }
        });

        jlblDoc1.setFont(new java.awt.Font("Comic Sans MS", 0, 10));
        jlblDoc1.setText("Saldo Caja:");

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

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

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

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

        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()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(85, 85, 85))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addComponent(jLabel1)
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblDoc1))
                        .addGap(20, 20, 20)))
                .addGap(76, 76, 76))
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDetalles, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 537, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(64, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel5)
                    .addGap(18, 18, 18)
                    .addComponent(jtxtNumeroCC, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addComponent(jbtnBuscar)
                    .addGap(18, 18, 18)
                    .addComponent(jbtnSeleccionarCuenta)
                    .addContainerGap(20, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(jlblDoc1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel6)))
                .addGap(69, 69, 69)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jlblDetalles, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(208, 208, 208)
                    .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)
                        .addComponent(jbtnSeleccionarCuenta))
                    .addContainerGap(253, Short.MAX_VALUE)))
        );

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

    private void jtxtSaldoActualCajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtSaldoActualCajaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jtxtSaldoActualCajaActionPerformed

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

        if(jtxtNumeroCC.getText().equals("")) {
            jlblDetalles.setText("Debes introducir Cuenta");
        }
        else {
            Cliente auxC = sucAux.devuelveCliente(jtxtNumeroCC.getText());
            //System.out.println("Ya tengo el cliente" + auxC.getNombreCliente());

            List<Domiciliacion> arrDom = auxC.getMDomiciliacion();
                        
            DefaultListModel dlm = new DefaultListModel();
            jlstDom.setModel(dlm);
            jlstDom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            Iterator iterador = arrDom.listIterator();

            Calendar c = Calendar.getInstance();
            String dia;
            String mes;
            String ano;

            while( iterador.hasNext() ) {
                Domiciliacion d = (Domiciliacion) iterador.next();

                c.setTime(d.getDUltima());
                dia = Integer.toString(c.get(Calendar.DATE));
                mes = Integer.toString(c.get(Calendar.MONTH)+1);
                ano = Integer.toString(c.get(Calendar.YEAR));

                dlm.addElement(     d.getStOrdenante() +
                                    "     " +
                                    dia +
                                    "/" +
                                    mes +
                                    "/" +
                                    ano +
                                    "   " + d.getDCantidad() + " €");
            }
        }
}//GEN-LAST:event_jbtnBuscarActionPerformed

    private void jbtnSeleccionarCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSeleccionarCuentaActionPerformed
        new jfrmSeleccionarCuentaCliente(sucAux,this,jtxtNumeroCC,true).setVisible(true);
        this.setEnabled(false);
        // TODO add your handling code here:
}//GEN-LAST:event_jbtnSeleccionarCuentaActionPerformed

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

    public void update(Observable o, Object arg) {
        jtxtSaldoActualCaja.setText(String.valueOf(sucAux.saldoCaja(sucAux.dameDNIEmpleado())));
    }

    /**
    * @param args the command line arguments
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmDomiciliaciones().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 jbtnSeleccionarCuenta;
    private javax.swing.JLabel jlblDetalles;
    private javax.swing.JLabel jlblDoc1;
    private javax.swing.JList jlstDom;
    private javax.swing.JTextField jtxtNumeroCC;
    private javax.swing.JTextField jtxtSaldoActualCaja;
    private java.awt.Label label2;
    // End of variables declaration//GEN-END:variables

}
