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

/*
 * JFVerCuentas.java
 *
 * Created on 13-oct-2010, 13:21:13
 */

package vistas;

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

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

    Sucursal su;
    JFPrincipal p;
    //Para saber si llamamos desde ingreso o reintegro o incluso movimientos
    public boolean ingreso;
    //Para saber si llamamos desde transferencia
    public boolean transferencia;
    public boolean transferencia2;
    /** Creates new form JFVerCuentas */
    public JFVerCuentas(Sucursal su, JFPrincipal p) {
        this.su = su;
        this.p = p;
        initComponents();
        p.setEnabled(false);
        ingreso = false;
        transferencia = false;
    }

    /** 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();
        jLNombre = new javax.swing.JLabel();
        jLDNI = new javax.swing.JLabel();
        jTnombre = new javax.swing.JTextField();
        jTDNI = new javax.swing.JTextField();
        jBVerCuentas = new javax.swing.JButton();
        jScuentas = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jBTerminar = new javax.swing.JButton();
        jLMensaje = new javax.swing.JLabel();
        jRFisico = new javax.swing.JRadioButton();
        jBSeleccionar = new javax.swing.JButton();
        jSnumerocuenta = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = 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 cuentas de un cliente.");

        jLNombre.setText("Nombre: ");

        jLDNI.setText("DNI: ");

        jTnombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTnombreActionPerformed(evt);
            }
        });

        jBVerCuentas.setText("Ver cuentas");
        jBVerCuentas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBVerCuentasActionPerformed(evt);
            }
        });

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

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

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

        jRFisico.setSelected(true);
        jRFisico.setText("Cliente Fisico");
        jRFisico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRFisicoActionPerformed(evt);
            }
        });

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

        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList2MouseClicked(evt);
            }
        });
        jSnumerocuenta.setViewportView(jList2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(0, 102, 0));
        jLabel1.setText("Numero de cuenta:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLTitulo))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLNombre)
                                            .addComponent(jLDNI))
                                        .addGap(38, 38, 38)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTDNI)
                                            .addComponent(jTnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jBVerCuentas))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(89, 89, 89)
                                .addComponent(jRFisico))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addComponent(jLMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jScuentas, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jSnumerocuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(28, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBTerminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 368, Short.MAX_VALUE)
                .addComponent(jBSeleccionar)
                .addGap(71, 71, 71))
        );
        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(jLTitulo)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLNombre)
                                .addGap(18, 18, 18)
                                .addComponent(jLDNI))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRFisico)
                        .addGap(31, 31, 31)
                        .addComponent(jLMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jBVerCuentas)
                        .addGap(18, 27, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSnumerocuenta, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScuentas, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBTerminar)
                    .addComponent(jBSeleccionar))
                .addContainerGap())
        );

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

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

    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 jBVerCuentasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBVerCuentasActionPerformed
        // TODO add your handling code here:
        jLMensaje.setText("");
        DefaultListModel mod=new DefaultListModel();
        DefaultListModel mod2=new DefaultListModel();
        jList1.setModel(mod);
        jList2.setModel(mod);
        if(jTnombre.getText().compareTo("")==0||jTDNI.getText().compareTo("")==0){
            jLMensaje.setText("Introduzca los datos");
        }
        else{
            String nombre = jTnombre.getText();
            String DNI = jTDNI.getText();
            List<Producto> pro = new ArrayList();
            //Si es un cliente fisico
            if(jRFisico.isSelected()){
                pro = su.getProductoClienteF(nombre, DNI); //Tenemos la lista de los productos del cliente
                if(pro.size() == 0){ //Si es cero no tiene productos
                    jLMensaje.setText("No existe el cliente");
                }
                else{ //Si no, si tiene productos
                    int i = 0;
                    String mostrar = "";
                    Producto aux;
                    while(i<pro.size()){ //Añadimos los productos a un ListModel
                        aux = su.dameCuenta(pro.get(i).getNumeroCuenta());
                        mostrar = aux.getTipo()+"  "+aux.getDinero()+" €  Numero titulares: "+aux.getNumeroTitulares();
                        mod.addElement(mostrar);
                        mod2.addElement(pro.get(i).getNumeroCuenta());
                        i++;
                        mostrar="";
                    }
                    jList1.setModel(mod); //Los añadimos a la lista
                    jList2.setModel(mod2);
            }
            }
            //Si es un cliente juridico
            else{
                pro = su.getProductoClienteJ(nombre, DNI); //Tenemos la lista de los productos del cliente
                if(pro.size() == 0){ //Si es cero no tiene productos
                    jLMensaje.setText("No existe el cliente");
                }
                else{ //Si no si tiene productos
                    int i = 0;
                    String mostrar = "";
                    while(i<pro.size()){ //Añadimos los productos a un ListModel
                        mostrar = pro.get(i).getTipo()+"  "+pro.get(i).getDinero()+" €"+"  Numero titulares: "+pro.get(i).getNumeroTitulares();
                        mod.addElement(mostrar);
                        mod2.addElement(pro.get(i).getNumeroCuenta());
                        i++;
                        mostrar="";
                    }
                    jList1.setModel(mod); //Los añadimos a la lista
                    jList2.setModel(mod2);
            }
            }
        }
    }//GEN-LAST:event_jBVerCuentasActionPerformed

    private void jRFisicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRFisicoActionPerformed
        // TODO add your handling code here:
        if(jRFisico.isSelected()){
        jLDNI.setText("DNI: ");
        }
        else{
        jLDNI.setText("NIF: ");
        }
}//GEN-LAST:event_jRFisicoActionPerformed

    private void jBSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSeleccionarActionPerformed
        // TODO add your handling code here:
        jLMensaje.setText("");
        if(ingreso == true){
            if(jList2.getSelectedIndex() == -1){
                jLMensaje.setText("Seleccione una cuenta");
            }
            else{
                p.auxiliar = String.valueOf(jList2.getSelectedValue());
                this.ingreso = false;
                p.escogida = true;
                this.dispose();
            }
        }
        else if(transferencia == true){
            if(jList2.getSelectedIndex() == -1){
                jLMensaje.setText("Seleccione una cuenta");
            }
            else{
                p.auxiliar = String.valueOf(jList2.getSelectedValue());
                p.escogida = true;
                this.dispose();
            }
        }
        else if(transferencia2 == true){
            if(jList2.getSelectedIndex() == -1){
                jLMensaje.setText("Seleccione una cuenta");
            }
            else{
                p.auxiliardes = String.valueOf(jList2.getSelectedValue());
                p.escogidades = true;
                this.dispose();
            }
        }
        else{
            jLMensaje.setText("Accion no aplicable");
        }
    }//GEN-LAST:event_jBSeleccionarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        if(ingreso == false && transferencia == false && transferencia2 == false) //si no llamamos de ingreso hacemos visible principal
            p.setEnabled(true);
        else{
            ingreso = false;
            transferencia = false;
            transferencia2 = false;
        }
    }//GEN-LAST:event_formWindowClosed

    private void jList2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList2MouseClicked
        // TODO add your handling code here:
        if(jList2.getSelectedIndex()!=-1 && ingreso == false && transferencia == false && transferencia2 == false){
            new JFVerTitularesEspecial(su,this, String.valueOf(jList2.getSelectedValue())).setVisible(true);
        }
    }//GEN-LAST:event_jList2MouseClicked

    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:
        if(ingreso == false && transferencia == false && transferencia2 == false){

        }
        else{
        DefaultListModel m = new DefaultListModel();
        jTDNI.setText("");
        jTnombre.setText("");
        jLMensaje.setText("");
        jList1.setModel(m);
        jList2.setModel(m);
        }
    }//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 JFVerCuentas().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBSeleccionar;
    private javax.swing.JButton jBTerminar;
    private javax.swing.JButton jBVerCuentas;
    private javax.swing.JLabel jLDNI;
    private javax.swing.JLabel jLMensaje;
    private javax.swing.JLabel jLNombre;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JRadioButton jRFisico;
    private javax.swing.JScrollPane jScuentas;
    private javax.swing.JScrollPane jSnumerocuenta;
    private javax.swing.JTextField jTDNI;
    private javax.swing.JTextField jTnombre;
    // End of variables declaration//GEN-END:variables

}
