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

import Controllers.Traitement.contro;
import Models.connexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author Maher
 */
public class FournisseurContactDB {
    connexion cnx = new connexion();
    
    public int UpdListFournisseurContact(String Code, String Nom, String Prenom, String Tele1, String Tele2, String Mail)
    {
        contro clt= new contro();
        Nom = "'"+Nom.replace("'", "\\'")+"'";
        Prenom = "'"+Prenom.replace("'", "\\'")+"'";
        if(clt.isStringNull(Mail)){ Mail = "NULL"; }else{Mail = "'"+Mail.replace("'", "\\'")+"'";}
        if(clt.isStringNull(Tele1)){ Tele1 = "NULL";}
        if(clt.isStringNull(Tele2)){Tele2 = "NULL"; }
            
        String Query = "UPDATE `contact_frs` SET `nom` = "+Nom+", `prenom` = "+Prenom+", `tele1` = "+Tele1+", `tele2` = "+Tele2+", `email` = "+Mail+" WHERE `idcontactfrs` = "+Code+";";
        return cnx.MettreAJour(Query);
    }
    
    public boolean SetListFournisseurContact(ObservableList<FournisseurContactList> ObservableContactList, String IdFournisseur)
    {
        String Query = "INSERT INTO `contact_frs` (`nom`, `prenom`, `tele1`, `tele2`, `email`, `id_fournisseur`) VALUES ";
	contro clt= new contro();
       
        for(FournisseurContactList ContactList : ObservableContactList){
            String Prenom = "'"+ContactList.getPrenom().replace("'", "\\'")+"'";
            String Nom = "'"+ContactList.getNom().replace("'", "\\'")+"'";
            String Mail = "NULL"; if(!clt.isStringNull(ContactList.getMail())){ Mail = "'"+ContactList.getMail().replace("'", "\\'")+"'";}
            String Tele1 = "NULL"; if(!clt.isStringNull(ContactList.getTele1())){ Tele1 = ContactList.getTele1();}
            String Tele2 = "NULL"; if(!clt.isStringNull(ContactList.getTele2())){ Tele1 = ContactList.getTele2();}
            Query = Query + "("+Nom+", "+Prenom+", "+Tele1+", "+Tele2+", "+Mail+", "+IdFournisseur+"),";
        }
        Query = Query.substring(0, Query.length()-1)+";";
                
        return  cnx.insertion(Query);
    }
    
    public ObservableList<FournisseurContactList> GetListFournisseurContact(String CodeFrs)
    {
        ObservableList<FournisseurContactList> ObservableListContactFrs = FXCollections.observableArrayList();
        
        String Query = "SELECT * FROM contact_frs WHERE (id_fournisseur = "+CodeFrs+");";
        
        if(cnx.ouverture()){
            try{
                ResultSet resultat = cnx.excutionquery(Query);
                
                resultat.last();//on place le curseur sur le dernier tuple
               
                if(resultat.getRow()>0){ //on récupère le numéro de la ligne
                    resultat.beforeFirst();
                    while ( resultat.next() ) {
                        String Code = resultat.getString("idcontactfrs");
                        String Nom = resultat.getString("nom");
                        String Prenom = resultat.getString("prenom");
                        String Tele1 = resultat.getString("tele1");
                        String Tele2 = resultat.getString("tele2");
                        String Mail = resultat.getString("email");
                        ObservableListContactFrs.add(new FournisseurContactList(Code, Nom, Prenom, Tele1, Tele2, Mail));
                    }
                }
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
            }
        }
        return ObservableListContactFrs;
    }
}
