#include <QtGui>
#include <QtCore>

#include "modifierrecette.h"

modifierrecette::~modifierrecette() {}

modifierrecette::modifierrecette()

{
m_db1 = QSqlDatabase::database("un");
m_db2 = QSqlDatabase::database("deux");
setupUi(this);
dateEdit->setDate(QDate::currentDate());
dateEdit_2->setDate(QDate::currentDate());
setAttribute(Qt::WA_DeleteOnClose);
m_modela = new QSqlTableModel(this,m_db2);
//model->setQuery("SELECT * FROM honoraires",db2);
m_modela->setTable("honoraires");

m_modela->select();
tableView->setModel(m_modela);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setColumnHidden(0,true);
tableView->setColumnHidden(1,true);
tableView->setColumnHidden(2,true);
tableView->setColumnHidden(4,true);
tableView->setColumnHidden(5,true);
tableView->setColumnHidden(8,true);
tableView->setColumnHidden(15,true);
tableView->setColumnHidden(16,true);
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->resizeColumnsToContents();
//tableView->show();

/*for (int row = 0; row < modela->rowCount(); ++row)
{
class_record = modela->record(row);
//QStringList ident;
class_ident.append(class_record.value("id_hono").toString());
}*/
//construction comboBox "selon"
QStringList listechamps;
listechamps << "patient" << "praticien" << "remarque" << "esp" << "chq" << "cb" << "daf" << "autre" << "du" << "valide" ;
comboBox->setEditable(false);
comboBox->setInsertPolicy(QComboBox::NoInsert);
comboBox->addItems(listechamps);

connect(pushButton,SIGNAL(pressed()),this,SLOT(modifier()));
connect(pushButton_2,SIGNAL(pressed()),this,SLOT(effacer()));
connect(pushButton_3,SIGNAL(pressed()),this,SLOT(close()));
connect(pushButton_4,SIGNAL(pressed()),this,SLOT(rechercher()));

}

void modifierrecette::modifier()
{
QSqlTableModel *modele = new QSqlTableModel(this,m_db2);
modele->setTable("honoraires");
modele->setEditStrategy(QSqlTableModel::OnFieldChange);
modele->select();

tableView->setModel(modele);
tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setColumnHidden(0,true);
tableView->setColumnHidden(1,true);
tableView->setColumnHidden(2,true);
tableView->setColumnHidden(4,true);
tableView->setColumnHidden(5,true);
tableView->setColumnHidden(8,true);
tableView->setColumnHidden(15,true);
tableView->setColumnHidden(16,true);
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->resizeColumnsToContents();

}

void modifierrecette::effacer()
{
//QItemSelectionModel *selection = new QItemSelectionModel;
QModelIndex index = tableView->QAbstractItemView::currentIndex();
if(!index.isValid()){QMessageBox::warning(0,tr("erreur"),tr("erreur index non valide"),QMessageBox::Ok);}
int i = index.row();
//int id = class_ident[i].toInt();

if(tableView->model() == m_modela){
m_modela->removeRows(i,1,QModelIndex());

/*QSqlQuery query(db2);
query.exec("DELETE FROM honoraires WHERE id_hono = "+QString::number(id)+"");*/
QMessageBox::information(0,tr("information"),trUtf8("la ligne a été effacée."),QMessageBox::Ok);
//emit close();
}

else if(tableView->model() == m_modelfiltre){
m_modelfiltre->removeRows(i,1,QModelIndex());
QMessageBox::information(0,tr("information"),trUtf8("la ligne a été effacée."),QMessageBox::Ok);
//emit close();
}

else {QMessageBox::warning(0,tr("erreur"),trUtf8("strange thing"),QMessageBox::Ok);}
}

void modifierrecette::rechercher()
{
QString filtre;
QString filtremodulo;
filtre = lineEdit->text();
QString champ = comboBox->currentText();
if(filtre == "")
{
filtremodulo = "%";
}
else filtremodulo = filtre+"%";

QDate datedebut = dateEdit->date();
QString datedebutstring = datedebut.toString("yyyy-MM-dd");
QDate datefin = dateEdit_2->date();
QString datefinstring = datefin.toString("yyyy-MM-dd");
//le code du filtre
//QString filtremodel = "SELECT * FROM honoraires WHERE "+champ+" LIKE '"+filtremodulo+"' AND "+champ+" NOT LIKE '"+QString::number(0)+"' AND date BETWEEN '"+datedebutstring+"' AND '"+datefinstring+"'";
QString filtremodel = ""+champ+" LIKE '"+filtremodulo+"' AND "+champ+" NOT LIKE '"+QString::number(0)+"' AND date BETWEEN '"+datedebutstring+"' AND '"+datefinstring+"'";
m_modelfiltre = new QSqlTableModel(this,m_db2);
m_modelfiltre->setTable("honoraires");
//QSqlQueryModel *modelfiltre = new QSqlQueryModel;
//modelfiltre->setQuery("SELECT * FROM honoraires WHERE "+champ+" LIKE '"+filtremodulo+"' AND "+champ+" NOT LIKE '"+QString::number(0)+"' AND date BETWEEN '"+datedebutstring+"' AND '"+datefinstring+"'",db2);
//proxyfiltre = new QProxyModel;
//proxyfiltre->setModel(modelfiltre);
//modelfiltre->submit();
m_modelfiltre->setFilter(filtremodel);
m_modelfiltre->select();

tableView->setModel(m_modelfiltre);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setColumnHidden(0,true);
tableView->setColumnHidden(1,true);
tableView->setColumnHidden(2,true);
tableView->setColumnHidden(4,true);
tableView->setColumnHidden(5,true);
tableView->setColumnHidden(8,true);
tableView->setColumnHidden(15,true);
tableView->setColumnHidden(16,true);
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
tableView->resizeColumnsToContents();

}
