package telas;

import atributos.Cliente;
import atributos.Lembrete;
import funcoes.ClienteDAO;
import funcoes.FuncoesDiversas;
import static funcoes.FuncoesDiversas.ConverterData;
import static funcoes.FuncoesDiversas.FormataData;
import funcoes.LembreteDAO;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import static telas.DetalharCliente.GetIdLembrete;
import static telas.ExibeCliente.GetIndice;

public class DetalharLembrete extends javax.swing.JFrame {

    private DetalharCliente detalharCliente;
    private PreparedStatement pst;
    private int codCli;

    /**
     * Creates new form DetalheLembrete
     */
    public DetalharLembrete() {
        initComponents();
        CarregaLembrete();
        DesabilitarCampos();
        DadosEmpresa();
    }

    public DetalharLembrete(DetalharCliente detalharCliente) {
        this();
        this.detalharCliente = detalharCliente;
        codCli = GetIndice(); //GetIndice está no form exibecliente, retorna o id do cliente selecionado na tabela
        DadosEmpresa();
    }

    private void DesabilitarCampos() {

        jBtnAlterarLembrete.setVisible(false);
        txtDataLembrete.setVisible(false);
        txtDescricaoLembrete.setEnabled(false);
        txtHoraLembrete.setVisible(false);
        jBtnCancel.setVisible(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() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtHoraLembrete = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabCodigo = new javax.swing.JLabel();
        jLabEmpresa = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricaoLembrete = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        txtDataLembrete = new com.toedter.calendar.JDateChooser();
        jLbDataLembrete = new javax.swing.JLabel();
        jBtnExcluir = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jBtnAlterarLembrete = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLbHoraLembrete = new javax.swing.JLabel();
        jBtnCancel = new javax.swing.JButton();
        jBtnEditarLembrete = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lembrete");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(223, 237, 253));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Código:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, -1, 20));

        try {
            txtHoraLembrete.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtHoraLembrete.setText("00:00");
        jPanel1.add(txtHoraLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 180, 42, -1));

        jLabel1.setFont(new java.awt.Font("Raavi", 1, 18)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/lembrete.png"))); // NOI18N
        jLabel1.setText("Lembrete:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 29, -1, -1));

        jLabCodigo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabCodigo.setText("codigo");
        jPanel1.add(jLabCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 120, -1, 20));

        jLabEmpresa.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabEmpresa.setText("empresa");
        jPanel1.add(jLabEmpresa, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 150, 176, 20));

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

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 260, 372, 160));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Descrição:");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 230, -1, -1));
        jPanel1.add(txtDataLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 180, 140, -1));

        jLbDataLembrete.setText("Exibe Data");
        jPanel1.add(jLbDataLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(81, 132, 142, 23));

        jBtnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/excluir.png"))); // NOI18N
        jBtnExcluir.setText("Excluir");
        jBtnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnExcluirActionPerformed(evt);
            }
        });
        jPanel1.add(jBtnExcluir, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 440, -1, -1));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/sair.gif"))); // NOI18N
        jButton2.setText("Sair");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 500, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Empresa:");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, -1, 20));

        jBtnAlterarLembrete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/ok.png"))); // NOI18N
        jBtnAlterarLembrete.setText("Salvar");
        jBtnAlterarLembrete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAlterarLembreteActionPerformed(evt);
            }
        });
        jPanel1.add(jBtnAlterarLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 440, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Data:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 180, -1, 20));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Hora:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 180, -1, 20));

        jLbHoraLembrete.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLbHoraLembrete.setText("Exibe Hora");
        jPanel1.add(jLbHoraLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 180, 60, 20));

        jBtnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/cancelar.png"))); // NOI18N
        jBtnCancel.setText("Cancelar");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });
        jPanel1.add(jBtnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 440, -1, -1));

        jBtnEditarLembrete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/editar.png"))); // NOI18N
        jBtnEditarLembrete.setText("Editar");
        jBtnEditarLembrete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEditarLembreteActionPerformed(evt);
            }
        });
        jPanel1.add(jBtnEditarLembrete, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 440, -1, -1));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/leiaute/img2.png"))); // NOI18N
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 670, 100));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 672, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    private void CarregaLembrete() {

        ArrayList<Lembrete> lembretes = new ArrayList<Lembrete>();
        lembretes = LembreteDAO.CarregaLembrete(GetIdLembrete());

        for (Lembrete lem : lembretes) {

            jLbDataLembrete.setText(String.valueOf(lem.getDataLembrete()));
            jLbHoraLembrete.setText(String.valueOf(lem.getHora()));
            txtDescricaoLembrete.setText(lem.getDescricao());
        }
    }

    private void DadosEmpresa() {

        ArrayList<Cliente> cliente = new ArrayList<Cliente>();
        cliente = ClienteDAO.CarregaNomeCliente(codCli);

        for (Cliente cli : cliente) {

            jLabCodigo.setText(String.valueOf(cli.getId()));
            jLabEmpresa.setText(cli.getEmpresa());
        }
    }

    private void jBtnEditarLembreteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEditarLembreteActionPerformed

        txtDataLembrete.requestFocus();
        txtHoraLembrete.setText(jLbHoraLembrete.getText());
        Date d = ConverterData(jLbDataLembrete.getText());
        txtDataLembrete.setDate(d);
        txtDataLembrete.setVisible(true);
        txtHoraLembrete.setVisible(true);
        jLbDataLembrete.setVisible(false);
        jLbHoraLembrete.setVisible(false);
        jBtnAlterarLembrete.setVisible(true);
        jBtnEditarLembrete.setVisible(false);
        txtDescricaoLembrete.setEnabled(true);
        jBtnCancel.setVisible(true);

    }//GEN-LAST:event_jBtnEditarLembreteActionPerformed

    private void jBtnAlterarLembreteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAlterarLembreteActionPerformed
        if (VerificaCampos() == true) {
            try {
                Lembrete lembrete = new Lembrete();

                lembrete.setDataLembrete(FormataData(txtDataLembrete.getDate()));
                lembrete.setHora(FuncoesDiversas.ConverterHora(txtHoraLembrete.getText()));
                lembrete.setDescricao(txtDescricaoLembrete.getText());
                lembrete.setCodCliente(ExibeCliente.GetIndice());
                LembreteDAO.UpdateLembrete(lembrete, GetIdLembrete());

                jLbHoraLembrete.setVisible(true);
                jLbDataLembrete.setVisible(true);
                CarregaLembrete();
                DesabilitarCampos();
                jBtnEditarLembrete.setVisible(true);

                this.detalharCliente.TabelaLembrete2(codCli);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Campo data Inválido!");
            }
        }
    }//GEN-LAST:event_jBtnAlterarLembreteActionPerformed


    private void jBtnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnExcluirActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Deseja excluir o registro?", "Confirmar Exclusão", JOptionPane.YES_NO_OPTION) == 0) {
            LembreteDAO.ExcluirLembrete(GetIdLembrete());
            this.detalharCliente.TabelaLembrete2(codCli);
            this.dispose();
        }
    }//GEN-LAST:event_jBtnExcluirActionPerformed

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Cancelar alteração?", "Confirmar Cancelamento", JOptionPane.YES_NO_OPTION) == 0) {
            jLbHoraLembrete.setVisible(true);
            jLbDataLembrete.setVisible(true);
            CarregaLembrete();
            DesabilitarCampos();
            jBtnEditarLembrete.setVisible(true);
        }
    }//GEN-LAST:event_jBtnCancelActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        //habilita a tela principal
        this.detalharCliente.setEnabled(true);
        //coloca a tela principal na frente
        this.detalharCliente.toFront();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        this.dispose();
        this.detalharCliente.setEnabled(true);
        this.detalharCliente.toFront();
    }//GEN-LAST:event_formWindowClosed

    private boolean VerificaCampos() {

        boolean valida = true;

        if (txtDataLembrete.getDate() == null) {
            JOptionPane.showMessageDialog(null, "Campo(s) vazio(s)!");
            valida = false;
            return valida;
        }

        if (txtHoraLembrete.getText().trim().equals(":")) {
            JOptionPane.showMessageDialog(null, "Campo(s) vazio(s)!");
            valida = false;
            return valida;
        }
        
        if (txtDescricaoLembrete.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Campo(s) vazio(s)!");
            valida = false;
            return valida;
        }

        return valida;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAlterarLembrete;
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnEditarLembrete;
    private javax.swing.JButton jBtnExcluir;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabCodigo;
    private javax.swing.JLabel jLabEmpresa;
    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.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLbDataLembrete;
    private javax.swing.JLabel jLbHoraLembrete;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private com.toedter.calendar.JDateChooser txtDataLembrete;
    private javax.swing.JTextArea txtDescricaoLembrete;
    private javax.swing.JFormattedTextField txtHoraLembrete;
    // End of variables declaration//GEN-END:variables
}
