

package vistas;

import bbuvajava.Cliente;
import bbuvajava.ControladoraBD;
import bbuvajava.Producto;
import bbuvajava.Sucursal;


public class jfrmAnadirTitular extends javax.swing.JFrame {

    Sucursal Auxsuc;
    public jfrmAnadirTitular(Sucursal suc) {
        Auxsuc=suc;
        initComponents();
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbtnRealizar = new javax.swing.JButton();
        jtxtNCuenta = new javax.swing.JTextField();
        jtxtNombre = new javax.swing.JTextField();
        jtxtDNI = new javax.swing.JTextField();

        jLabel1.setText("AÑADIR TITULAR");

        jLabel2.setText("nºCuenta");

        jLabel3.setText("nombre nuevo cliente");

        jLabel4.setText("dni nuevo cliente");

        jbtnRealizar.setText("Realizar");
        jbtnRealizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRealizarActionPerformed(evt);
            }
        });

        jtxtNCuenta.setText("20984002161000015263");

        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(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtxtDNI)
                    .addComponent(jtxtNombre)
                    .addComponent(jtxtNCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(107, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(166, Short.MAX_VALUE)
                .addComponent(jbtnRealizar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(152, 152, 152))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtNCuenta, 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(jLabel3)
                    .addComponent(jtxtNombre, 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(jtxtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50)
                .addComponent(jbtnRealizar)
                .addContainerGap(79, Short.MAX_VALUE))
        );

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

    private void jbtnRealizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRealizarActionPerformed
        String dni=jtxtDNI.getText();
        String nom=jtxtNombre.getText();
        String cuenta=jtxtNCuenta.getText();
        Cliente cli=new Cliente();
        Producto p=new Producto();
        p=Auxsuc.get_Producto(cuenta);
        ControladoraBD BD=new ControladoraBD();
        cli.setNombreCliente(nom);
        cli.setDni(dni);

        if(p!=null){
            if(Auxsuc.get_Cliente(nom)==null){
                  Auxsuc.getMCliente().add(cli);
                  if(Auxsuc.get_Producto(cuenta).getCliente(dni)==null){
                        Auxsuc.get_Cliente(nom).insertaProductoenCliente(p);
                        p.insertaTitularenProducto(cli);
                      //  cli.insertaProductoenCliente(p);
                        BD.actualizar(p);
                        jtxtDNI.setText("Titular añadido.");
                        }
                  else
                        jtxtDNI.setText("Ya insertado");
            }
            else{
                 if(Auxsuc.get_Producto(cuenta).getCliente(dni)==null){
                    Auxsuc.get_Cliente(nom).insertaProductoenCliente(p);
                    p.insertaTitularenProducto(cli);
                    //cli.insertaProductoenCliente(p);
                    BD.actualizar(cli);
                    jtxtDNI.setText("Titular añadido.");
                 }
                 else
                    jtxtDNI.setText("Ya insertado");
            }


        }
        else
            jtxtDNI.setText("El producto no existe");


}//GEN-LAST:event_jbtnRealizarActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbtnRealizar;
    private javax.swing.JTextField jtxtDNI;
    private javax.swing.JTextField jtxtNCuenta;
    private javax.swing.JTextField jtxtNombre;
    // End of variables declaration//GEN-END:variables

}
