
/***************************************************************************
 *   Copyright (C) 2008 by Dr Pierre-Marie DESOMBRE   *
 *   pm.desombre@medsyn.fr   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


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

#include "lecturedialog.h"
#include "modifierdialog.h"



Lecturalarme::Lecturalarme(QWidget *lecturealarmes):QWidget(lecturealarmes)
{

Ui_lecturealarmes::setupUi(this);

modele->setTable("Alarmes");
modele->setFilter("Alarm_Date <= '"+QDate::currentDate().toString("yyyy-MM-dd")+"'");
modele->select();

tableView->setModel(modele);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setColumnHidden(0,true);
tableView->setColumnHidden(3,true);
tableView->setColumnHidden(6,true);

tableView->show();


for (int row = 0; row < modele->rowCount(); ++row)
{
c_record = modele->record(row);
//QStringList ident;
c_ident.append(c_record.value("Alarm_IDDos").toString());
}

connect(Ui_lecturealarmes::pushButton,SIGNAL(clicked()),this,SLOT(bouger()));
connect(Ui_lecturealarmes::pushButton_2,SIGNAL(clicked()),this,SLOT(bouger()));
connect(Ui_lecturealarmes::pushButton_3,SIGNAL(clicked()),this,SLOT(effacer()));
connect(Ui_lecturealarmes::pushButton_4,SIGNAL(clicked()),this,SLOT(enregistrer()));
connect(Ui_lecturealarmes::pushButton_4,SIGNAL(clicked()),this,SLOT(close()));


}


void Lecturalarme::bouger()
{
modifierdialog *modif = new modifierdialog;
modif->setModal(true);
if(modif->exec() == QDialog::Accepted)
{
modif->modifierdialog::effacer(modif);
}
}

/*void Lecturalarme::voirtoutes()
{
QSqlTableModel *modeletoute = new QSqlTableModel;
modeletoute->setTable("Alarmes");
modeletoute->select();

QTableView *tabletoute = new QTableView;
tabletoute->setModel(modeletoute);
tabletoute->setColumnHidden(0,true);
tabletoute->setColumnHidden(3,true);
tabletoute->setColumnHidden(6,true);
tabletoute->setColumnHidden(7,true);
tabletoute->show();
}*/

void Lecturalarme::effacer()
{
QString id;
QModelIndex index = tableView->QAbstractItemView::currentIndex();
int i = index.row();
 id = c_ident[i];
QSqlQuery query;
query.exec("DELETE FROM Alarmes WHERE Alarm_IDDos = '"+id+"'");
}


void Lecturalarme::enregistrer()
{
QString alarmeId;
QString textealarme;
int alarmvalid;

QSqlQuery query;
query.exec("SELECT Alarm_IDDos,TextAlarme,Alarm_valid FROM Alarmes WHERE Alarm_Date <= '"+QDate::currentDate().toString("yyyy-MM-dd")+"'");

while(query.next())
{
alarmvalid = query.value(2).toInt();
bool b = alarmvalid==1;
if(b == false)
{
alarmeId = query.value(0).toString();
textealarme = query.value(1).toString();
textealarme.replace(QRegExp("\\n"),"<br/>");

textealarme = "<html><head><meta name=\"qrichtext\" content=\"1\"/></head><body style=\"font-size:20pt;font-family:Arial\"><p>"+textealarme+"</p></body></html>";

QSqlQuery query2;
query2.exec("INSERT INTO RubriquesBlobs (RbDate_PrimKey,RbDate_DataRub,RbDate_IDDos) VALUES (NULL,'"+textealarme+"','"+alarmeId+"')");
QString lastid = query2.lastInsertId().toString();

QSqlQuery query3;
query3.exec("INSERT INTO RubriquesHead"
"(RbDate_PrimKey,RbDate_IDDos,RbDate_TypeRub,RbDate_NomDate,RbDate_SubTypeRub,RbDate_Date,RbDate_CreateUser,RbDate_CreateSignUser,RbDate_Duree,RbDate_Ref_NumDoss,RbDate_RefBlobs_Primkey)"
"VALUES (NULL,'"+alarmeId+"',20030000,'Alarme',NULL,'"+QDate::currentDate().toString("yyyy-MM-dd")+"','admin','admin',0,0,'"+lastid+"')");
QSqlQuery query4;
query4.exec("UPDATE Alarmes SET Alarm_valid = '1' WHERE Alarm_IDDos = '"+alarmeId+"'");
}
}

}
