/*
 * 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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

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

    /**
     * Creates new form DetailCommande
     */
    public DetailCommande() {
        initComponents();
        this.setLocationRelativeTo(this);
        this.setResizable(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() {

        txtNumCmd = new javax.swing.JTextField();
        btnSearchCmd = new javax.swing.JButton();
        lblNomFournisseur = new javax.swing.JLabel();
        lblAdresseFournisseur = new javax.swing.JLabel();
        lblTelephoneFournisseur = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        lblEtat = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDetailsCmd = new javax.swing.JTable();
        btnValiderCmd = new javax.swing.JButton();
        lblFactureCmd = new javax.swing.JLabel();
        jButton1 = 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();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

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

        txtNumCmd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNumCmdActionPerformed(evt);
            }
        });
        getContentPane().add(txtNumCmd, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 50, 80, 40));

        btnSearchCmd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/loop.png"))); // NOI18N
        btnSearchCmd.setContentAreaFilled(false);
        btnSearchCmd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchCmdActionPerformed(evt);
            }
        });
        getContentPane().add(btnSearchCmd, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 50, -1, -1));

        lblNomFournisseur.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblNomFournisseur.setText("jLabel1");
        getContentPane().add(lblNomFournisseur, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 150, 160, -1));

        lblAdresseFournisseur.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblAdresseFournisseur.setText("jLabel1");
        getContentPane().add(lblAdresseFournisseur, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 190, 160, -1));

        lblTelephoneFournisseur.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblTelephoneFournisseur.setText("jLabel1");
        getContentPane().add(lblTelephoneFournisseur, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 250, 180, -1));

        lblDate.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblDate.setText("jLabel1");
        getContentPane().add(lblDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 160, 120, -1));

        lblEtat.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblEtat.setText("jLabel1");
        getContentPane().add(lblEtat, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 220, 120, -1));

        lblTotal.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblTotal.setText("jLabel1");
        getContentPane().add(lblTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(880, 220, 120, 20));

        jtbDetailsCmd.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"
            }
        ));
        jScrollPane1.setViewportView(jtbDetailsCmd);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, 480, 260));

        btnValiderCmd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btndown.png"))); // NOI18N
        btnValiderCmd.setContentAreaFilled(false);
        btnValiderCmd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderCmdActionPerformed(evt);
            }
        });
        getContentPane().add(btnValiderCmd, new org.netbeans.lib.awtextra.AbsoluteConstraints(820, 500, 90, 60));

        lblFactureCmd.setFont(new java.awt.Font("Arial Black", 0, 16)); // NOI18N
        lblFactureCmd.setText("jLabel1");
        getContentPane().add(lblFactureCmd, new org.netbeans.lib.awtextra.AbsoluteConstraints(880, 160, 120, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ACCUEIL.png"))); // NOI18N
        jButton1.setContentAreaFilled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(900, 30, 80, 70));

        jLabel1.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel1.setText("Nom :");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel2.setText("Adresse :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 200, -1, -1));

        jLabel3.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel3.setText("Téléphone :");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 250, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel4.setText("Date Commande :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 160, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel5.setText("Etat Commande :");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 220, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel6.setText("N° Commande :");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 160, -1, -1));

        jLabel7.setFont(new java.awt.Font("Arial Black", 1, 16)); // NOI18N
        jLabel7.setText("Total TTC :");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 220, -1, -1));

        jLabel8.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Détails commande", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Vivaldi", 1, 24))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 120, 600, 180));

        jLabel9.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information Fournisseur", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Vivaldi", 1, 18))); // NOI18N
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 360, 170));

        jLabel12.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "N° Commande", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Vivaldi", 1, 20))); // NOI18N
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 10, 210, 100));

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/info.png"))); // NOI18N
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 300, 340, 270));

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

         BDPharmacie bd = new BDPharmacie();
        Connection connex;
    private void btnSearchCmdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchCmdActionPerformed
       
                 bd=new BDPharmacie();
                 connex=bd.connectToBD();
             String tab[]={"Médicament", "Quantité", "Montant"};
            DefaultTableModel dftm=new DefaultTableModel();
            
            dftm.setColumnIdentifiers(tab); 
    
             int idAchat=Integer.parseInt(txtNumCmd.getText());
            String sql="SELECT f.nom_fournisseur, f.adresse_fournisseur, f.telephone_fournisseur, cm.date, cm.montant AS total, cm.etat, m.nom_medicament_f, d.quantite, m.pu_f, (m.pu_f*d.quantite) AS somme\n" +
            "FROM detail_commande d, commande cm, fournisseur f, medicament_f m \n" +
            "WHERE d.id_cmd=cm.id_cmd AND f.id_fournisseur=cm.id_fournisseur AND m.id_medicament_f=d.id_medicament_f AND d.id_cmd=? ";  
    
        try {
                BDPharmacie bd = new BDPharmacie();
                PreparedStatement stm=connex.prepareStatement(sql);
                stm.setInt(1, idAchat); 
                ResultSet rs=stm.executeQuery();           
                while(rs.next()){
                Vector v=new Vector();
           
           String nom = rs.getString("f.nom_fournisseur");
           String date = rs.getString("cm.date");
           String telephone = rs.getString("f.telephone_fournisseur");
           String adresse = rs.getString("f.adresse_fournisseur");
           String total = rs.getString("total");
           String medicament = rs.getString("m.nom_medicament_f");
           String quantite = rs.getString("d.quantite");
           String montant = rs.getString("m.pu_f");
           String etat = rs.getString("cm.etat");
           
           
           lblNomFournisseur.setText(nom);
           lblTelephoneFournisseur.setText(telephone);
           lblAdresseFournisseur.setText(adresse);
           lblDate.setText(date);
           lblTotal.setText(total); 
           lblEtat.setText(etat);
          
           v.add(medicament);
           v.add(quantite);
           v.add(montant);
           dftm.addRow(v); 
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        jtbDetailsCmd.setModel(dftm); 
    
    }//GEN-LAST:event_btnSearchCmdActionPerformed

    private void btnValiderCmdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValiderCmdActionPerformed
        
                 bd=new BDPharmacie();
                 connex=bd.connectToBD();
        try {
            
            int idAchat=Integer.parseInt(txtNumCmd.getText());
            String a = String.valueOf(idAchat);
            lblFactureCmd.setText(a);
            int num = Integer.parseInt(a);
            
            String sql = "INSERT INTO medicament (nom_medicament, code, type_medicament, description_medicament, personne, contenance_medicament,"
                    + "posologie_medicament, fabriquant_medicament, date_expiration, pu, stock, image, id_pharmacie) "
                    + "SELECT DISTINCT m.nom_medicament_f, m.code_f, m.type_medicament_f, m.description_medicament_f, m.personne_f, m.contenance_medicament_f,"
                    + "m.posologie_medicament_f, m.fabriquant_medicament_f, m.date_expiration_f, m.pu_f, d.quantite, m.image_f, ? "
                    + "FROM medicament_f m, detail_commande d \n"
                    + "WHERE d.id_cmd=?";
            PreparedStatement stm = connex.prepareStatement(sql);
            stm.setInt(1, InterfaceLogin.idPharmacie);
            stm.setInt(2, num);
            
            stm.executeUpdate();
            
          
            JOptionPane.showMessageDialog(this, "Médicament ajouter dans la base avec succès");
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Erreur ajouter dans la base avec succès");
        }
    }//GEN-LAST:event_btnValiderCmdActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        
            dispose();
    }//GEN-LAST:event_jButton1MouseClicked

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearchCmd;
    private javax.swing.JButton btnValiderCmd;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    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 jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbDetailsCmd;
    private javax.swing.JLabel lblAdresseFournisseur;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblEtat;
    private javax.swing.JLabel lblFactureCmd;
    private javax.swing.JLabel lblNomFournisseur;
    private javax.swing.JLabel lblTelephoneFournisseur;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTextField txtNumCmd;
    // End of variables declaration//GEN-END:variables
}
