/*
 * JCadastroCliente.java
 *
 * Created on 10 de Novembro de 2006, 19:51
 */

package veterinaria;

import java.io.IOException;
import java.sql.SQLException;

/**
 *
 * @author  203620
 */
public class JCadastroCliente extends javax.swing.JFrame {
    
    /** Creates new form JCadastroCliente */
    public JCadastroCliente() {
        initComponents();
        try {
            SimpleDataSource.init("bd.properties");
            try {
                bd=new ClienteDAO(SimpleDataSource.getConnection());
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        btnOk.setVisible(false);
        btnCancelar.setVisible(false);
        btnExcluir.setEnabled(false);
        btnAlterar.setEnabled(false);
        txtNome.setEnabled(false);
        txtCPF.setEnabled(false);
        txtTelefone.setEnabled(false);
        txtEndereco.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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btnIncluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnPesquisar = new javax.swing.JButton();
        txtNome = new javax.swing.JTextField();
        txtTelefone = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        txtCPF = new javax.swing.JTextField();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        btnIncluir.setText("Incluir");
        btnIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIncluirActionPerformed(evt);
            }
        });

        getContentPane().add(btnIncluir);
        btnIncluir.setBounds(20, 240, 63, 23);

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

        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(210, 270, 90, 23);

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        getContentPane().add(btnOk);
        btnOk.setBounds(120, 270, 80, 23);

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        getContentPane().add(btnExcluir);
        btnExcluir.setBounds(120, 240, 65, 23);

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        getContentPane().add(btnAlterar);
        btnAlterar.setBounds(230, 240, 67, 23);

        btnPesquisar.setText("Pesquisar");
        getContentPane().add(btnPesquisar);
        btnPesquisar.setBounds(315, 240, 80, 23);

        getContentPane().add(txtNome);
        txtNome.setBounds(200, 30, 80, 20);

        getContentPane().add(txtTelefone);
        txtTelefone.setBounds(200, 60, 80, 20);

        getContentPane().add(txtEndereco);
        txtEndereco.setBounds(210, 100, 6, 20);

        getContentPane().add(txtCPF);
        txtCPF.setBounds(40, 80, 6, 20);

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

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        limparCampos();
        if(umcliente != null) {
            bd.excluirCliente(umcliente);
            umcliente = null;
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        controlarBotoes(true);
        limparCampos();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        controlarBotoes(true);
        if (operacaoAtual=="INC") {
            umcliente = new Cliente();
            umcliente.setNome(txtNome.getText());
            umcliente.setCPF(txtCPF.getText());
            umcliente.setTelefone(txtTelefone.getText());
            umcliente.setEndereco(txtEndereco.getText());
            if(bd.incluirCliente(umcliente)) {
                btnExcluir.setEnabled(true);
                btnAlterar.setEnabled(true);
            }
        }
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        controlarBotoes(false);
        operacaoAtual="ALT";
        controlarCampos(true);
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIncluirActionPerformed
        controlarBotoes(false);
        operacaoAtual="INC";
        controlarCampos(true);
    }//GEN-LAST:event_btnIncluirActionPerformed
    
    private void controlarBotoes(boolean flag) {
        btnIncluir.setVisible(flag);
        btnExcluir.setVisible(flag);
        btnAlterar.setVisible(flag);
        btnPesquisar.setVisible(flag);
        btnOk.setVisible(! flag);
        btnCancelar.setVisible(! flag);
    }
    
    public void controlarCampos(boolean flag){
        txtNome.setEnabled(flag);
        txtCPF.setEnabled(flag);
        txtTelefone.setEnabled(flag);
        txtEndereco.setEnabled(flag);
    }
    
    private void limparCampos() {
        txtNome.setText("");
        txtCPF.setText("");
        txtTelefone.setText("");
        txtEndereco.setText("");
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JCadastroCliente().setVisible(true);
            }
        });
    }

    String operacaoAtual;
    ClienteDAO bd;
    Cliente umcliente;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnIncluir;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JTextField txtCPF;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtTelefone;
    // End of variables declaration//GEN-END:variables
    
}
