#include <QtGui>
#include <QtCore>
#include <QtSql>
#include "absencedialog.h"
#include "explication.h"
#include "reparation.h"
extern QString g_pathabsolu;
class explic;
class repare;
absencedialog::absencedialog(QString & choix)
{
  setupUi(this);
  setAttribute(Qt::WA_DeleteOnClose);
  setWindowFlags(Qt::WindowStaysOnTopHint);
  if(choix == "un"){
      pushButton->hide();
  }
  if(choix == "deux"){
      pushButton_2->hide();
  }
  connect(pushButton,SIGNAL(pressed()),this,SLOT(lancerenov()));
  connect(pushButton_2,SIGNAL(pressed()),this,SLOT(lancecree()));
  connect(pushButton_3,SIGNAL(pressed()),this,SLOT(fermer()));
  }

void absencedialog::lancerenov(){
  QSqlDatabase db = QSqlDatabase::database();
  QSqlQuery query(db);
  query.exec("ALTER TABLE `Alarmes` CHANGE `Alarm_IDDos` `Alarm_IDDos` VARCHAR( 36 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL;");
  QSqlQuery query1;
  query1.exec("ALTER TABLE Alarmes ADD Alarm_utilisateur VARCHAR(30) COLLATE utf8_unicode_ci NULL AFTER Alarm_valid;");
  QSqlQuery query2(db);
  query2.exec("ALTER TABLE `Alarmes` CHANGE Alarm_Date Alarm_Date datetime;");
    QSqlQuery query3(db);
  query3.exec("ALTER TABLE `Alarmes` CHANGE Alarm_AlarmDate Alarm_AlarmDate datetime;");
  QString texte = trUtf8("La table Alarmes a été mise à jour, le widget va fermer.");
  QString textHtml = "<html><body><font size = 3 color = ""red"">"+texte+"</font></body></html>";
  label-> setText(textHtml);
  label -> setWordWrap(true);
  QTimer::singleShot(3000,qApp,SLOT(closeAllWindows()));
  /*repare *reparation = new repare;
  reparation->show();*/

}

void absencedialog::lancecree(){
  /*explic *explication = new explic;
  explication->show();*/
  //la requete se trouve dans la fonction void explic::fonctioncreetable() de explication.cpp
  QSqlDatabase db = QSqlDatabase::database();
  QString requete;
  requete = "CREATE TABLE Alarmes (ID_Primkey BIGINT(20) NOT NULL AUTO_INCREMENT,"
			    	  "TextAlarme LONGTEXT COLLATE utf8_unicode_ci NULL,"
			          "Alarm_Date DATETIME NULL,"
			          "Alarm_AlarmDate DATETIME NULL,"
				  "Nom VARCHAR(50) COLLATE utf8_unicode_ci NULL,"
				  "Prenom VARCHAR(50) COLLATE utf8_unicode_ci NULL,"
			          "Alarm_IDDos VARCHAR(36) COLLATE utf8_unicode_ci NULL,"
  				  "Alarm_valid BOOL NULL,"
				  "Alarm_utilisateur VARCHAR(30) COLLATE utf8_unicode_ci NULL,"
				  "PRIMARY KEY(ID_Primkey));";
  QSqlQuery query(db);
  query.exec(requete);
  if(db.commit() == true){
    db.close();
    if(connexion()== false)
    {
        label->setText(trUtf8("Vérification impossible."));
        }
  }
  else{
    label->setText(trUtf8("Problème de fermeture de base."));
    }
  }

bool absencedialog::connexion() {
QStringList liste;
QFile file(""+g_pathabsolu+"/utilisateur.ini");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
        QMessageBox::warning(0,tr("erreur"),trUtf8("le fichier utilisateur.ini ne peut pas être ouvert."),QMessageBox::Ok);}
    QTextStream stream(&file);
    while(!stream.atEnd()){
        QString line = stream.readLine();
        liste << ""+line+"";}

QString pmotdepassechoix = liste[2];
QString putil            = liste[1];
QString phost            = liste[0];

QStringList motdepasslist = pmotdepassechoix.split("=");
QStringList utillist      = putil.split("=");
QStringList hostlist      = phost.split("=");

QString mot  = motdepasslist[1];
mot.replace(" ","");
QString util = utillist[1];
util.replace(" ","");
QString host = hostlist[1];
host.replace(" ","");

QSqlDatabase db2   = QSqlDatabase :: addDatabase("QMYSQL");
db2.setHostName    (""+host+"");
db2.setDatabaseName("DrTuxTest");
db2.setUserName    (""+util+"");
db2.setPassword    (""+mot+"");

if (!db2.open()) {
	QMessageBox::critical(0,"Erreur ouverture de base 104", db2.lastError().text());
	return false;
}
  QStringList listetables;
  listetables = db2.tables(QSql::Tables);

  if(listetables.contains("Alarmes",Qt::CaseInsensitive)){
        QString texte = trUtf8("La table Alarmes a été installée,\nle widget se ferme dans 3 secondes.");
        QString textHtml = "<html><body><font size = 3 color = ""red"">"+texte+"</font></body></html>";
	label->setText(textHtml);
	QTimer::singleShot(3000,qApp,SLOT(closeAllWindows()));
		}
	else{

	label->setText(trUtf8("La table Alarmes n'a pas pu être installée."));

	QTimer::singleShot(5000,this,SLOT(close()));
		}

return true;
}

void absencedialog::fermer(){
  emit QApplication::closeAllWindows();
}
