package Models.Client;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.H2JDBC;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Maher Ben Tili
 */
public class ClientEntrepriseContactDB {
    
    Logger logger = Logger.getLogger(ClientEntrepriseContactDB.class.getName());
    
    H2JDBC h2jdbc = new H2JDBC();
    
    contro clt = new contro();
    
    ShowDialog SD = new ShowDialog();
    
    public boolean addClientEntrepriseContact(ClientEntrepriseContact CltEtrCon)
    {
        String req = "INSERT INTO cltcontact (nom, prenom, cin, gsm1, gsm2, mail, idcltentreprise, local_nom) VALUES ('"+CltEtrCon.getNom()+"', '"+CltEtrCon.getPrenom()+"', "+CltEtrCon.getCIN()+", "+CltEtrCon.getTele1()+", "+CltEtrCon.getTele2()+", '"+CltEtrCon.getMail()+"', '"+CltEtrCon.getIdcltentreprise()+"', '"+ParametreSystem.NomLocalPC+"'); " ;
        
        int nbrInsert = 0;
        try {
            nbrInsert = h2jdbc.InsertUpdateDeleteInTable(req);
        } catch (SQLException ex) {
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ClientEntrepriseContactDB  : addClientEntrepriseContact :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
                
        return (nbrInsert>0);
    }
    
    public ClientEntrepriseContact getContactByIdContact(String idContactEtr)
    {
        ClientEntrepriseContact ContactEtr = new ClientEntrepriseContact();
        
        String sql="SELECT *  FROM cltcontact WHERE (codeclient= "+idContactEtr+") AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(sql);
            while (resultat.next()){
                ContactEtr.setIdcltentrepriseString(resultat.getString("idcltcontact"));
                ContactEtr.setCINString(resultat.getString("cin"));
                ContactEtr.setNom(resultat.getString("nom"));
                ContactEtr.setPrenom(resultat.getString("prenom"));
                ContactEtr.setTele1String(resultat.getString("gsm1"));
                ContactEtr.setTele2String(resultat.getString("gsm2"));
                ContactEtr.setMail(resultat.getString("mail"));
                ContactEtr.setLocalNom(resultat.getString("local_nom"));
            }
        }catch(SQLException ex){
		
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(sql);
			
            logger.error("ClientEntrepriseContactDB  : getContactByIdContact :" + ex.getMessage());
			
            ShowDialog.setTitre("Erreur SQL !");
            ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
            ShowDialog SD = new ShowDialog();
            SD.ShowAletDialog();
        }
        return ContactEtr ;
    }
    
    public ClientEntrepriseContact getContactByIdEntreprise(String idEtr)
    {
        ClientEntrepriseContact ContactEtr = new ClientEntrepriseContact();
        
        String sql="SELECT * FROM cltcontact WHERE (idcltentreprise= "+idEtr+") AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(sql);
            while (resultat.next()){
                ContactEtr.setCodeString(resultat.getString("idcltcontact"));
                ContactEtr.setIdcltentrepriseString(resultat.getString("idcltentreprise"));
                ContactEtr.setCINString(resultat.getString("cin"));
                ContactEtr.setNom(resultat.getString("nom"));
                ContactEtr.setPrenom(resultat.getString("prenom"));
                ContactEtr.setTele1String(resultat.getString("gsm1"));
                ContactEtr.setTele2String(resultat.getString("gsm2"));
                ContactEtr.setMail(resultat.getString("mail"));
                ContactEtr.setLocalNom(resultat.getString("local_nom"));
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(sql);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ClientEntrepriseContactDB  : getContactByIdEntreprise :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ContactEtr ;
    }
    
    
    public boolean updateEtrContact(ClientEntrepriseContact ContactEtr)
    {
        String req = " UPDATE cltcontact ";
        
        req += " SET nom = '"+ContactEtr.getNom()+"',";
        
        if(!clt.isStringNull(ContactEtr.getCINString())){
            req += " cin = '"+ContactEtr.getCINString()+"',";
        }
        if(!clt.isStringNull(ContactEtr.getMail())){
            req += " mail = '"+ContactEtr.getMail()+"',";
        }
        if(!clt.isStringNull(ContactEtr.getTele2String())){
            req += " gsm2 = '"+ContactEtr.getTele2String()+"',";
        }
        
        req += " prenom = '"+ContactEtr.getPrenom()+"',";
        req += " gsm1 = '"+ContactEtr.getTele1String()+"' ";
        
        req += "WHERE (idcltcontact = '"+ContactEtr.getCode()+"') AND (local_nom = '"+ContactEtr.getLocalNom()+"');";
        
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(req);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ClientEntrepriseContactDB  : updateEtrContact :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return (nbr>0);
    }
    
}
