/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/



void FormAjoutDevoir::init()
{
    //on vide les listes déroulantes
    comboBoxMatiere->clear();
    comboBoxClasse->clear();
    
    //on sélectionne toutes les matières
    QSqlQuery req;
    QString lib="SELECT nomMatiere FROM MATIERE ORDER BY nomMatiere";
    req.exec(lib);
    
     //pour chaque matière
    while (req.next() )
    {
	comboBoxMatiere->insertItem(req.value(0).toString() );
    }
    
    //on remplit la liste des classes
    comboBoxClasse->insertItem("IG1");
    comboBoxClasse->insertItem("IG2");    
}


void FormAjoutDevoir::ajout_devoir()
{
    //on récupère les valeurs saisies
    QString matiere=comboBoxMatiere->currentText();
    QString classe=comboBoxClasse->currentText();
    QString jour=lineEditJour->text();
    QString mois=lineEditMois->text();
    QString annee=lineEditAnnee->text();
    QString date=annee+"-"+mois+"-"+jour;
    QString libelle=lineEditLib->text();
    
    //on vérifie si tous les champs sont remplis, sinon on affiche un message d'erreur
    if ((jour.isEmpty() )||(mois.isEmpty() )||(annee.isEmpty() )||(libelle.isEmpty() ))
    {
	QMessageBox::warning(this,"Erreur","Tous les champs doivent être remplis");
	return;
    }
    //si tout est bon
    else
    {
	//on récupère le numéro de la matière correspondant
	QSqlQuery req;
	QString lib="SELECT noMatiere FROM MATIERE WHERE nomMatiere='"+matiere+"'";
	req.exec(lib);
	req.next();
	int noMat=req.value(0).toInt();
	QString noMatiere=QString::number(noMat);
	
	
	//on ajoute le devoir dans la base
	lib="INSERT INTO DEVOIR VALUES ("+noMatiere+",'','"+date+"','"+libelle+"','"+classe+"')";
	req.exec(lib);
	//((FormGestionDevoirs *)parent() )->init();
	done(0);
    }
}
