#include <qsqlquery.h>

void FormSession::init()
{
    // Suppression de la colonne de gauche
    zdlParticipants->verticalHeader()->hide();
    zdlParticipants->setLeftMargin(0);
    // Affichage de tous les code de formation
    QSqlQuery query( "SELECT codeform FROM formation ORDER BY codeform;" );
    while ( query.next() )
    {
	cbxFormations->insertItem( query.value(0).toString() );
    }
    // Chargement le formation par défaut et on affiche les numéros de sessions disponibles
    ChargeFormation();
}

int FormSession::CompteParticipants(QString f, int s)
{
    QString ch_s=QString::number(s);
    QString requete="SELECT count(*) FROM participer WHERE codeform='"+f+"' AND nosession="+ch_s+";";
    QSqlQuery query( requete);
    query.next();
    QVariant ch_nb=query.value(0);
    return ch_nb.toInt();
}

void FormSession::ChargeFormation()
{
    // Affichage des sessions pour la formation
    QString codeform=cbxFormations->currentText();
    QString requete="SELECT nosession FROM session WHERE codeform='"+codeform+"' ORDER BY nosession;";
    QSqlQuery query( requete);
    cbxSessions->clear();
    while ( query.next() )
    {
	cbxSessions->insertItem( query.value(0).toString() );
    }
    // Récupération du nombre maximum de stagiaire pour la formation
    requete="SELECT nbmaxstagiaires FROM formation WHERE codeform='"+codeform+"';";
    QSqlQuery query2(requete);
    query2.next();
    QVariant nb_variant=query2.value(0);
    int nb_int=nb_variant.toInt();
    QString ch_nb=QString::number(nb_int);
    etqNbMaxStag->setText(ch_nb);
    // Affichage du nombre de participants à la session par défaut pour la formation
    SurChangement();
}

void FormSession::SurChangement()
{
    // Affichage du nombre de participants pour la session de telle formation 
    QString lib_session=cbxSessions->currentText();
    QString codeform=cbxFormations->currentText();
    int nosession=lib_session.toInt();
    int nbParticipant=CompteParticipants(codeform,nosession);
    QString chnbParticipant=QString::number(nbParticipant);
    etqNbParticipants->setText(chnbParticipant);
    
    // Affichage des valeurs dans le QTable
    QString requete="SELECT S.nostagiaire, nomstagiaire, prenomstagiaire, nomclient FROM participer P, stagiaire S, client C WHERE P.nostagiaire=S.nostagiaire AND S.noclient=C.noclient AND codeform='"+codeform+"' AND nosession="+lib_session+" ORDER BY nosession;";
    QSqlQuery query( requete);
    zdlParticipants->setNumRows(nbParticipant); // Création des lignes
    int ligne=0;
    // Affichage des enregistrements pour chaque donnée
    while ( query.next() )
    {
	zdlParticipants->setText(ligne,0,query.value(0).toString());
	zdlParticipants->setText(ligne,1,query.value(1).toString());
	zdlParticipants->setText(ligne,2,query.value(2).toString());
	zdlParticipants->setText(ligne,3,query.value(3).toString());
	ligne++;
    }
    
    chargeAutreParticipant();
}


void FormSession::misajour(int row, int col)
{
    // récupération des coordonées de la case à mettre à jour
    //int row=zdlParticipants->currentRow();
    //int col=zdlParticipants->currentColumn();
    if (col==0 or col==3)
    { // si c'est l'identifiant, on ne fait rien
	textLabelInfo->setText("ATTENTION : Impossible de modifier cette case...");
    }
    else
    {
	if (col==1 or col==2)
	{
	    QString requete="UPDATE stagiaire SET nomstagiaire='"+zdlParticipants->text(row,1)+"', prenomstagiaire='"+zdlParticipants->text(row,2)+"' WHERE nostagiaire='"+zdlParticipants->text(row,0)+"';";
	    QSqlQuery query( requete);
	    textLabelInfo->setText("Modification réalisée avec succès...");
	}
    }
    SurChangement();
}


void FormSession::chargeAutreParticipant()
{
    // Affichage des sessions pour la formation
    QString numpresent;
    for (int i=0; i<zdlParticipants->numRows();i++)
    {
	if (i==0)
	    numpresent=zdlParticipants->text(i,0);
	else
	    numpresent=numpresent+","+zdlParticipants->text(i,0);
    }
    QString requete="SELECT * FROM stagiaire WHERE nostagiaire NOT IN ("+numpresent+") ORDER BY nomstagiaire;";
    QSqlQuery query( requete);
    cbxNouveauParticipants->clear();
    nostagiaire.clear();
    while ( query.next() )
    {
	cbxNouveauParticipants->insertItem( query.value(1).toString() + " " + query.value(2).toString() );
	nostagiaire.append(query.value(0).toString());
    }
}


void FormSession::ajouterParticipant()
{
    int nb=cbxNouveauParticipants->currentItem();
    QString requete="INSERT INTO participer values ('"+cbxFormations->currentText()+"',"+cbxSessions->currentText()+","+nostagiaire[nb]+");";
    QSqlQuery query(requete);
    SurChangement();
}


void FormSession::supprimerParticipant()
{
    QString requete="DELETE FROM participer WHERE codeform='"+cbxFormations->currentText()+"' AND nosession="+cbxSessions->currentText()+" AND nostagiaire="+zdlParticipants->text(zdlParticipants->currentRow(),0)+";";
    QSqlQuery query(requete);
    SurChangement();
}
