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

/*
 * jfrmArqInicial.java
 *
 * Created on 04-ene-2010, 20:26:27
 */

package interfaz;

import daial.Arqueo;
import daial.Caja;
import daial.Empleado;
import daial.Sucursal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Alberto
 */
public class jfrmArqInicial extends javax.swing.JFrame {
    private Sucursal s = new Sucursal();
    jfrmPrincipal p;
    private Caja caja_principal;
    private Caja caja_central;

    public jfrmArqInicial(Sucursal suc) {
        initComponents();
        this.setLocationRelativeTo(null);
        s = suc;


        ArrayList<Caja> cajas = s.getMCajas();
        DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
        Iterator<Caja> it=cajas.iterator();
        while (it.hasNext()) {
           Caja c = (Caja) it.next();
           if(c.getNombre().compareTo("Caja Central")!=0){
                dcbm.addElement(c);
           }else{
                caja_central=c;
           }
        }
        jCbBx_caja.setModel(dcbm);

        caja_principal=(Caja) jCbBx_caja.getSelectedItem();
        if (caja_principal.getArqueo().isEmpty()){
            jTxt_saldo.setText("500000");
        }else{
            jTxt_saldo.setText(String.valueOf(caja_principal.getArqueo().get(caja_principal.getArqueo().size()-1).getSaldo_final()));
        }

    }

    /** 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();
        jTxt_saldo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jBtn_aceptar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jTxt_user = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTxt_pass = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jCbBx_caja = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Arqueo Inicial");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Arqueo Inicial");

        jTxt_saldo.setEditable(false);
        jTxt_saldo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel3.setText("Saldo inicial: ");

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

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Usuario:");

        jLabel4.setText("Contraseña:");

        jLabel5.setText("Selecciona caja:");

        jCbBx_caja.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCbBx_cajaMouseClicked(evt);
            }
        });
        jCbBx_caja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbBx_cajaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTxt_user, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTxt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)))
                .addGap(85, 85, 85))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(jCbBx_caja, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jCbBx_caja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
        );

        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(123, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(147, 147, 147))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(28, 28, 28)
                        .addComponent(jTxt_saldo, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(78, 78, 78))))
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(229, Short.MAX_VALUE)
                .addComponent(jBtn_aceptar)
                .addGap(170, 170, 170))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(12, 12, 12)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxt_saldo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jBtn_aceptar)
                .addGap(19, 19, 19))
        );

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

    private void jBtn_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_aceptarActionPerformed
        Date dia = new Date();
        Empleado trabajador = s.buscarEmpleado(jTxt_user.getText(),jTxt_pass.getText());
        
        if(trabajador == null)
        {
            JOptionPane dialogo2 = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "Usuario y/o contraseña incorrectos", "Aviso", JOptionPane.INFORMATION_MESSAGE);
        }else
        {
            caja_principal=(Caja) jCbBx_caja.getSelectedItem();
            Arqueo arq = new Arqueo();
            arq.setEmpleado(trabajador);

            if (caja_principal.getArqueo().isEmpty()){
                arq.setSaldo_inicial(500000);
                caja_principal.setSaldo(500000);
            }
            else{
                //Buscar el saldo del arqueo anterior
                arq.setSaldo_inicial(caja_principal.getArqueo().get(caja_principal.getArqueo().size()-1).getSaldo_final());
                caja_principal.setSaldo(caja_principal.getArqueo().get(caja_principal.getArqueo().size()-1).getSaldo_final());
            }
            arq.setFecha(dia);

            caja_principal.anadirArqueo(arq);

            JOptionPane dialogo = new JOptionPane();
            JOptionPane.showMessageDialog(rootPane, "Arqueo inicial realizado con éxito", "Aviso", JOptionPane.INFORMATION_MESSAGE);
            p = new jfrmPrincipal(s,caja_principal,caja_central);
            p.setEnabled(true);
            p.setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_jBtn_aceptarActionPerformed

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

    private void jCbBx_cajaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCbBx_cajaMouseClicked
        caja_principal=(Caja) jCbBx_caja.getSelectedItem();
        if (caja_principal.getArqueo().isEmpty()){
            jTxt_saldo.setText("500000");
        }else{
            jTxt_saldo.setText(String.valueOf(caja_principal.getArqueo().get(caja_principal.getArqueo().size()-1).getSaldo_final()));
        }
    }//GEN-LAST:event_jCbBx_cajaMouseClicked

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtn_aceptar;
    private javax.swing.JComboBox jCbBx_caja;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTxt_pass;
    private javax.swing.JTextField jTxt_saldo;
    private javax.swing.JTextField jTxt_user;
    // End of variables declaration//GEN-END:variables

}
