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

#include "reparation.h"
extern QString g_pathabsolu;

repare::repare(){
setupUi(this);
connect(pushButton,SIGNAL(pressed()),this,SLOT(fonctionrepare()));
connect(pushButton_2,SIGNAL(pressed()),qApp,SLOT(closeAllWindows()));
}

void repare::fonctionrepare(){
m_fichierlog = new QFile(""+g_pathabsolu+"/erreursid.txt");
if(!m_fichierlog->open(QIODevice::WriteOnly|QIODevice::Text))
{QMessageBox::warning(0,tr("attention"),trUtf8("le fichier de log erreursid.txt ne peut pas être ouvert."),QMessageBox::Ok);}
m_out = new QTextStream(m_fichierlog);
m_db = QSqlDatabase::database();
reparemodel = new QSqlTableModel(this,m_db);
reparemodel->setTable("Alarmes");
reparemodel->select();
for(int i=0;i<reparemodel->rowCount()+1;i++){
QModelIndex indexnom = reparemodel->index(i,4,QModelIndex());
QModelIndex indexprenom = reparemodel->index(i,5,QModelIndex());
QModelIndex indexid = reparemodel->index(i,6,QModelIndex());
m_datanom = reparemodel->data(indexnom,Qt::DisplayRole).toString();
m_dataprenom = reparemodel->data(indexprenom,Qt::DisplayRole).toString();
m_dataid = reparemodel->data(indexid,Qt::DisplayRole).toString();
QStringList liste;
QSqlQuery query(m_db);
query.exec("SELECT FchGnrl_IDDos FROM IndexNomPrenom WHERE FchGnrl_NomDos='"+m_datanom+"' AND FchGnrl_Prenom='"+m_dataprenom+"';");
/*if(query.lastError().number() == -1)
{
*out << ""+datanom+""+dataprenom+"""\n";
}*/
while(query.next()){
m_drtuxid = query.value(0).toString();
liste << ""+m_drtuxid+"";
}

//qDebug() << ""+drtuxid+"";
if(liste.size()>1){

*m_out << ""+m_drtuxid+"";
}
QSqlQuery query2(m_db);
query2.exec("SELECT  FchGnrl_NomDos,FchGnrl_Prenom FROM IndexNomPrenom WHERE FchGnrl_IDDos= '"+m_drtuxid+"';");
while(query2.next()){
m_autrenom = query2.value(0).toString();
m_autreprenom = query2.value(1).toString();}
if(m_autrenom != m_datanom){
*m_out << ""+m_datanom+""+m_dataprenom+"""\n";
}

if(m_drtuxid != m_dataid){
//qDebug() << ""+dataid+""+datanom+""+dataprenom+"";
reparemodel->setData(indexid,m_drtuxid,Qt::EditRole);}

}
QMessageBox::information(0,tr("info"),trUtf8("les identifiants ont été réparés"),QMessageBox::Ok);
m_fichierlog->close();
m_autrenom = "";
m_autreprenom = "";
m_drtuxid = "";
delete m_out;
delete m_fichierlog;
affiche = new affichage;
affiche->show();
}
