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

/*
 * jfrmAperturaPrestamo.java
 *
 * Created on 28-ene-2010, 20:28:04
 */

package Vistas;
import Hito4.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
/**
 *
 * @author Ro
 */
public class jfrmAperturaPrestamo extends javax.swing.JFrame {
    Sucursal auxsuc;
    javax.swing.JFrame auxjf;

    /** Creates new form jfrmAperturaPrestamo */
    public jfrmAperturaPrestamo(Sucursal suc, javax.swing.JFrame jf) {
        auxsuc=suc;
        auxjf=jf;
        initComponents();
    }

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

        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jtxtDni = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstCuentas = new javax.swing.JList();
        jbtnValida = new javax.swing.JButton();
        jbtnSelecciona = new javax.swing.JButton();
        jbtnSalir = new javax.swing.JButton();
        jlblMensaje = new javax.swing.JLabel();
        jlblNumCta = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel3.setFont(new java.awt.Font("Papyrus", 1, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 102));
        jLabel3.setText("Banco ALROMA");

        jLabel1.setFont(new java.awt.Font("Papyrus", 1, 18)); // NOI18N
        jLabel1.setText("Apertura Prestamo");

        buttonGroup2.add(jRadioButton1);
        jRadioButton1.setText("Personal");

        buttonGroup2.add(jRadioButton2);
        jRadioButton2.setText("Hipotecario");

        jLabel2.setText("DNI");

        jtxtDni.setText("24784598J");

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

        jbtnValida.setText("Valida");
        jbtnValida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnValidaActionPerformed(evt);
            }
        });

        jbtnSelecciona.setText("Selecciona");
        jbtnSelecciona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSeleccionaActionPerformed(evt);
            }
        });

        jbtnSalir.setText("Salir");
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButton2)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jtxtDni, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
                    .addComponent(jLabel1))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jbtnSelecciona)
                            .addComponent(jbtnSalir)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlblNumCta, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jbtnValida)
                .addContainerGap(194, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jLabel2)
                    .addComponent(jtxtDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtnValida)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnSelecciona)
                        .addGap(18, 18, 18)
                        .addComponent(jlblNumCta, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnSalir)
                        .addGap(25, 25, 25)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

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

    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        auxjf.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jbtnSalirActionPerformed

    private void jbtnValidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnValidaActionPerformed
/*++++++++Array*/List<Producto> a =auxsuc.busquedaProductosCliente(jtxtDni.getText(), 1);
        if( a == null){jlblMensaje.setText("El Cliente No existe.");}
        else{
            DefaultListModel dlm = new DefaultListModel();
            jlstCuentas.setModel(dlm);
            jlstCuentas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//para que solo coja un valor
            Iterator iterador = a.listIterator();

            while( iterador.hasNext() ) {
                Producto p = (Producto) iterador.next();
                if(p.getTipoProducto().compareToIgnoreCase("c")==0){
                    dlm.addElement(p.getNumeroProducto()+"  "+p.getSaldo());}
            }
        }
    }//GEN-LAST:event_jbtnValidaActionPerformed

    private void jbtnSeleccionaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSeleccionaActionPerformed
        ControladorBD cbd=new ControladorBD();
        ArrayList<Producto> AP=new ArrayList<Producto>();
        ArrayList<Cliente> AC=new ArrayList<Cliente>();

        String cadena=(String) jlstCuentas.getSelectedValue();
        jlblNumCta.setText(cadena.substring(0, 20));

        Cliente c= new Cliente();
        c=auxsuc.busquedaCliente(jtxtDni.getText());

        if(jRadioButton1.isSelected()){
            Producto p=new PrestamoPersonal();
            p=auxsuc.devuelveProducto(cadena);
            p.setTipoProducto("p");
            c.insertaProductoenCliente(p);
            AP.add(p);
            auxsuc.setProducto(AP);
            cbd.persist(c);
        }else{
            if(jRadioButton2.isSelected()){
                Producto p=new PrestamoHipotecario();
                p=auxsuc.devuelveProducto(cadena);
                p.setTipoProducto("h");
                c.insertaProductoenCliente(p);
                AP.add(p);
                auxsuc.setProducto(AP);
                cbd.persist(c);
            }
        }
    }//GEN-LAST:event_jbtnSeleccionaActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JButton jbtnSelecciona;
    private javax.swing.JButton jbtnValida;
    private javax.swing.JLabel jlblMensaje;
    private javax.swing.JLabel jlblNumCta;
    private javax.swing.JList jlstCuentas;
    private javax.swing.JTextField jtxtDni;
    // End of variables declaration//GEN-END:variables

}
