/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfacePharmacie;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LENOVO
 */
public class EditPharmacie extends javax.swing.JFrame {

    /**
     * Creates new form EditPharmacie
     */
    public EditPharmacie() {
        initComponents();
        this.setLocationRelativeTo(this); 
        this.setResizable(false);
        raffraichir();
        
        
    }

    /**
     * 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() {

        txtRecherche = new javax.swing.JTextField();
        btnRecherche = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnEnregistrerPharmacie = new javax.swing.JButton();
        btnSuupprimerPharmacie = new javax.swing.JButton();
        btnEffacerChampPharmacie = new javax.swing.JButton();
        btnUpdatePharmacie = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbListePharmacie = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtCodePharmacie0 = new javax.swing.JTextField();
        txtNomPharmacie0 = new javax.swing.JTextField();
        txtTelephonePharmacie0 = new javax.swing.JTextField();
        txtPasswordPharmacie0 = new javax.swing.JPasswordField();
        txtPasswordPharmacie10 = new javax.swing.JPasswordField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAdressePharmacie0 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(txtRecherche, new org.netbeans.lib.awtextra.AbsoluteConstraints(147, 11, 149, 30));

        btnRecherche.setText("Search");
        btnRecherche.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRechercheActionPerformed(evt);
            }
        });
        getContentPane().add(btnRecherche, new org.netbeans.lib.awtextra.AbsoluteConstraints(314, 11, -1, 30));

        jLabel1.setText("Nom");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 44, -1));

        jLabel2.setText("Code");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 44, -1));

        jLabel3.setText("Mot de passe");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 74, -1));

        jLabel4.setText("Re-taper le mot de passe");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 204, 127, -1));

        jLabel5.setText("Téléphone");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 262, -1, -1));

        btnEnregistrerPharmacie.setText("Save");
        btnEnregistrerPharmacie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnregistrerPharmacieActionPerformed(evt);
            }
        });
        getContentPane().add(btnEnregistrerPharmacie, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 430, 60, 30));

        btnSuupprimerPharmacie.setText("Supprimer");
        btnSuupprimerPharmacie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuupprimerPharmacieActionPerformed(evt);
            }
        });
        getContentPane().add(btnSuupprimerPharmacie, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 430, -1, -1));

        btnEffacerChampPharmacie.setText("Effacer");
        btnEffacerChampPharmacie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEffacerChampPharmacieActionPerformed(evt);
            }
        });
        getContentPane().add(btnEffacerChampPharmacie, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 470, -1, -1));

        btnUpdatePharmacie.setText("Mise à jour");
        btnUpdatePharmacie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdatePharmacieActionPerformed(evt);
            }
        });
        getContentPane().add(btnUpdatePharmacie, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 470, -1, -1));

        jtbListePharmacie.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jtbListePharmacie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbListePharmacieMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbListePharmacie);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(451, 106, 431, 374));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ACCUEIL.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(767, 11, 60, 50));

        jLabel6.setText("Adresse");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 331, -1, -1));
        getContentPane().add(txtCodePharmacie0, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, 130, 30));
        getContentPane().add(txtNomPharmacie0, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 120, 130, 30));
        getContentPane().add(txtTelephonePharmacie0, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 250, 130, 30));
        getContentPane().add(txtPasswordPharmacie0, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 160, 170, 30));
        getContentPane().add(txtPasswordPharmacie10, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 200, 170, 30));

        txtAdressePharmacie0.setColumns(20);
        txtAdressePharmacie0.setRows(5);
        jScrollPane2.setViewportView(txtAdressePharmacie0);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 300, 260, 110));

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

    private void btnEffacerChampPharmacieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEffacerChampPharmacieActionPerformed
        viderChampsPharmacie();
    }//GEN-LAST:event_btnEffacerChampPharmacieActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        new Administrateur().setVisible(true);
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEnregistrerPharmacieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnregistrerPharmacieActionPerformed
        
        String code = txtCodePharmacie0.getText();
        String nom = txtNomPharmacie0.getText();
        String password = txtPasswordPharmacie0.getText();
        String password1 = txtPasswordPharmacie10.getText();
        String telephone = txtTelephonePharmacie0.getText();
        String adresse = txtAdressePharmacie0.getText();
        
        if (code.isEmpty() || nom.isEmpty() || password.isEmpty() || password1.isEmpty() ||telephone.isEmpty() || adresse.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Veuillez remplir les champs");
            return;
        }
        if (password.equals(password1)==false){
            JOptionPane.showMessageDialog(this, "Mot de passe pas conforme");
            return;
        }
        BDPharmacie bd = new BDPharmacie();
        String resultat = bd.checkCodePharmacie(code);
        
        if (resultat !=null){
            JOptionPane.showMessageDialog(this, "Code Identifiant déjà attribué, veuillez choisir un autre!");
            return;
        }
        
        boolean result = bd.savePharmacie(code, nom, adresse, telephone, password);
        
        if (result){
            JOptionPane.showMessageDialog(this, "Enregistrement réussi");
            viderChampsPharmacie();
            rechercherPharmacie();
            
        } else {
            JOptionPane.showMessageDialog(this, "Erreur enregistrement");
        }
    }//GEN-LAST:event_btnEnregistrerPharmacieActionPerformed

    String idPharma;
    private void btnSuupprimerPharmacieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuupprimerPharmacieActionPerformed
        try {
            BDPharmacie bd = new BDPharmacie();
            Connection connex = bd.connectToBD();
            String sql = "DELETE FROM pharmacie WHERE(id_pharmacie = ?)";
            PreparedStatement stm = connex.prepareStatement(sql);
            int id = Integer.parseInt(idPharma);
            stm.setInt(1, id);
            
            int choix = JOptionPane.showConfirmDialog(this, "Voulez-vous supprimer cette pharmacie ?" , "Suppression", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
             
            if (choix==JOptionPane.YES_OPTION) {
                stm.executeUpdate();
                viderChampsPharmacie();
                JOptionPane.showMessageDialog(this, "Pharmacie supprimée avec succès");
                raffraichir();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnSuupprimerPharmacieActionPerformed

    private void jtbListePharmacieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbListePharmacieMouseClicked
        int ligne = jtbListePharmacie.getSelectedRow();
        idPharma = jtbListePharmacie.getValueAt(ligne, 0).toString();
        String code = jtbListePharmacie.getValueAt(ligne, 1).toString();
        String nom = jtbListePharmacie.getValueAt(ligne, 2).toString();
        String telephone = jtbListePharmacie.getValueAt(ligne, 3).toString();
        String adresse = jtbListePharmacie.getValueAt(ligne, 4).toString();
        
        txtCodePharmacie0.setText(code);
        txtNomPharmacie0.setText(nom);
        txtTelephonePharmacie0.setText(telephone);
        txtAdressePharmacie0.setText(adresse);
               
    }//GEN-LAST:event_jtbListePharmacieMouseClicked

    private void btnUpdatePharmacieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdatePharmacieActionPerformed
        String code = txtCodePharmacie0.getText().toString();
        String nom = txtNomPharmacie0.getText().toString();
        String telephone = txtTelephonePharmacie0.getText().toString();
        String adresse = txtAdressePharmacie0.getText().toString();
        int id = Integer.parseInt(idPharma);
        
        int p = JOptionPane.showConfirmDialog(null, "Voulez-vous modifier la pharmacie ?", "Modification Informations Pharmacie", JOptionPane.YES_NO_OPTION);
        if (p==0){
            try {
                BDPharmacie bd = new BDPharmacie();
                Connection connex = bd.connectToBD();
                String sql = "UPDATE pharmacie set nom_pharmacie = ?, telephone_pharmacie = ?, \n"
                           + "adresse_pharmacie = ? WHERE id_pharmacie = ?";
                PreparedStatement stm = connex.prepareStatement(sql);
                stm.setString(1, nom);
                stm.setString(2, telephone);
                stm.setString(3, adresse);
                stm.setInt(4, id);
                stm.executeUpdate();
                raffraichir();
                viderChampsPharmacie();
                
                JOptionPane.showMessageDialog(this, "Modification réussie");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnUpdatePharmacieActionPerformed

    private void btnRechercheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRechercheActionPerformed
        rechercherPharmacie();
    }//GEN-LAST:event_btnRechercheActionPerformed

    public void raffraichir (){
        try {
            BDPharmacie bd = new BDPharmacie();
            Connection connex = bd.connectToBD();
            String sql = "SELECT * FROM pharmacie WHERE profil IS NULL ORDER BY 3";
            PreparedStatement stm = connex.prepareStatement(sql);
            ResultSet rs = stm.executeQuery();
            
            DefaultTableModel dftm = new DefaultTableModel();
            String columns[] = {"ID", "CODE","NOM","TELEPHONE","ADRESSE"};
            dftm.setColumnIdentifiers(columns);
            
            while (rs.next()) {
                String id = rs.getString("id_pharmacie");
                String code = rs.getString("code_pharmacie");
                String nom = rs.getString("nom_pharmacie");
                String telephone = rs.getString("telephone_pharmacie");
                String adresse = rs.getString("adresse_pharmacie");
                
                Vector v = new Vector();
                v.add(id);
                v.add(code);
                v.add(nom);
                v.add(telephone);
                v.add(adresse);
                dftm.addRow(v);
            }
              jtbListePharmacie.setModel(dftm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void viderChampsPharmacie (){
        txtCodePharmacie0.setText("");
        txtNomPharmacie0.setText("");
        txtPasswordPharmacie0.setText("");
        txtPasswordPharmacie10.setText("");
        txtTelephonePharmacie0.setText("");
        txtAdressePharmacie0.setText("");
    }
    
    public void rechercherPharmacie (){
        
        String nomSearch = "%"+txtRecherche.getText()+"%";
        
        try {
            BDPharmacie bd = new BDPharmacie();
            Connection connex = bd.connectToBD();
            String sql = "SELECT * FROM pharmacie WHERE profil IS NULL AND nom_pharmacie LIKE ? ";
            PreparedStatement stm = connex.prepareStatement(sql);
            stm.setString(1, nomSearch);
            
            ResultSet rs = stm.executeQuery();
            DefaultTableModel dftm = new DefaultTableModel();
            String columns[] = {"ID","CODE","NOM","TELEPHONE","Adresse"};
            dftm.setColumnIdentifiers(columns);
            
            while (rs.next()) {
                String id = rs.getString("id_pharmacie");
                String code = rs.getString("code_pharmacie");
                String nom = rs.getString("nom_pharmacie");
                String telephone = rs.getString("telephone_pharmacie");
                String adresse = rs.getString("adresse_pharmacie");
                
                Vector v = new Vector();
                v.add(id);
                v.add(code);
                v.add(nom);
                v.add(telephone);
                v.add(adresse);
                dftm.addRow(v);
            }
            jtbListePharmacie.setModel(dftm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditPharmacie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditPharmacie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditPharmacie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditPharmacie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditPharmacie().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEffacerChampPharmacie;
    private javax.swing.JButton btnEnregistrerPharmacie;
    private javax.swing.JButton btnRecherche;
    private javax.swing.JButton btnSuupprimerPharmacie;
    private javax.swing.JButton btnUpdatePharmacie;
    private javax.swing.JButton jButton1;
    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.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jtbListePharmacie;
    private javax.swing.JTextArea txtAdressePharmacie0;
    private javax.swing.JTextField txtCodePharmacie0;
    private javax.swing.JTextField txtNomPharmacie0;
    private javax.swing.JPasswordField txtPasswordPharmacie0;
    private javax.swing.JPasswordField txtPasswordPharmacie10;
    private javax.swing.JTextField txtRecherche;
    private javax.swing.JTextField txtTelephonePharmacie0;
    // End of variables declaration//GEN-END:variables
}
