/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package presentation;

import Metier.Medicament;
import acces_aux_donnes.MedicamentSQL;
import acces_aux_donnes.familleMedocSQL;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author emile.lalorcey
 */
public class jffFamilleMedoc extends javax.swing.JFrame {

    /**
     * Creates new form jffFamilleMedoc
     */
    public jffFamilleMedoc() {
        initComponents();
        familleMedocSQL famille = new familleMedocSQL();
        jCBFamille.setModel(new DefaultComboBoxModel(famille.rechercheFamille()));
    }

    /**
     * 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();
        jLRecherche = new javax.swing.JLabel();
        jTFRecherche = new javax.swing.JTextField();
        jBRechercheValide = new javax.swing.JButton();
        jCBFamille = new javax.swing.JComboBox<>();
        jCBMedocListe = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/logodetoure.gif"))); // NOI18N

        jLRecherche.setText("Recherche médicament : ");

        jTFRecherche.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFRechercheActionPerformed(evt);
            }
        });

        jBRechercheValide.setText("Rechercher");
        jBRechercheValide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRechercheValideActionPerformed(evt);
            }
        });

        jCBFamille.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCBFamille.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBFamilleActionPerformed(evt);
            }
        });

        jCBMedocListe.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCBMedocListe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBMedocListeActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLRecherche)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTFRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(58, 58, 58))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCBFamille, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(42, 42, 42)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBRechercheValide)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCBMedocListe, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLRecherche)
                    .addComponent(jTFRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBRechercheValide))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBFamille, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBMedocListe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(287, Short.MAX_VALUE))
        );

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

    private void jTFRechercheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFRechercheActionPerformed
        
    }//GEN-LAST:event_jTFRechercheActionPerformed

    private void jBRechercheValideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRechercheValideActionPerformed
        String medocRechercher = jTFRecherche.getText();
        System.out.println(medocRechercher);
        MedicamentSQL medocTest = new MedicamentSQL();
        ArrayList<String> rechercheMedoc = new ArrayList<String>();
        rechercheMedoc = medocTest.rechercherMedicament(medocRechercher);
        if (rechercheMedoc.get(0) != null) {
            String idTrouve = rechercheMedoc.get(0);
            String nomTrouve = rechercheMedoc.get(1);
            String compositionTrouve = rechercheMedoc.get(2);
            String effetTrouve = rechercheMedoc.get(3);
            String contreIndicationTrouve = rechercheMedoc.get(4);
            String imgTrouve = idTrouve;
            Medicament medocTrouve = new Medicament(idTrouve, nomTrouve , compositionTrouve, effetTrouve, contreIndicationTrouve,imgTrouve);
            new jffInfoMedoc(medocTrouve).setVisible(true);
       
            
        }else {
            //ajout d'un JoptionPane
        }
    }//GEN-LAST:event_jBRechercheValideActionPerformed

    private void jCBFamilleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBFamilleActionPerformed
        familleMedocSQL famille = new familleMedocSQL();
        Vector<String> medocFamille = new Vector<String>();
        String libelleFamille = jCBFamille.getSelectedItem().toString();
        String codeFamille = famille.rechercheCodeFamille(libelleFamille);
        medocFamille = famille.rechercherMedocFamille(codeFamille);
        jCBMedocListe.setModel(new DefaultComboBoxModel(medocFamille));
        
        
        
    }//GEN-LAST:event_jCBFamilleActionPerformed

    private void jCBMedocListeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBMedocListeActionPerformed
        String nomMedoc = jCBMedocListe.getSelectedItem().toString();
        System.out.println(nomMedoc);
        MedicamentSQL medocTest = new MedicamentSQL();
        ArrayList<String> rechercheMedoc = new ArrayList<String>();
        rechercheMedoc = medocTest.rechercherMedicament(nomMedoc);
        System.out.print("TEst");
        if (rechercheMedoc.get(0) != null) {
            String idTrouve = rechercheMedoc.get(0);
            String nomTrouve = rechercheMedoc.get(1);
            String compositionTrouve = rechercheMedoc.get(2);
            String effetTrouve = rechercheMedoc.get(3);
            String contreIndicationTrouve = rechercheMedoc.get(4);
            String imgTrouve = idTrouve;
            Medicament medocTrouve = new Medicament(idTrouve, nomTrouve , compositionTrouve, effetTrouve, contreIndicationTrouve,imgTrouve);
            new jffInfoMedoc(medocTrouve).setVisible(true);
        }
    }//GEN-LAST:event_jCBMedocListeActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBRechercheValide;
    private javax.swing.JComboBox<String> jCBFamille;
    private javax.swing.JComboBox<String> jCBMedocListe;
    private javax.swing.JLabel jLRecherche;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTFRecherche;
    // End of variables declaration//GEN-END:variables
}
