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

/*
 * JFCuenta.java
 *
 * Created on 13-oct-2010, 14:20:41
 */

package vistas;

import javabbuva.*;

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

    Sucursal su;
    String DNIprincipal;
    JFPrincipal p;

    /** Creates new form JFCuenta */
    public JFCuenta(Sucursal su, JFPrincipal p) {
        this.su = su;
        DNIprincipal = "";
        this.p = p;
        initComponents();
        jBTerminar.setVisible(false);
        p.setEnabled(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() {

        jLnombre = new javax.swing.JLabel();
        jLapellido1 = new javax.swing.JLabel();
        jLDireccion = new javax.swing.JLabel();
        jLDNI = new javax.swing.JLabel();
        jTapellido1 = new javax.swing.JTextField();
        jTDNI = new javax.swing.JTextField();
        jTnombre = new javax.swing.JTextField();
        jTDireccion = new javax.swing.JTextField();
        jLTitulo = new javax.swing.JLabel();
        jLTelefono = new javax.swing.JLabel();
        jLapellido2 = new javax.swing.JLabel();
        JTtelefono = new javax.swing.JTextField();
        jTapellido2 = new javax.swing.JTextField();
        jCmodo = new javax.swing.JCheckBox();
        jBAceptar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTSaldo = new javax.swing.JTextField();
        jBMas = new javax.swing.JButton();
        jLerror = new javax.swing.JLabel();
        jBTerminar = new javax.swing.JButton();

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

        jLnombre.setText("Nombre: *");

        jLapellido1.setText("Primer apellido:");

        jLDireccion.setText("Dirección: ");

        jLDNI.setText("DNI: *");

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLTitulo.setText("Creando nueva cuenta corriente.");

        jLTelefono.setText("Telefono:");

        jLapellido2.setText("Segundo apellido:");

        jCmodo.setSelected(true);
        jCmodo.setText("Cliente fisico");
        jCmodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCmodoActionPerformed(evt);
            }
        });

        jBAceptar.setText("Aceptar");
        jBAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAceptarActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Saldo: *");

        jBMas.setText("Añadir mas");
        jBMas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBMasActionPerformed(evt);
            }
        });

        jLerror.setForeground(new java.awt.Color(153, 0, 0));

        jBTerminar.setText("Terminar");
        jBTerminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTerminarActionPerformed(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()
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLnombre)
                            .addComponent(jLapellido1)
                            .addComponent(jLDireccion)
                            .addComponent(jLDNI)
                            .addComponent(jLabel1))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTapellido1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTDNI, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTnombre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTDireccion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLTitulo)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLapellido2)
                            .addComponent(jLTelefono))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(JTtelefono)
                            .addComponent(jTapellido2, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                        .addContainerGap(58, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLerror, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                            .addComponent(jCmodo))
                        .addGap(46, 46, 46))))
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jBAceptar)
                .addGap(56, 56, 56)
                .addComponent(jBCancelar)
                .addGap(61, 61, 61)
                .addComponent(jBMas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addComponent(jBTerminar)
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jCmodo))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLTitulo)))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLnombre)
                    .addComponent(jTnombre, 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.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLapellido1)
                            .addComponent(jTapellido1, 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(jTDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLDNI)
                            .addComponent(jLTelefono))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLDireccion)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTapellido2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLapellido2))
                        .addGap(18, 18, 18)
                        .addComponent(JTtelefono, 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(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLerror, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAceptar)
                    .addComponent(jBMas)
                    .addComponent(jBCancelar)
                    .addComponent(jBTerminar))
                .addGap(22, 22, 22))
        );

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

    private void jCmodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCmodoActionPerformed
        // TODO add your handling code here:
        if(jCmodo.isSelected()){
            jLDNI.setText("DNI: *");
            jLapellido1.setText("Primer apellido:");
            jLapellido2.setText("Segundo apellido:");
            jTapellido1.enable(true);
            jTapellido2.enable(true);
            jBMas.setVisible(false);
        } else{
            jLDNI.setText("NIF: *");
            jLapellido1.setText("ND");
            jLapellido2.setText("ND");
            jTapellido1.enable(false);
            jTapellido2.enable(false);
            jBMas.setVisible(false);
        }
}//GEN-LAST:event_jCmodoActionPerformed

    private void jBAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAceptarActionPerformed
        // TODO add your handling code here:
        Boolean error = false;
        jLerror.setText("");
        //COMPROBACIONES PREVIAS
        if(jTSaldo.getText().compareTo("") == 0 || jTnombre.getText().compareTo("") == 0 || jTDNI.getText().compareTo("") == 0){
            jLerror.setText("Introduce los datos obligatorios");
        }

        else{
        //SI ES UN CLIENTE FISICO
        if(jCmodo.isSelected()){
            String nombre = jTnombre.getText();
            String apellido1 = jTapellido1.getText();
            String apellido2 = jTapellido2.getText();
            String DNI = jTDNI.getText();
            String telefono = JTtelefono.getText();
            String direccion = jTDireccion.getText();
            float saldo = Float.parseFloat(jTSaldo.getText());

            if(saldo <0){
                error = true;
            }
            else{
                su.crearProductoF(nombre, apellido1, apellido2, DNI, telefono, direccion, "Cuenta Corriente", saldo);
            }
        }
        //SI ES UN CLIENTE JURIDICO
        else{
            String nombre = jTnombre.getText();
            String NIF = jTDNI.getText();
            String telefono = JTtelefono.getText();
            String direccion = jTDireccion.getText();
            float saldo = Float.parseFloat(jTSaldo.getText());

            if(saldo<0){
            error = true;
            }
            else{
                su.crearProductoJ(nombre, NIF, telefono, direccion, "Cuenta Corriente", saldo);
            }
        }
        if(error == false){
        this.dispose();
        p.setVisible(true);
        }
        else{
            jLerror.setText("Error en saldo");
        }
        }
}//GEN-LAST:event_jBAceptarActionPerformed

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
        p.setEnabled(true);
}//GEN-LAST:event_jBCancelarActionPerformed

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

    private void jBMasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBMasActionPerformed
        // TODO add your handling code here:
        jLerror.setText("");
        Boolean error = false;

        //COMPROBACIONES PREVIAS
        if(jTSaldo.getText().compareTo("") == 0 || jTnombre.getText().compareTo("") == 0 || jTDNI.getText().compareTo("") == 0){
            jLerror.setText("Introduce los datos obligatorios");
        }
        else{
            jBAceptar.setVisible(false);
            jBCancelar.setVisible(false);
            jBTerminar.setVisible(true);
            jBMas.setVisible(false);
            String nombre = jTnombre.getText();
            String apellido1 = jTapellido1.getText();
            String apellido2 = jTapellido2.getText();
            String DNI = jTDNI.getText();
            String telefono = JTtelefono.getText();
            String direccion = jTDireccion.getText();
            float saldo = Float.parseFloat(jTSaldo.getText());

            if(saldo <0){
                error = true;
            }
            else{
                if(DNIprincipal.equals("")){
                    DNIprincipal = DNI;
                }
                su.crearProductoF(nombre, apellido1, apellido2, DNI, telefono, direccion, "Cuenta Corriente", saldo);
            }
        if(error == false){
            new JFAñadirMas(su, DNIprincipal).setVisible(true);
        }
        else{
            jLerror.setText("Error en saldo");
        }
        }

}//GEN-LAST:event_jBMasActionPerformed

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTtelefono;
    private javax.swing.JButton jBAceptar;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBMas;
    private javax.swing.JButton jBTerminar;
    private javax.swing.JCheckBox jCmodo;
    private javax.swing.JLabel jLDNI;
    private javax.swing.JLabel jLDireccion;
    private javax.swing.JLabel jLTelefono;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLapellido1;
    private javax.swing.JLabel jLapellido2;
    private javax.swing.JLabel jLerror;
    private javax.swing.JLabel jLnombre;
    private javax.swing.JTextField jTDNI;
    private javax.swing.JTextField jTDireccion;
    private javax.swing.JTextField jTSaldo;
    private javax.swing.JTextField jTapellido1;
    private javax.swing.JTextField jTapellido2;
    private javax.swing.JTextField jTnombre;
    // End of variables declaration//GEN-END:variables

}
