#include <QtCore>
#include <QtGui>
#include <QtSql>
#include <QDebug>
#include "payeursdialog.h"
#include "vision.h"
#include "../constants.h"
using namespace Common;
using namespace Constants;
payeurs::payeurs()
{
m_database	= QSqlDatabase::database(Constants::DB_ACCOUNTANCY);
setupUi(this);
setAttribute      ( Qt::WA_DeleteOnClose);
//setWindowModality ( Qt::ApplicationModal );
pushButton_2    ->setEnabled(true);
m_nompayeur 	= "";
m_adressepayeur = "";
m_cdpostpayeur 	= "";
m_villepayeur 	= "";
m_telpayeur 	= "";
  connect(pushButton,  SIGNAL(pressed()),this,SLOT(enregistrepayeurs()));
  connect(pushButton_2,SIGNAL(pressed()),this,SLOT(close()));
  connect(pushButton_3,SIGNAL(pressed()),this,SLOT(voir()));

}
payeurs::~payeurs(){}

void payeurs::enregistrepayeurs(){
  m_nompayeur 	               = lineEdit->text();
  m_adressepayeur              = textEdit->toPlainText();
  m_cdpostpayeur               = lineEdit_2->text();
  m_villepayeur                = lineEdit_3->text();
  m_telpayeur 	               = lineEdit_4->text();
  
  m_nompayeur 	               . replace("'","''");
  m_adressepayeur              . replace("'","''");
  m_villepayeur                . replace("'","''");


  QString idpayeur;
  QSqlQuery query(m_database);
  query.exec("SELECT id_payeurs FROM payeurs");
  while(query.next()){
      idpayeur = query.value(0).toString();
  }
  qDebug() << idpayeur;
  QString idpayeur1 = QString::number(idpayeur.toInt()+1);//incrementation du deuxieme champ
  QSqlQuery query2(m_database);
  query2.exec("INSERT INTO payeurs (id_payeurs,nom_payeur,adresse_payeur,ville_payeur,code_postal,telephone_payeur)"
		"VALUES ("
		"'"+idpayeur1+"',"
		"'"+m_nompayeur+"',"
		"'"+m_adressepayeur+"',"
		"'"+m_villepayeur+"',"
		"'"+m_cdpostpayeur+"',"
		"'"+m_telpayeur+"')");
		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 payeurs::voir(){
  QString         base         = "payeurs";
  QList<int>      list         ;
                  list        << 0 << 1 ;
  vision         *v            = new vision(base , list);
                  v           -> show();
}
