/*
 * 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 static interfacePharmacie.BDPharmacie.conex;
import static interfacePharmacie.BDPharmacie.connectToBD;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author awn27
 */
public class InterfaceAjoutMedicament extends javax.swing.JFrame {

    /**
     * Creates new form InterfaceAjoutMedicament
     */
    public InterfaceAjoutMedicament() {
        initComponents();
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width/2 - getWidth()/2, 
        size.height/2 - getHeight()/2);
        dateSysteme();
        lblNomPharmacie.setText(String.valueOf(InterfaceLogin.nomPharmacie).toString());
        int idP = InterfaceLogin.idPharmacie;
        

    }
    
    public void dateSysteme (){
        
        Calendar cal =new GregorianCalendar();
        int mois = cal.get(Calendar.MONTH);
        int annee = cal.get(Calendar.YEAR);
        int jour = cal.get(Calendar.DAY_OF_MONTH);
        
        //jLabel23.setText(jour+"/"+(mois+1)+"/"+annee);
        
        //Heure
      
        int seconde = cal.get(Calendar.SECOND);
        int minute = cal.get(Calendar.MINUTE);
        int heure = cal.get(Calendar.HOUR);
        
        //jLabel22.setText(heure+"H : "+(minute)+" : "+seconde);
        
       
    }

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

        jPanel1 = new javax.swing.JPanel();
        txtRechercheMedicament = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtCodeMedicament = new javax.swing.JTextField();
        txtContenanceMedicament = new javax.swing.JTextField();
        txtPosologieMedicament0 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        lblImageMedicament = new javax.swing.JLabel();
        btnEnregistrerMedicament = new javax.swing.JButton();
        btnModifierMedicament = new javax.swing.JButton();
        btnViderChamp = new javax.swing.JButton();
        btnSupprimerMedicament = new javax.swing.JButton();
        cbxTypeMedicament = new javax.swing.JComboBox<>();
        txtFabricantMedicament = new javax.swing.JTextField();
        quantiteMedicament = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescriptionMedicament = new javax.swing.JTextArea();
        pu = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtNomMedicament = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        checkNourrisson = new javax.swing.JRadioButton();
        checkEnfant = new javax.swing.JRadioButton();
        checkAdolescent = new javax.swing.JRadioButton();
        checkAdulte = new javax.swing.JRadioButton();
        txtDate = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtPosologieMedicament = new javax.swing.JComboBox<>();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbAffichageMedicament = new javax.swing.JTable();
        jLabel12 = new javax.swing.JLabel();
        lblNomPharmacie = new javax.swing.JLabel();
        txtRechercheInformation = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        lblChoixFichier = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Recherche", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Georgia", 1, 16))); // NOI18N
        jPanel1.setMaximumSize(new java.awt.Dimension(32707, 32707));
        jPanel1.setLayout(null);

        txtRechercheMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRechercheMedicamentActionPerformed(evt);
            }
        });
        jPanel1.add(txtRechercheMedicament);
        txtRechercheMedicament.setBounds(130, 30, 300, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Médicament :");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 20, 130, 30);

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 50, 470, 70));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Description Médicament", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N
        jPanel2.setLayout(null);

        txtCodeMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodeMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(txtCodeMedicament);
        txtCodeMedicament.setBounds(100, 100, 100, 30);
        jPanel2.add(txtContenanceMedicament);
        txtContenanceMedicament.setBounds(110, 300, 200, 30);
        jPanel2.add(txtPosologieMedicament0);
        txtPosologieMedicament0.setBounds(90, 180, 200, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Code :");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(20, 110, 80, 15);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Description :");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(360, 240, 90, 15);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Contenance :");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(10, 300, 140, 15);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Posplogie :");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(10, 340, 100, 15);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Fabricant :");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(10, 390, 110, 15);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Type :");
        jPanel2.add(jLabel8);
        jLabel8.setBounds(20, 150, 60, 15);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("Nom :");
        jPanel2.add(jLabel11);
        jLabel11.setBounds(20, 70, 60, 15);

        jDesktopPane1.setPreferredSize(new java.awt.Dimension(261, 177));

        jDesktopPane1.setLayer(lblImageMedicament, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDesktopPane1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImageMedicament, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDesktopPane1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImageMedicament, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.add(jDesktopPane1);
        jDesktopPane1.setBounds(350, 40, 240, 150);

        btnEnregistrerMedicament.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Save-icon.png"))); // NOI18N
        btnEnregistrerMedicament.setText("Enregistrer");
        btnEnregistrerMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnregistrerMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(btnEnregistrerMedicament);
        btnEnregistrerMedicament.setBounds(350, 400, 110, 40);

        btnModifierMedicament.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/update icon.png"))); // NOI18N
        btnModifierMedicament.setText("Mise à jour");
        btnModifierMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifierMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(btnModifierMedicament);
        btnModifierMedicament.setBounds(490, 400, 110, 40);

        btnViderChamp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/erase-128.png"))); // NOI18N
        btnViderChamp.setText("Effacer");
        btnViderChamp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViderChampActionPerformed(evt);
            }
        });
        jPanel2.add(btnViderChamp);
        btnViderChamp.setBounds(350, 460, 110, 40);

        btnSupprimerMedicament.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/delete_16x16.gif"))); // NOI18N
        btnSupprimerMedicament.setText("Supprimer");
        btnSupprimerMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSupprimerMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(btnSupprimerMedicament);
        btnSupprimerMedicament.setBounds(490, 460, 110, 40);

        cbxTypeMedicament.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Sirop", "Poudre", "Comprimé", "Suppositoire", "Cosmétique" }));
        cbxTypeMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxTypeMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(cbxTypeMedicament);
        cbxTypeMedicament.setBounds(100, 140, 100, 30);
        jPanel2.add(txtFabricantMedicament);
        txtFabricantMedicament.setBounds(110, 380, 200, 30);
        jPanel2.add(quantiteMedicament);
        quantiteMedicament.setBounds(110, 480, 70, 30);

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

        jPanel2.add(jScrollPane1);
        jScrollPane1.setBounds(350, 270, 280, 100);

        pu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                puActionPerformed(evt);
            }
        });
        jPanel2.add(pu);
        pu.setBounds(110, 440, 70, 30);

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel14.setText("Quantité :");
        jPanel2.add(jLabel14);
        jLabel14.setBounds(20, 490, 62, 15);

        txtNomMedicament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomMedicamentActionPerformed(evt);
            }
        });
        jPanel2.add(txtNomMedicament);
        txtNomMedicament.setBounds(100, 70, 200, 20);

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel15.setText("Date expiration");
        jPanel2.add(jLabel15);
        jLabel15.setBounds(10, 530, 100, 30);

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel16.setText("CFA");
        jPanel2.add(jLabel16);
        jLabel16.setBounds(200, 440, 23, 15);

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel17.setText("Prix :");
        jPanel2.add(jLabel17);
        jLabel17.setBounds(20, 440, 60, 30);

        checkNourrisson.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        checkNourrisson.setText("Nourrisson");
        checkNourrisson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkNourrissonActionPerformed(evt);
            }
        });
        jPanel2.add(checkNourrisson);
        checkNourrisson.setBounds(100, 200, 100, 29);

        checkEnfant.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        checkEnfant.setText("Enfant");
        checkEnfant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkEnfantActionPerformed(evt);
            }
        });
        jPanel2.add(checkEnfant);
        checkEnfant.setBounds(210, 200, 67, 23);

        checkAdolescent.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        checkAdolescent.setText("Adolescent");
        checkAdolescent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkAdolescentActionPerformed(evt);
            }
        });
        jPanel2.add(checkAdolescent);
        checkAdolescent.setBounds(100, 240, 93, 23);

        checkAdulte.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        checkAdulte.setText("Adulte");
        checkAdulte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkAdulteActionPerformed(evt);
            }
        });
        jPanel2.add(checkAdulte);
        checkAdulte.setBounds(210, 240, 67, 23);
        jPanel2.add(txtDate);
        txtDate.setBounds(110, 530, 180, 30);

        jLabel9.setText("aaaa-mm-jj");
        jPanel2.add(jLabel9);
        jLabel9.setBounds(300, 540, 100, 14);

        txtPosologieMedicament.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Matin", "Soir", "Matin/Soir", "Matin/Midi/Soir", " " }));
        jPanel2.add(txtPosologieMedicament);
        txtPosologieMedicament.setBounds(110, 340, 70, 30);

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 640, 580));

        jtbAffichageMedicament.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jtbAffichageMedicament.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jtbAffichageMedicament.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {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", "Title 5", "Title 6", "Title 7", "Title 8"
            }
        ));
        jtbAffichageMedicament.setCellSelectionEnabled(true);
        jtbAffichageMedicament.setFocusTraversalPolicyProvider(true);
        jtbAffichageMedicament.setMaximumSize(new java.awt.Dimension(120, 64));
        jtbAffichageMedicament.setSelectionBackground(new java.awt.Color(255, 255, 255));
        jtbAffichageMedicament.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbAffichageMedicamentMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtbAffichageMedicament);
        jtbAffichageMedicament.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 140, 940, 610));

        jLabel12.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel12.setText("Espace Gestion Médicament");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(770, 0, 330, 50));

        lblNomPharmacie.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblNomPharmacie.setForeground(new java.awt.Color(0, 204, 0));
        lblNomPharmacie.setText("nom");
        getContentPane().add(lblNomPharmacie, new org.netbeans.lib.awtextra.AbsoluteConstraints(1110, 0, 270, 40));

        txtRechercheInformation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRechercheInformationActionPerformed(evt);
            }
        });
        txtRechercheInformation.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRechercheInformationKeyReleased(evt);
            }
        });
        getContentPane().add(txtRechercheInformation, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, 100, 30));

        jLabel10.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 680, 1670, 110));
        getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(1750, 10, -1, 890));

        jLabel19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ACCUEIL.png"))); // NOI18N
        jLabel19.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel19MouseClicked(evt);
            }
        });
        getContentPane().add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(1270, 70, 50, 50));

        jLabel20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jLabel20.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel20.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel20MouseClicked(evt);
            }
        });
        getContentPane().add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(1140, 70, 50, 40));
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 1810, 930));

        jLabel22.setText("jLabel22");
        getContentPane().add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 40, 1810, 950));

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel13.setText("Recherche par ID");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        lblChoixFichier.setText("Fichier");

        jMenuItem1.setText("Joindre photo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        lblChoixFichier.add(jMenuItem1);

        jMenuBar1.add(lblChoixFichier);

        setJMenuBar(jMenuBar1);

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

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        
        filename =f.getAbsolutePath();
        ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(lblImageMedicament.getWidth(), lblImageMedicament.getHeight(), Image.SCALE_DEFAULT));
        lblImageMedicament.setIcon(imageIcon);
      try {

            File images = new File(filename);
            FileInputStream fis = new FileInputStream (images);
            ByteArrayOutputStream bos= new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for(int readNum; (readNum=fis.read(buf))!=-1; ){

                bos.write(buf,0,readNum);
            }
            image_medoc=bos.toByteArray();
        }

        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);

        }
       
    }//GEN-LAST:event_jMenuItem1ActionPerformed

  /*  String tabColumnsMedicament[]={"ID", "Libellé","PU", "Stock"};

    //String tabColumnsProduitSelect[]={"ID", "Libellé","Quantité","Montant"};

    DefaultTableModel dftmMedicament, dftmMedicamentSelect;
      public void chargerMedicament(){
    
             BDPharmacie bd=new BDPharmacie();
             Connection connex = bd.connectToBD();
                String sql="SELECT * FROM medicament WHERE id_pharmacie = '"+InterfaceLogin.idPharmacie+"'";
        try {
            
                    PreparedStatement stm=connex.prepareStatement(sql);
                    ResultSet rs=stm.executeQuery();
                    dftmMedicament=new DefaultTableModel();
                   // dftmProduitSelect=new DefaultTableModel();
                    dftmMedicament.setColumnIdentifiers(tabColumnsMedicament); 
                    //dftmProduitSelect.setColumnIdentifiers(tabColumnsProduitSelect); 
     
        while(rs.next())
        {
          String id=rs.getString("id_produit");
          String libelle=rs.getString("libelle");
          String pu=rs.getString("pu");
          String stock=rs.getString("stock");
          Vector v=new Vector();
          v.add(id);
          v.add(libelle);
          v.add(pu);
          v.add(stock);
          dftmProduit.addRow(v);
          
        }
        
        jtbCProduit.setModel(dftmProduit);

        } catch (Exception e) {
        }

        
    }*/
    
     public void rafraichir()
    {
       
        try {
             BDPharmacie bd=new BDPharmacie();
             Connection connex = bd.connectToBD();
             String sql="SELECT * FROM medicament  WHERE id_pharmacie ='"+InterfaceLogin.idPharmacie+"' AND stock > 0  ORDER BY nom_medicament ASC";
             PreparedStatement stm = connex.prepareStatement(sql);
             ResultSet rs=stm.executeQuery();
             DefaultTableModel dftm = new DefaultTableModel();
             String columns[]={"ID", "NOM","Code","Type", "Contenance","Personne", "Expiration", "Prix","Stock", "Fabriquant",""};
             dftm.setColumnIdentifiers(columns); 
             
             while(rs.next())
             { 
                String id=rs.getString("id_medicament");
                 String nom=rs.getString("nom_medicament");
                 String code=rs.getString("code");
                 String type=rs.getString("type_medicament");
                 String contenance=rs.getString("contenance_medicament");
                 String personne=rs.getString("personne");
                 String date=rs.getString("date_expiration");
                 String prix=rs.getString("pu");
                 String stock=rs.getString("stock");
                 String fabricant=rs.getString("fabriquant_medicament");
                 String description=rs.getString("description_medicament");
                
                 Vector v=new Vector();
                 v.add(id);
                 v.add(nom);
                 v.add(code);
                 v.add(type);
                 v.add(contenance);
                 v.add(personne);
                 v.add(date);
                 v.add(prix);
                 v.add(stock);
                 v.add(fabricant);
                 v.add(description);
                 
                 dftm.addRow(v); 
                 
             }
             
             jtbAffichageMedicament.setModel(dftm); 
        
        } catch (Exception e) {
            e.printStackTrace();
        }
          
    }
     
     public void viderChampsMedicament (){
         
         txtCodeMedicament.setText("");
        txtContenanceMedicament.setText("");
        txtDescriptionMedicament.setText("");
        txtFabricantMedicament.setText("");
        txtPosologieMedicament0.setText("");
        quantiteMedicament.setText("");
        txtRechercheMedicament.setText("");
        txtNomMedicament.setText("");
        pu.setText("");
        txtDate.setText("");
        lblImageMedicament.setIcon(null);
     }
        public void rechercherMedicament () {
            
            String nomSearch="%"+txtRechercheMedicament.getText()+"%";
       
        try {
             BDPharmacie bd=new BDPharmacie();
             Connection connex= bd.connectToBD();
             String sql="SELECT * FROM medicament WHERE nom_medicament like ? AND id_pharmacie ='"+InterfaceLogin.idPharmacie+"' AND stock > 0 ";
             PreparedStatement stm=connex.prepareStatement(sql);
             
             stm.setString(1, nomSearch);
             
             
             ResultSet rs=stm.executeQuery();
             DefaultTableModel dftm=new DefaultTableModel();
             String columns[]={"ID", "NOM","Code", "Type","Personne", "Expiration", "Prix","Stock", "",""};
             dftm.setColumnIdentifiers(columns); 
             
             while(rs.next())
             { 
                 String id=rs.getString("id_medicament");
                 String nom=rs.getString("nom_medicament");
                 String code=rs.getString("code");
                 String type=rs.getString("type_medicament");
                // String contenance=rs.getString("contenance_medicament");
                 String personne=rs.getString("personne");
                 String date=rs.getString("date_expiration");
                 String prix=rs.getString("pu");
                 String stock=rs.getString("stock");
                 //String fabricant=rs.getString("fabriquant_medicament");
                // String description=rs.getString("description_medicament");
                
                 Vector v=new Vector();
                 v.add(id);
                 v.add(nom);
                 v.add(code);
                 v.add(type);
                 //v.add(contenance);
                 v.add(personne);
                 v.add(date);
                 v.add(prix);
                 v.add(stock);
                // v.add(fabricant);
               //  v.add(description);
                 
                 dftm.addRow(v); 
                 
             }
             
             jtbAffichageMedicament.setModel(dftm); 
        
        } catch (Exception e) {
            e.printStackTrace();
        }
          
        }
    private void txtRechercheMedicamentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRechercheMedicamentActionPerformed
        // TODO add your handling code here:
        rechercherMedicament();
    }//GEN-LAST:event_txtRechercheMedicamentActionPerformed

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

    private void btnViderChampActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViderChampActionPerformed
        // TODO add your handling code here:
        txtCodeMedicament.setText("");
        txtContenanceMedicament.setText("");
        txtDescriptionMedicament.setText("");
        txtFabricantMedicament.setText("");
        txtPosologieMedicament0.setText("");
        quantiteMedicament.setText("");
        txtRechercheMedicament.setText("");
        txtNomMedicament.setText("");
        pu.setText("");
        txtDate.setText("");
        lblImageMedicament.setIcon(null);

    }//GEN-LAST:event_btnViderChampActionPerformed

    private void btnEnregistrerMedicamentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnregistrerMedicamentActionPerformed
        // TODO add your handling code here:
        String nom = txtNomMedicament.getText();
        String code = txtCodeMedicament.getText();
        String type = cbxTypeMedicament.getSelectedItem().toString();
        String description = txtDescriptionMedicament.getText();
        String contenance = txtContenanceMedicament.getText();
        String posologie = txtPosologieMedicament.getSelectedItem().toString();
        String fabricant = txtFabricantMedicament.getText();
        String date = txtDate.getText();
        String prix = pu.getText();
        float price = Float.parseFloat(prix);
        String stock = quantiteMedicament.getText();
        int Stock = Integer.parseInt(stock);
        byte [] image = image_medoc;

        if (code.isEmpty() || type.isEmpty() || description.isEmpty() || contenance.isEmpty()||
            posologie.isEmpty() || fabricant.isEmpty() || date.isEmpty() || prix.isEmpty() || stock.isEmpty()){
            JOptionPane.showMessageDialog(this, "Veuillez remplir les champs");
            return;
        }

        BDPharmacie bd = new BDPharmacie();
        /*  String resultat = bd.checkCodeMedicament(code);
        if(resultat !=null){
            JOptionPane.showMessageDialog(this, "Code médicament déjà attribué, veuillez en choisir un autre");
            return;
        }
        */
        boolean result = bd.saveMedicament(nom, code, type, description, personne, contenance, posologie, fabricant, date, price, Stock, image, InterfaceLogin.idPharmacie);

        if (result) {
            JOptionPane.showMessageDialog(this, "Nouveau médicament enregistré");
            rafraichir();
            viderChampsMedicament(); //vider les
        } else {
            JOptionPane.showMessageDialog(this, "Erreur enregistrement");
        }

    }//GEN-LAST:event_btnEnregistrerMedicamentActionPerformed

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

    private void checkNourrissonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkNourrissonActionPerformed
        // TODO add your handling code here:
         personne ="Nourisson";
        checkNourrisson.setSelected(true);
        checkAdolescent.setSelected(false);
        checkAdulte.setSelected(false);
        checkEnfant.setSelected(false);
    }//GEN-LAST:event_checkNourrissonActionPerformed

    private void checkEnfantActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkEnfantActionPerformed
        // TODO add your handling code here:
        personne ="Enfant";
        checkNourrisson.setSelected(false);
        checkAdolescent.setSelected(false);
        checkAdulte.setSelected(false);
        checkEnfant.setSelected(true);
    }//GEN-LAST:event_checkEnfantActionPerformed

    private void checkAdolescentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkAdolescentActionPerformed
        // TODO add your handling code here:
        personne ="Adolescent";
        checkNourrisson.setSelected(false);
        checkAdolescent.setSelected(true);
        checkAdulte.setSelected(false);
        checkEnfant.setSelected(false);
    }//GEN-LAST:event_checkAdolescentActionPerformed

    private void checkAdulteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkAdulteActionPerformed
        // TODO add your handling code here:
        personne ="Adulte";
        checkNourrisson.setSelected(false);
        checkAdolescent.setSelected(false);
        checkAdulte.setSelected(true);
        checkEnfant.setSelected(false);
    }//GEN-LAST:event_checkAdulteActionPerformed
    
    String idMedoc;
    private void jtbAffichageMedicamentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbAffichageMedicamentMouseClicked
        // TODO add your handling code here:
        int ligne=jtbAffichageMedicament.getSelectedRow();
            idMedoc=jtbAffichageMedicament.getValueAt(ligne, 0).toString();
            String nom=jtbAffichageMedicament.getValueAt(ligne, 1).toString();
            String code=jtbAffichageMedicament.getValueAt(ligne, 2).toString();
            String type=jtbAffichageMedicament.getValueAt(ligne, 3).toString();
            String contenance = jtbAffichageMedicament.getValueAt(ligne, 4).toString();
            String personne = jtbAffichageMedicament.getValueAt(ligne, 5).toString();
            String date = jtbAffichageMedicament.getValueAt(ligne, 6).toString();
            String prix = jtbAffichageMedicament.getValueAt(ligne, 7).toString();
            String stock = jtbAffichageMedicament.getValueAt(ligne, 8).toString();
            String fabricant = jtbAffichageMedicament.getValueAt(ligne, 9).toString();
            String description = jtbAffichageMedicament.getValueAt(ligne, 10).toString();

                txtNomMedicament.setText(nom);
                txtCodeMedicament.setText(code);
                cbxTypeMedicament.setSelectedItem(type);
                txtContenanceMedicament.setText(contenance);
                txtDate.setText(date);
                pu.setText(prix);
                quantiteMedicament.setText(stock);
                txtFabricantMedicament.setText(fabricant);
                txtDescriptionMedicament.setText(description);
     
     
    }//GEN-LAST:event_jtbAffichageMedicamentMouseClicked

    private void btnSupprimerMedicamentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSupprimerMedicamentActionPerformed
        // TODO add your handling code here:
             try {

             BDPharmacie bd=new BDPharmacie();
             Connection connex=bd.connectToBD();
             String sql="DELETE FROM medicament WHERE (id_medicament =? AND id_pharmacie = ?)";
             PreparedStatement stm=connex.prepareStatement(sql);
             int id=Integer.parseInt(idMedoc);
             stm.setInt(1, id);
             stm.setInt(2, InterfaceLogin.idPharmacie);

  
            int choix=JOptionPane.showConfirmDialog(this, "Voulez-vous supprimer ce médicament de votre liste ?" , "Suppression", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  
                 if(choix==JOptionPane.YES_OPTION)
                 {
                      stm.executeUpdate();
                          rafraichir();
                         // viderChamps();
                      JOptionPane.showMessageDialog(this, "Médicament supprimé");
                     }
                 
  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnSupprimerMedicamentActionPerformed
    
    private void btnModifierMedicamentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifierMedicamentActionPerformed

        
        String nom = txtNomMedicament.getText().toString();;
        String code = txtCodeMedicament.getText().toString();;
        String type = cbxTypeMedicament.getSelectedItem().toString();
        String description = txtDescriptionMedicament.getText().toString();;
        String contenance = txtContenanceMedicament.getText().toString();;
        String posologie = txtPosologieMedicament0.getText().toString();;
        String fabricant = txtFabricantMedicament.getText().toString();;
        String date = txtDate.getText().toString();
        String prix = pu.getText();
        float price = Float.parseFloat(prix);
        String stock = quantiteMedicament.getText();
        int Stock = Integer.parseInt(stock);
                
                int id=Integer.parseInt(idMedoc);
        
                int p = JOptionPane.showConfirmDialog(null, "Voulez-vous vraiment modifier le médicement?","Modification Médicament",JOptionPane.YES_NO_OPTION);
        if(p==0){
                   try {
            BDPharmacie bd=new BDPharmacie();
            Connection connex=bd.connectToBD();
            String sql="UPDATE medicament SET nom_medicament=?, code=?, type_medicament=?, description_medicament=?, personne=?,"
                + "contenance_medicament=?, posologie_medicament=?, fabriquant_medicament=?, date_expiration=?, pu=?, stock=? WHERE (id_medicament=? AND id_pharmacie = ?) ";
            PreparedStatement stm=connex.prepareStatement(sql);
            stm.setString(1,nom);
            stm.setString(2, code);
            stm.setString(3,type);
            stm.setString(4, description);
            stm.setString(5, personne);
            stm.setString(6,contenance);
            stm.setString(7, posologie);
            stm.setString(8,fabricant);
            stm.setString(9,date);
            stm.setFloat(10, price);
            stm.setInt(11,Stock);
            stm.setInt(12, id);
            stm.setInt(13, InterfaceLogin.idPharmacie);
            
            stm.executeUpdate();
            rafraichir();
            viderChampsMedicament();

            JOptionPane.showMessageDialog(this, "Médicament mise à jour avec succès"); 
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   
                    try {
                /*
                        byte[] image = rs.getBytes("image");
            ImageIcon imageIcon = new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(lblImageMedicament.getWidth(), lblImageMedicament.getHeight(), Image.SCALE_SMOOTH));
            lblImageMedicament.setIcon(imageIcon);
                        */
                File file =  new File(filename);
                FileInputStream fis = new FileInputStream(file);
                byte [] image = new byte [(int)file.length()];
                fis.read(image);
                //ImageIcon imageIcon = new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(lblImageMedicament.getWidth(), lblImageMedicament.getHeight(), Image.SCALE_SMOOTH));
                //lblImageMedicament.setIcon(imageIcon);
                
               BDPharmacie bd=new BDPharmacie();
               Connection connex = bd.connectToBD();
            
                String sql ="UPDATE medicament SET image = ? WHERE id_medicament = ? ";
                     PreparedStatement stm=connex.prepareStatement(sql);
                     stm = connex.prepareStatement(sql);
                     stm.setBytes(1, image);
                     stm.setInt(2, id);
                     stm.executeUpdate();
                     stm.close();
                
                JOptionPane.showMessageDialog(this, "Photo médicament mis à jour avec succès"); 
            } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);

            }
     }
    }//GEN-LAST:event_btnModifierMedicamentActionPerformed

    private void txtRechercheInformationKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRechercheInformationKeyReleased
           
  
 
    }//GEN-LAST:event_txtRechercheInformationKeyReleased

    private void txtRechercheInformationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRechercheInformationActionPerformed
        // TODO add your handling code here:
                  String nomSearch="%"+txtRechercheInformation.getText()+"%";
            BDPharmacie bd=new BDPharmacie();
             Connection connex= bd.connectToBD();
          try {
             
             String sql="SELECT * FROM medicament WHERE (id_pharmacie = '"+InterfaceLogin.idPharmacie+"' AND (id_medicament = ? OR nom_medicament LIKE ? )) AND stock > 0  ";
             PreparedStatement stm=connex.prepareStatement(sql);
              stm.setString(1, txtRechercheInformation.getText());
              stm.setString(2, nomSearch);

              ResultSet rs=stm.executeQuery();
              
              if(rs.next())
        {
            
                 String nom=rs.getString("nom_medicament");
                 txtNomMedicament.setText(nom);
                 String code=rs.getString("code");
                 txtCodeMedicament.setText(code);     
                 String contenance=rs.getString("contenance_medicament");
                 txtContenanceMedicament.setText(contenance);
                 String date=rs.getString("date_expiration");
                 txtDate.setText(date);
                 String prix=rs.getString("pu");
                 pu.setText(prix);
                 String stock=rs.getString("stock");
                 quantiteMedicament.setText(stock);
                            
            
                       
            byte[] image = rs.getBytes("image");
            ImageIcon imageIcon = new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(lblImageMedicament.getWidth(), lblImageMedicament.getHeight(), Image.SCALE_SMOOTH));
            lblImageMedicament.setIcon(imageIcon);
        }
        else
        {
                JOptionPane.showMessageDialog(null,"Aucun enregistrement correspondant à votre recherche");        }
         
        }catch(Exception e){
            e.printStackTrace();
            
        }
        
    }//GEN-LAST:event_txtRechercheInformationActionPerformed

    private void jLabel19MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel19MouseClicked
                dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel19MouseClicked

    private void jLabel20MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel20MouseClicked

    rafraichir();        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel20MouseClicked

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

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

    /**
     * @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(InterfaceAjoutMedicament.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InterfaceAjoutMedicament.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InterfaceAjoutMedicament.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InterfaceAjoutMedicament.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 InterfaceAjoutMedicament().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEnregistrerMedicament;
    private javax.swing.JButton btnModifierMedicament;
    private javax.swing.JButton btnSupprimerMedicament;
    private javax.swing.JButton btnViderChamp;
    private javax.swing.JComboBox<String> cbxTypeMedicament;
    private javax.swing.JRadioButton checkAdolescent;
    private javax.swing.JRadioButton checkAdulte;
    private javax.swing.JRadioButton checkEnfant;
    private javax.swing.JRadioButton checkNourrisson;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    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 jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jtbAffichageMedicament;
    private javax.swing.JMenu lblChoixFichier;
    private javax.swing.JLabel lblImageMedicament;
    private javax.swing.JLabel lblNomPharmacie;
    private javax.swing.JTextField pu;
    private javax.swing.JTextField quantiteMedicament;
    private javax.swing.JTextField txtCodeMedicament;
    private javax.swing.JTextField txtContenanceMedicament;
    private javax.swing.JTextField txtDate;
    private javax.swing.JTextArea txtDescriptionMedicament;
    private javax.swing.JTextField txtFabricantMedicament;
    private javax.swing.JTextField txtNomMedicament;
    private javax.swing.JComboBox<String> txtPosologieMedicament;
    private javax.swing.JTextField txtPosologieMedicament0;
    private javax.swing.JTextField txtRechercheInformation;
    private javax.swing.JTextField txtRechercheMedicament;
    // End of variables declaration//GEN-END:variables

 private ImageIcon format =null;
    //strin filename
    String filename = null;
    byte[] image_medoc = null;
   private String personne;

}
