/*
 * 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 java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.ParametreSystem;
import org.apache.log4j.Logger;
import Models.H2JDBC;

/**
 * @author Maher Ben Tili
 */
public class FournisseurContactDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(FournisseurContactDB.class.getName());
    contro clt= new contro();
    
    public int UpdListFournisseurContact(String Code, String Nom, String Prenom, String Tele1, String Tele2, String Mail)
    {
        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 req = "UPDATE contact_frs SET nom = "+Nom+", prenom = "+Prenom+", tele1 = "+Tele1+", tele2 = "+Tele2+", email = "+Mail+" WHERE idcontactfrs = "+Code+";";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(req);
        }catch(SQLException ex){
            System.err.println("UpdListFournisseurContact Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FournisseurContactDB  : UpdListFournisseurContact :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return nbr;
    }
    
    public boolean SetListFournisseurContact(ObservableList<FournisseurContactList> ObservableContactList, String IdFournisseur)
    {
        String Query = "INSERT INTO contact_frs (nom, prenom, tele1, tele2, email, id_fournisseur) VALUES ";

        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)+";";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(Query);
        }catch(SQLException ex){
            System.err.println("SetListFournisseurContact Erreur SQL ! \n"+ex.getMessage());
            System.out.println(Query);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FournisseurContactDB  : SetListFournisseurContact :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }        
        return (nbr>0);
    }
    
    public ObservableList<FournisseurContactList> GetListFournisseurContact(String CodeFrs)
    {
        ObservableList<FournisseurContactList> ObservableListContactFrs = FXCollections.observableArrayList();
        
        String req = "SELECT * FROM contact_frs WHERE (id_fournisseur = "+CodeFrs+");";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            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("GetListFournisseurContact Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FournisseurContactDB  : GetListFournisseurContact :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ObservableListContactFrs;
    }
}
