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

/*
 * jfrmRemesaCargos.java
 *
 * Created on 13-nov-2009, 12:58:03
 */

package interfaz;


import daial.Sucursal;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author acc_local
 */
public class jfrmRemesa extends javax.swing.JFrame {
    Sucursal auxsuc = new Sucursal();
    jfrmPrincipal p;
    boolean importado;
    /** Creates new form jfrmRemesaCargos */
    public jfrmRemesa(Sucursal suc,jfrmPrincipal principal) {
        auxsuc=suc;
        p = new jfrmPrincipal(suc);
        p = principal;
        initComponents();
        this.setLocationRelativeTo(null);
        importado = 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtPathFicheroRemesa = new javax.swing.JTextField();
        jbtnImportar = new javax.swing.JButton();
        jbtnValidar = new javax.swing.JButton();
        jBtnAtras = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaVerRemesa = new javax.swing.JTextArea();
        jCbBx_abonosCargos = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cargar remesas");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("TRATAMIENTO DE REMESAS");

        jLabel2.setText("Fichero a tratar");

        jtxtPathFicheroRemesa.setText("c:\\remesa.txt");
        jtxtPathFicheroRemesa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtPathFicheroRemesaActionPerformed(evt);
            }
        });

        jbtnImportar.setText("Importar");
        jbtnImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnImportarActionPerformed(evt);
            }
        });

        jbtnValidar.setText("Validar");
        jbtnValidar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnValidarActionPerformed(evt);
            }
        });

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

        jtxaVerRemesa.setColumns(20);
        jtxaVerRemesa.setRows(5);
        jScrollPane1.setViewportView(jtxaVerRemesa);

        jCbBx_abonosCargos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CARGOS", "ABONOS" }));
        jCbBx_abonosCargos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbBx_abonosCargosActionPerformed(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(97, 97, 97)
                .addComponent(jbtnValidar)
                .addGap(73, 73, 73)
                .addComponent(jBtnAtras)
                .addContainerGap(104, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jtxtPathFicheroRemesa, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCbBx_abonosCargos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(163, Short.MAX_VALUE)
                .addComponent(jbtnImportar)
                .addGap(160, 160, 160))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtPathFicheroRemesa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCbBx_abonosCargos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtnImportar)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnValidar)
                    .addComponent(jBtnAtras))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleDescription("Cargar remesas");

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

    private void jbtnImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnImportarActionPerformed
        if(!jtxtPathFicheroRemesa.getText().isEmpty()){
            String seleccion=jCbBx_abonosCargos.getSelectedItem().toString();
            if(seleccion.compareTo("ABONOS")==0){
                String textoFichero = auxsuc.verRemesaAbonos(jtxtPathFicheroRemesa.getText());
                if(textoFichero.isEmpty()){
                    JOptionPane dialogo = new JOptionPane();
                    JOptionPane.showMessageDialog(rootPane, "Fichero inexistente o erroneo", "Error", JOptionPane.ERROR_MESSAGE);
                }else{
                    jtxaVerRemesa.setText(textoFichero);
                    importado=true;
                }
            }else{
                String textoFichero = auxsuc.verRemesaCargos(jtxtPathFicheroRemesa.getText());
                if(textoFichero.isEmpty()){
                    JOptionPane dialogo = new JOptionPane();
                    JOptionPane.showMessageDialog(rootPane, "Fichero inexistente o erroneo", "Error", JOptionPane.ERROR_MESSAGE);
                }else{
                    jtxaVerRemesa.setText(textoFichero);
                    importado=true;
                }
            }
        }
}//GEN-LAST:event_jbtnImportarActionPerformed

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

    private void jbtnValidarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnValidarActionPerformed
        try {
            if(importado){
                String textoFichero=jtxtPathFicheroRemesa.getText();
                String seleccion=jCbBx_abonosCargos.getSelectedItem().toString();
                if(seleccion.compareTo("ABONOS")==0){
                    auxsuc.incorporarRemesaAbonos(textoFichero);
                }else{
                    auxsuc.incorporarRemesaCargos(textoFichero);
                }
                p.actualizar_info(); // Actualiza el saldo en el formulario principal
                JOptionPane dialogo = new JOptionPane();
                JOptionPane.showMessageDialog(rootPane, "Remesa incluida y saldos modificados con éxito", "Aviso", JOptionPane.INFORMATION_MESSAGE);    
                p.setEnabled(true);
                this.dispose();

            }else{
                JOptionPane dialogo = new JOptionPane();
                JOptionPane.showMessageDialog(rootPane, "Para validar se necesita importar un fichero", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (ParseException ex) {
            Logger.getLogger(jfrmRemesa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtnValidarActionPerformed

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

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

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAtras;
    private javax.swing.JComboBox jCbBx_abonosCargos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnImportar;
    private javax.swing.JButton jbtnValidar;
    private javax.swing.JTextArea jtxaVerRemesa;
    private javax.swing.JTextField jtxtPathFicheroRemesa;
    // End of variables declaration//GEN-END:variables

}
