/*
 * 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 Models.Client;

import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.connexion;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 *
 * @author Maher
 */
public class ClientEntrepriseContactDB {
    
    connexion cnx = new connexion();
    contro clt = new contro();
    
    public boolean addClientEntrepriseContact(ClientEntrepriseContact CltEtrCon){
        String req = "INSERT INTO `cltcontact` (`idcltcontact`, `nom`, `prenom`, `cin`, `gsm1`, `gsm2`, `mail`, `idcltentreprise`, `local_nom`) VALUES (NULL, '"+CltEtrCon.getNom()+"', '"+CltEtrCon.getPrenom()+"', "+CltEtrCon.getCIN()+", "+CltEtrCon.getTele1()+", "+CltEtrCon.getTele2()+", '"+CltEtrCon.getMail()+"', '"+CltEtrCon.getIdcltentreprise()+"', '"+ParametreSystem.NomLocalPC+"'); " ;
        return cnx.insertion(req);
    }
    
    public ClientEntrepriseContact getContactByIdContact(String idContactEtr){
        ClientEntrepriseContact ContactEtr = new ClientEntrepriseContact();
        
        if(cnx.ouverture())
        {
            String sql="SELECT *  FROM cltcontact WHERE (`codeclient`= "+idContactEtr+") AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
            ResultSet resultat;
            try {
                resultat = cnx.excutionquery(sql);
                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);
            }
        }
       return ContactEtr ;
    }
    
    public ClientEntrepriseContact getContactByIdEntreprise(String idEtr)
    {
        ClientEntrepriseContact ContactEtr = new ClientEntrepriseContact();
        
        if(cnx.ouverture())
        {
            String sql="SELECT * FROM cltcontact WHERE (`idcltentreprise`= "+idEtr+") AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
            ResultSet resultat;
            try {
                resultat = cnx.excutionquery(sql);
                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);
            }
        }
       return ContactEtr ;
    }
    
    
    public boolean updateEtrContact(ClientEntrepriseContact ContactEtr) throws UnsupportedEncodingException
    {
        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()+"');";
        
        if(cnx.ouverture()){
            try{
               int nbr = cnx.executionupdate(req);
               return true;
            }
            catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(req);
                return false ;
            }
        }
        else{
            return false ;
        }
    }
    
}
