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

/*
 * JFPrestamoP.java
 *
 * Created on 13-oct-2010, 18:59:52
 */

package vistas;

import javabbuva.*;

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

    boolean correcto;
    Sucursal su;
    JFPrincipal p;

    /** Creates new form JFPrestamoP */
    public JFPrestamoP(Sucursal su, JFPrincipal p) {
        this.su = su;
        correcto = false;
        this.p = p;
        initComponents();
        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() {

        jLTitulo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTNombre = new javax.swing.JTextField();
        jTCuenta = new javax.swing.JTextField();
        jBAceptar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jBComprobar = new javax.swing.JButton();
        jLerror = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTcantidad = new javax.swing.JTextField();
        jCFisico = new javax.swing.JCheckBox();

        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);
            }
        });

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLTitulo.setText("Creando nuevo prestamo personal.");

        jLabel1.setText("DNI titular:");

        jLabel2.setText("Cuenta asociada: ");

        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);
            }
        });

        jBComprobar.setText("Comprobar");
        jBComprobar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBComprobarActionPerformed(evt);
            }
        });

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

        jLabel3.setText("Cantidad:");

        jCFisico.setSelected(true);
        jCFisico.setText("Cliente fisico");
        jCFisico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCFisicoActionPerformed(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()
                .addContainerGap()
                .addComponent(jBAceptar)
                .addGap(78, 78, 78)
                .addComponent(jBCancelar)
                .addContainerGap(323, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLTitulo)
                        .addGap(147, 147, 147))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(58, 58, 58)
                                .addComponent(jTcantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLerror, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(23, 23, 23)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCFisico)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jBComprobar)))
                .addGap(37, 37, 37))
        );
        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(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .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.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBComprobar))
                        .addGap(31, 31, 31))
                    .addComponent(jCFisico))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
                .addComponent(jLerror, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTcantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAceptar)
                    .addComponent(jBCancelar))
                .addContainerGap())
        );

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

    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 jBComprobarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBComprobarActionPerformed
        // TODO add your handling code here:
        jLerror.setText("");
        correcto = false;
        //Cliente fisico
        if(jCFisico.isSelected()){
            //Si existe coincidencia
            if(su.comprobarCoincidenciaF(jTNombre.getText(),jTCuenta.getText())){
                jLerror.setText("Cuenta asociada.");
                correcto = true;
            }
            else{
                jLerror.setText("No existe coincidencia.");
            }
        }
        //Cliente juridico
        else{
            //Si no existe coincidencia
            if(su.comprobarCoincidenciaJ(jTNombre.getText(),jTCuenta.getText())){
                jLerror.setText("Cuenta asociada.");
            }
            else{
                jLerror.setText("No existe coincidencia.");
            }
        }
    }//GEN-LAST:event_jBComprobarActionPerformed

    private void jCFisicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCFisicoActionPerformed
        // TODO add your handling code here:
        correcto = false;
        if(jCFisico.isSelected())
            jLabel1.setText("DNI titular: ");
        else
            jLabel1.setText("NIF titular: ");
}//GEN-LAST:event_jCFisicoActionPerformed

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

        else{
             if(correcto == false){
                jLerror.setText("Datos incorrectos");
             }
             else{
        //SI ES UN CLIENTE FISICO
        if(jCFisico.isSelected()){
            String DNI = jTNombre.getText();
            String cuenta = jTCuenta.getText();
            float cantidad = Float.parseFloat(jTcantidad.getText());

            if(cantidad <0){
                error = true;
            }
            else{
                su.crearPrestamoF(DNI, cuenta, cantidad, "Pestamo Personal");
            }
        }
        //SI ES UN CLIENTE JURIDICO
        else{
            String NIF = jTNombre.getText();
            String cuenta = jTCuenta.getText();
            float cantidad = Float.parseFloat(jTcantidad.getText());

            if(cantidad<0){
            error = true;
            }
            else{
                su.crearPrestamoJ(NIF, cuenta, cantidad, "Pestamo Personal");
            }
        }
        if(error == false){
        this.dispose();
        p.setVisible(true);
        }
        else{
            jLerror.setText("Error en la cantidad");
        }
        }
        }
    }//GEN-LAST:event_jBAceptarActionPerformed

    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 JFPrestamoP().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAceptar;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBComprobar;
    private javax.swing.JCheckBox jCFisico;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLerror;
    private javax.swing.JTextField jTCuenta;
    private javax.swing.JTextField jTNombre;
    private javax.swing.JTextField jTcantidad;
    // End of variables declaration//GEN-END:variables

}
