/*
 * 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 javax.swing.JOptionPane;

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

    /**
     * Creates new form RechargementCarte
     */
    public RechargementCarte() {
        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")
    public static String etatCarte;
    public static int montantCarte;
    public static int soldeTotalPharmacie;
    public static int idCarteAchete;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCodeCarte = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnRecharger = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btnQuitter = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(txtCodeCarte, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 90, 190, 32));

        jLabel1.setText("Code de recharge");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 120, 30));

        btnRecharger.setText("Recharger");
        btnRecharger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRechargerActionPerformed(evt);
            }
        });
        getContentPane().add(btnRecharger, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 90, 90, 30));
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 150, 220, 50));

        btnQuitter.setText("Quittez");
        btnQuitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitterActionPerformed(evt);
            }
        });
        getContentPane().add(btnQuitter, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, -1, -1));

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

    private void btnRechargerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRechargerActionPerformed
        String recharge = txtCodeCarte.getText().toString();
        
        if(recharge.isEmpty()){
            JOptionPane.showMessageDialog(this, "Veuillez mettre le code de la carte");
        } else {
            BDPharmacie bd = new BDPharmacie();
            String etat_carte = bd.checkCodeCarte(recharge);
            String montant = bd.checkMontantCarte(recharge);  
            String idCarte = bd.checkIDCarte(recharge);
            
            if (etat_carte == null){
                JOptionPane.showMessageDialog(this, "Numéro de carte invalide");
                return;
              } 
                 etatCarte= String.valueOf(etat_carte);
                 montantCarte = Integer.parseInt(montant);
                 idCarteAchete = Integer.parseInt(idCarte);
                 
                
            switch (etatCarte) {
                case "oui":
                    JOptionPane.showMessageDialog(this, "Carte déjà utilisée");
                break;
                case "non":
                    boolean result = bd.saveCodeCarteClient(montantCarte);
                    String soldePharmacie = bd.checkCreditActuelPharmacie(InterfaceLogin.idPharmacie);
                    soldeTotalPharmacie = Integer.parseInt(soldePharmacie);
                    if (result){
                        boolean result1 = bd.updateStatutCarte(recharge);
                        boolean result2 = bd.saveCarteAchete(idCarteAchete, InterfaceLogin.idPharmacie);
                        JOptionPane.showMessageDialog(this, "Rechargement effectuée. Votre nouveau solde est: "+soldeTotalPharmacie);
                    } else {
                        JOptionPane.showMessageDialog(this, "Erreur enregistrement");
                    }
                   
                break;
                default:
                    JOptionPane.showMessageDialog(this, "Rehcargement impossible, veuillez contacter l'administrateur");
            }
            
        }
    }//GEN-LAST:event_btnRechargerActionPerformed

    private void btnQuitterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitterActionPerformed
        dispose();
    }//GEN-LAST:event_btnQuitterActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnQuitter;
    private javax.swing.JButton btnRecharger;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtCodeCarte;
    // End of variables declaration//GEN-END:variables
}
