/*
 * 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.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Vector;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;

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

    /**
     * Creates new form CarteAcheteEntre2Date
     */
    public CarteAcheteEntre2Date() {
        initComponents();
    }

    /**
     * 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();
        jtabListeCarteAchete = new javax.swing.JTable();
        jdateFin = new com.toedter.calendar.JDateChooser();
        jdateDebut = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        lblTotalCarteAchete = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnRecherche = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

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

        jtabListeCarteAchete.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(jtabListeCarteAchete);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 410, 310));
        getContentPane().add(jdateFin, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, 150, 30));
        getContentPane().add(jdateDebut, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 20, 150, 30));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Total carte acheté");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 130, 120, 30));

        lblTotalCarteAchete.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblTotalCarteAchete.setForeground(new java.awt.Color(0, 204, 153));
        lblTotalCarteAchete.setText("jLabel2");
        getContentPane().add(lblTotalCarteAchete, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 130, 90, 30));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("CFA");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(710, 140, 40, -1));

        btnRecherche.setText("Recherche");
        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(340, 60, 90, 30));

        jButton1.setText("Retour");
        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(690, 20, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Date début");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 20, 80, 30));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Date fin");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 70, 30));

        pack();
    }// </editor-fold>//GEN-END:initComponents
double total;
    public void totalCarteAchete (){
        total = 0;
        for (int i = 0; i<jtabListeCarteAchete.getRowCount(); i++){
            String totalCarte = jtabListeCarteAchete.getValueAt(i, 1).toString();
            total= total + Double.parseDouble(totalCarte);
        }
        lblTotalCarteAchete.setText(total+"");
    }
    
    private void btnRechercheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRechercheActionPerformed
        carteAchete();
        totalCarteAchete();
    }//GEN-LAST:event_btnRechercheActionPerformed

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

    /**
     * @param args the command line arguments
     */
    
    
    public void carteAchete () {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d", Locale.getDefault());
        String dateDebut = sdf.format(jdateDebut.getDate());
        String dateFin = sdf.format(jdateFin.getDate());
        
        try {
            BDPharmacie bd = new BDPharmacie();
            Connection connex = bd.connectToBD();
            String sql ="SELECT cp.code_cartep, cp.montant_cartep, ca.date_achat_carte\n" +
                        "FROM carte_pharmacie cp, cartep_achete ca\n" +
                        "WHERE cp.id_cartep=ca.id_cartep AND ca.date_achat_carte BETWEEN ? AND ? \n" +
                        "AND id_pharmacie ='"+InterfaceLogin.idPharmacie+"'";
            PreparedStatement stm = connex.prepareStatement(sql);
             stm.setString(1, dateDebut);
             stm.setString(2, dateFin);
             ResultSet rs = stm.executeQuery();
             DefaultTableModel dftm = new DefaultTableModel();
             String columns [] = {"Code carte","Montant","Date achat"};
             dftm.setColumnIdentifiers(columns); 
             
             while (rs.next()) {
                 String code = rs.getString("cp.code_cartep");
                 String montant = rs.getString("cp.montant_cartep");
                 String date = rs.getString("ca.date_achat_carte");
                // String montantTotal = rs.getString("Total");
                 
                 Vector v = new Vector();
                 v.add(code);
                 v.add(montant);
                 v.add(date);
                 dftm.addRow(v);
                //lblTotalCarteAchete.setText(total+"");
             }
             jtabListeCarteAchete.setModel(dftm);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    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(CarteAcheteEntre2Date.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CarteAcheteEntre2Date.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CarteAcheteEntre2Date.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CarteAcheteEntre2Date.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 CarteAcheteEntre2Date().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRecherche;
    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.JScrollPane jScrollPane1;
    private com.toedter.calendar.JDateChooser jdateDebut;
    private com.toedter.calendar.JDateChooser jdateFin;
    private javax.swing.JTable jtabListeCarteAchete;
    private javax.swing.JLabel lblTotalCarteAchete;
    // End of variables declaration//GEN-END:variables
}
