#include "sitesdialog.h"
#include "vision.h"

#include <common/constants.h>
#include <common/settings.h>
#include <common/icore.h>

#include <QtCore>
#include <QtGui>
#include <QtSql>

using namespace Common;
using namespace Constants;

static inline Common::Settings *settings() {return Common::ICore::instance()->settings();}

sites::sites(){
  setupUi(this);
  setAttribute    ( Qt::WA_DeleteOnClose);
  //setWindowModality ( Qt::ApplicationModal );
  //m_databaseUn    = QSqlDatabase::database(Constants::DB_DRTUX);
  m_database	  = QSqlDatabase::database(Constants::DB_ACCOUNTANCY);
  m_nomsite 	  = "";
  m_adressesite   = "";
  m_codepostal	  = "";
  m_ville	  = "";
  m_telsite	  = "";
  m_faxsite	  = "";
  m_mailsite	  = "";
  
  //---------comboBox des sites----------------------------
  qDebug() << "iconPath ="+settings()->iconPath()+"/face-smile.png";
  QStringList adresses;
  m_smile . addFile(settings()->iconPath()+"/face-smile.png");
  comboBox                 -> setEditable(false);
  comboBox                 -> setInsertPolicy(QComboBox::NoInsert);
  QSqlQuery  querySites(m_database);
  QString    requeteSites   = "SELECT site , adresse_site , cp_site , ville_site, telsite , faxsite , mailsite FROM sites";
             querySites     . exec(requeteSites);
    while(querySites.next()){
        QString lineSite    = querySites.value(0).toString();
        QString lineAdr     = querySites.value(1).toString();
        QString lineCP      = querySites.value(2).toString();
        QString lineVille   = querySites.value(3).toString();
        QString lineTel     = querySites.value(4).toString();
        QString lineFax     = querySites.value(5).toString();
        QString lineMail    = querySites.value(6).toString();
           m_listeDesSites << lineSite+"="+lineAdr+"="+lineCP+"="+lineVille+"="+lineTel+"="+lineFax+"="+lineMail ;
                adresses   << lineAdr+lineVille.replace(" ","");
                comboBox  -> addItem(m_smile , lineSite , QVariant());
    }
  
 
  //QIcon smileNo("face-crying.png");
  /*QSqlQuery querySitesDrTux(m_databaseUn);
  QString   requetePersonnes = "SELECT Nom , Prenom , Adresse , CodePostal , Ville , Tel_1 , EMail FROM Personnes";
            querySitesDrTux  . exec(requetePersonnes);
      while(querySitesDrTux.next()){
          QString Nom              = querySitesDrTux.value(0).toString();
          QString Prenom           = querySitesDrTux.value(1).toString();
          QString Adresse          = querySitesDrTux.value(2).toString();
          QString CP               = querySitesDrTux.value(3).toString();
          QString Ville            = querySitesDrTux.value(4).toString();
          QString Tel              = querySitesDrTux.value(5).toString();
          QString EMail            = querySitesDrTux.value(6).toString();
          QString siteDrTux        = Nom+" "+Prenom+" "+Adresse+" "+Ville;
          QString NomPrenom        = Nom+" "+Prenom;
          QString Fax              = "fax";
          QString AdrVil           = Adresse+Ville.replace(" ","");
          if(adresses.contains(AdrVil) == false){
              m_listeDesSites     << NomPrenom+"="+Adresse+"="+CP+"="+Ville+"="+Tel+"="+Fax+"="+EMail ;
                  comboBox        -> addItem(smileNo , siteDrTux , QVariant());
          }
      }*/
   if(m_listeDesSites.size() == 0){
       QIcon smileNo(settings()->iconPath()+"/face-crying.png");
       QString NomPrenom = "Docteur Administrateur";
       QString site = NomPrenom+" ma rue 28000 Chartres";
       QString Adresse;
       QString CP;
       QString Ville;
       QString Tel;
       QString Fax;
       QString EMail;
       m_listeDesSites << NomPrenom+"="+Adresse+"="+CP+"="+Ville+"="+Tel+"="+Fax+"="+EMail ;
       comboBox        -> addItem(smileNo , site , QVariant());
   }
    

  connect(pushButton,  SIGNAL(pressed())       ,this,SLOT(inseredanstablesites()));
  connect(pushButton_2,SIGNAL(pressed())       ,this,SLOT(close()));
  connect(pushButton_3,SIGNAL(pressed())       ,this,SLOT(voir()));
  connect(comboBox    ,SIGNAL(highlighted(int)),this,SLOT(integrer(int)));
}

sites::~sites(){
  m_listeDesSites               .clear();
}

void sites::inseredanstablesites(){
  m_nomsite 	= lineEdit->text();
  m_adressesite = textEdit->toPlainText();
  m_codepostal	= lineEdit_3->text();
  m_ville	= lineEdit_4->text();
  m_telsite	= lineEdit_5->text();
  m_faxsite	= lineEdit_6->text();
  m_mailsite	= lineEdit_7->text();
  m_nomsite 	.replace("'","''");
  m_adressesite .replace("'","''");
  m_ville	.replace("'","''");
  m_mailsite	.replace("'","''");
  
  QString idsite;
  QSqlQuery query(m_database);
            query.exec("SELECT id_site FROM sites");
            while(query.next()){
                idsite = query.value(0).toString();
            }
  QString idsite1 = QString::number(idsite.toInt()+1);//incrementation du deuxieme champ
  QSqlQuery query2(m_database);
            query2.exec("INSERT INTO sites (id_site,site,adresse_site,cp_site,ville_site,telsite,faxsite,mailsite)"
		        "VALUES("	
			"'"+idsite1+"',"
			"'"+m_nomsite+"',"
			"'"+m_adressesite+"',"
			"'"+m_codepostal+"',"
			"'"+m_ville+"',"
			"'"+m_telsite+"',"
			"'"+m_faxsite+"',"
			"'"+m_mailsite+"')");
		if(query2.numRowsAffected()== 1){
			QMessageBox::information(0,tr("information"),trUtf8("Les informations\nont été intégrées."),QMessageBox::Ok);
			emit close();
			}
		else {
			QMessageBox::warning(0,tr("attention"),trUtf8("Echec insertion !"),QMessageBox::Ok);
			}
}

void sites::voir(){
  QString         base         = "sites";
  QList<int>      list         ;
                  list        << 0 << 1 ;
  vision         *v            = new vision(base , list);
                  v           -> show();
}

void sites::integrer(int index){
  pushButton -> setEnabled(true);
  QIcon icon  = comboBox -> itemIcon(index);
  qDebug() << "icon "+QString::number(icon.cacheKey());
  qDebug() << "m_smile "+QString::number(m_smile.cacheKey());
  if(icon.cacheKey() == m_smile.cacheKey()){
      qDebug() << "in Combo Oui";
      pushButton -> setEnabled(false);
  }
  QString     infos           = m_listeDesSites[index];
  QStringList listeInfos      = infos.split("=");
              lineEdit       -> setText     (listeInfos[0]);//nom prenom
              textEdit       -> setPlainText(listeInfos[1]);//adresse
              lineEdit_3     -> setText     (listeInfos[2]);//CP
              lineEdit_4     -> setText     (listeInfos[3]);//ville
              lineEdit_5     -> setText     (listeInfos[4]);//tel
              lineEdit_6     -> setText     (listeInfos[5]);//fax
              lineEdit_7     -> setText     (listeInfos[6]);//email
              
}
