/*
 * 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.time.LocalDate;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

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

    /**
     * Creates new form GestionExpiration
     */
    public GestionExpiration() {
        initComponents();
        expiration();
        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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbMedicamentExpiration = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

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

        jtbMedicamentExpiration.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jtbMedicamentExpiration.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(jtbMedicamentExpiration);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 650, 330));

        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(700, 10, 60, 60));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public void expiration (){
        try {
             BDPharmacie bd=new BDPharmacie();
             Connection connex = bd.connectToBD();
             String sql="SELECT id_medicament, nom_medicament, code, stock, date_expiration, DATEDIFF (date_expiration, CURRENT_DATE) As expiration"
                        + " FROM medicament WHERE id_pharmacie = '"+InterfaceLogin.idPharmacie+"' AND DATEDIFF (date_expiration, CURRENT_DATE)<30 "
                        + " AND stock >0 GROUP BY nom_medicament ORDER BY expiration ASC";
             PreparedStatement stm = connex.prepareStatement(sql);
             ResultSet rs=stm.executeQuery();
             DefaultTableModel dftm = new DefaultTableModel();
             String columns[]={"ID", "NOM","Code", "Stock", "date_expiration","Jour restant"};
             dftm.setColumnIdentifiers(columns); 
             
             while(rs.next())
             { 
                String id=rs.getString("id_medicament");
                 String nom=rs.getString("nom_medicament");
                 String code=rs.getString("code");
                 String stock=rs.getString("stock");
                 String date=rs.getString("date_expiration");
                 String expiration=rs.getString("expiration");
                 
                 int a = Integer.parseInt(expiration);
                                 
                 Vector v=new Vector();
                 v.add(id);
                 v.add(nom);
                 v.add(code);
                 v.add(stock);
                 v.add(date);
                 v.add(expiration);
                 dftm.addRow(v); 
                 
             }
             
             jtbMedicamentExpiration.setModel(dftm); 
             
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    LocalDate date = LocalDate.now();
    BDPharmacie bd = new BDPharmacie();
        Connection connex;
        
   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbMedicamentExpiration;
    // End of variables declaration//GEN-END:variables
}
