/***************************************************************************
 *   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 <QtCore>
#include <QtSql>

#include "alarmedialog.h"

class QTextStream;
Alarmedialog::Alarmedialog(QWidget *parent):QDialog(parent)
	
{
//prépare le QWidget UI préparé par QTDesigner
setupUi(this);
texte=lineEdit->text();
if(texte.isEmpty())
textenonchange();

connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(textechange(const QString&)));


comboBox->setEditable(1);
comboBox->setInsertPolicy(QComboBox::NoInsert);
QFile dedans("../alertes.ini");

if(!dedans.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream stream(&dedans);
while(!stream.atEnd())
{QString ligne = stream.readLine();
textes << ""+ligne+"";
}

comboBox->addItems(textes);

connect(pushButton,SIGNAL(clicked()),this,SLOT(ma_fonction_globale()));

}

void Alarmedialog::textechange(const QString &texte){

model = new QSqlTableModel;
model->setTable("IndexNomPrenom");

model->setFilter("FchGnrl_NomDos LIKE '"+texte+"%'");
model->setSort(1,Qt::AscendingOrder);//on classe par ordre alphabétique

model->select();

proxymodel = new QSortFilterProxyModel;
proxymodel->setSourceModel(model);

tableView->setModel(proxymodel);
tableView->setColumnHidden(0,true);//on cache la colonne ID_Primkey
tableView->setColumnHidden(3,true);//on cache la colonne FchGnrl_IDDos
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->show();
}

void Alarmedialog::textenonchange(){

model = new QSqlTableModel;
model->setTable("IndexNomPrenom");

model->setSort(1,Qt::AscendingOrder);//on classe par ordre alphabétique

model->select();

proxymodel = new QSortFilterProxyModel;
proxymodel->setSourceModel(model);

tableView->setModel(proxymodel);
tableView->setColumnHidden(0,true);//on cache la colonne ID_Primkey
tableView->setColumnHidden(3,true);//on cache la colonne FchGnrl_IDDos
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->show();
}

void Alarmedialog::ma_fonction_globale()
{
QString donneenom;
QString donneeprenom;
QString donneeid;

QString textedit;
//recupere la date du jour de l'ordi puis la transforme dans le bon format.
date2 = QDate::currentDate();
QString datecourante = date2.toString("yyyy-MM-dd");
//recupere la date de l'alarme selectionnee
date3 = calendarWidget->selectedDate();

QString dateselect = date3.toString("yyyy-MM-dd");
//recuperation de l'identifiant du dossier patient

QModelIndex index = tableView->QAbstractItemView::currentIndex();
int row = index.row();
QModelIndex n = proxymodel->index(row,0,QModelIndex());
QModelIndex pr = proxymodel->index(row,1,QModelIndex());
QModelIndex i = proxymodel->index(row,2,QModelIndex());
donneenom = n.data(Qt::DisplayRole).toString();
donneeprenom = pr.data(Qt::DisplayRole).toString();
donneeid = i.data(Qt::DisplayRole).toString();

textedit = comboBox->currentText();
//insere les variables obtenues dans la table DrTuxTest > Alarmes
QSqlQuery query2;
query2.exec("INSERT INTO Alarmes (TextAlarme, Alarm_Date, Alarm_AlarmDate, Nom, Prenom,Alarm_IDDos)" 
		"VALUES('"+textedit+"','"+dateselect+"','"+datecourante+"','"+donneenom+"','"+donneeprenom+"','"+donneeid+"')");
comboBox->clearEditText();
}
