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

/*
 * jfrmNuevaCuenta.java
 *
 * Created on 16-dic-2009, 18:36:26
 */

package interfaz;

import daial.*;
import daial.Sucursal;
import javax.swing.JOptionPane;

/**
 *
 * @author FeNiX
 */
public class jfrmNuevaCuenta extends javax.swing.JFrame {

    private Sucursal s = new Sucursal();
    jfrmPrincipal p;

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

        //jtxtnumeroasig.setText(s.sigNumeroCuentaMax(jtxtDC.getText()));
    }

    /** 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();
        jCbBx_tipoCuenta = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jtxtNombreTitular = new javax.swing.JTextField();
        jBtn_crearcuenta = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jtxtnumeroasig = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtxtDC = new javax.swing.JTextField();
        jBtn_buscarnumerolibre = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nombre del Titular:");

        jCbBx_tipoCuenta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cuenta Corriente", "Libreta de Ahorro", "Prestamo Hipotecario", "Prestamo Personal" }));

        jLabel2.setText("Tipo de cuenta:");

        jBtn_crearcuenta.setText("CREAR");
        jBtn_crearcuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtn_crearcuentaActionPerformed(evt);
            }
        });

        jButton1.setText("Atras");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Numero de Cuenta");

        jtxtnumeroasig.setBackground(new java.awt.Color(250, 250, 250));
        jtxtnumeroasig.setEditable(false);

        jLabel4.setText("Digito de Control:");

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

        jBtn_buscarnumerolibre.setText("Buscar Numero de Cuenta Libre");
        jBtn_buscarnumerolibre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtn_buscarnumerolibreActionPerformed(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()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtxtNombreTitular, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                            .addGap(27, 27, 27))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel2))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jCbBx_tipoCuenta, 0, 259, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(76, 76, 76)
                                    .addComponent(jBtn_buscarnumerolibre)))
                            .addContainerGap()))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtDC, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtnumeroasig, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(104, 104, 104))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(157, Short.MAX_VALUE)
                .addComponent(jBtn_crearcuenta)
                .addGap(157, 157, 157))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(287, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtNombreTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCbBx_tipoCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtDC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtn_buscarnumerolibre))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtnumeroasig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBtn_crearcuenta)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(19, 19, 19))
        );

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

    private void jBtn_crearcuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_crearcuentaActionPerformed
        String seleccion=jCbBx_tipoCuenta.getSelectedItem().toString();
        if (jtxtnumeroasig.getText().compareTo("")==0){
            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "Debe buscar un Numero de Cuenta libre para poder crearla", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else if (jtxtNombreTitular.getText().compareTo("")==0){
            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "Debe asignar un nombre de Titular a la cuenta", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            if(seleccion.compareTo("Cuenta Corriente")==0){
                CtaCorriente prod = new CtaCorriente();
                prod.setNombreCliente(jtxtNombreTitular.getText());
                prod.setNumeroProducto(s.sigNumeroCuentaMax(jtxtDC.getText()));
                prod.setSaldo(0);
                s.insertarProducto(prod);
            }else if(seleccion.compareTo("Libreta de Ahorro")==0){
                LibretaAhorro prod = new LibretaAhorro();
                prod.setNombreCliente(jtxtNombreTitular.getText());
                prod.setNumeroProducto(s.sigNumeroCuentaMax(jtxtDC.getText()));
                prod.setSaldo(0);
                s.insertarProducto(prod);
            }else if(seleccion.compareTo("Prestamo Hipotecario")==0){
                PtmoPersonal prod = new PtmoPersonal();
                prod.setNombreCliente(jtxtNombreTitular.getText());
                prod.setNumeroProducto(s.sigNumeroCuentaMax(jtxtDC.getText()));
                prod.setSaldo(0);
                s.insertarProducto(prod);
            }else if(seleccion.compareTo("Prestamo Personal")==0){
                PtmoHipotecario prod = new PtmoHipotecario();
                prod.setNombreCliente(jtxtNombreTitular.getText());
                prod.setNumeroProducto(s.sigNumeroCuentaMax(jtxtDC.getText()));
                prod.setSaldo(0);
                s.insertarProducto(prod);
            }
            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "Producto incluido con éxito", "Aviso", JOptionPane.INFORMATION_MESSAGE);
            p.setEnabled(true);
            this.dispose();
        }

}//GEN-LAST:event_jBtn_crearcuentaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        p.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

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

    private void jBtn_buscarnumerolibreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_buscarnumerolibreActionPerformed
        if(jtxtDC.getText().length() != 2){
            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "El Digito de Control debe tener 2 digitos", "Error", JOptionPane.ERROR_MESSAGE);
            jtxtDC.setText("");
        }
        else{
            jtxtnumeroasig.setText(s.sigNumeroCuentaMax(jtxtDC.getText()));
        }
    }//GEN-LAST:event_jBtn_buscarnumerolibreActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtn_buscarnumerolibre;
    private javax.swing.JButton jBtn_crearcuenta;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCbBx_tipoCuenta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jtxtDC;
    private javax.swing.JTextField jtxtNombreTitular;
    private javax.swing.JTextField jtxtnumeroasig;
    // End of variables declaration//GEN-END:variables

}
