/////////////////////////////////////////////////
// STAGE NATIONAL
// [DOMAIN]/stagenational
/////////////////////////////////////////////////

/////////////////////////////////////////////////
// LOGGING
/////////////////////////////////////////////////
const logger = require("../public/js/app/logger_stagenat");

/////////////////////////////////////////////////
// APPELS DES MODULES
/////////////////////////////////////////////////
const path = require("path");
const express = require('express');
const router = express.Router();
// CALCUL DE DATE
const moment = require('moment');
moment.locale('fr');
let now = moment().format("YYYY-MM-DD HH:mm:ss");

////////////////////////////////////////////////////
// FILE NAME
////////////////////////////////////////////////////
const filename = path.parse(__filename).base;

/////////////////////////////////////////////////
// APPELS DES FONCTIONS
/////////////////////////////////////////////////

const SN_checkidentifiant = require("../controllers/stage_national/stn_identifierController");
const SN_checkinfos = require("../controllers/stage_national/stn_formulaireController");
const post_payer_accompte = require("../controllers/stage_national/stn_payeracompteController");
const SN_retour_stripe = require("../controllers/stage_national/stn_retourstripeController");

/////////////////////////////////////////////////
// 1 - PAGE CHECK IDENTIFIANT

// AFFICHAGE DE LA PAGE
// URL du stage donné au public /stagenational pour une url facile à mémoriser
// derrière on dirige vers la page d'identification
router.get('/', function(req, res) {
    // rediriger vers la page de saisie de l'identifiant
    res.redirect('/stagenational/identifier');
});
router.get('/identifier', function(req, res) {
    //if (req.session.err_mail_form_adhesion_1) {
        res.locals.err_mail_form_adhesion_1 = req.session.err_mail_form_adhesion_1;
        res.locals.err_mail_form_adhesion_2 = req.session.err_mail_form_adhesion_2;
        res.locals.err_mail_form_adhesion_3 = req.session.err_mail_form_adhesion_3;
        // on supprime le contenu après affichage
        req.session.err_mail_form_adhesion_1 = undefined;
        req.session.err_mail_form_adhesion_2 = undefined;
        req.session.err_mail_form_adhesion_3 = undefined;
    //}

    // DATE LIMITE D'INSCRIPTION
    // Date de process.env.sn_date_limite non incluse dans la période d'inscription
    if (now > process.env.sn_date_limite) {
        logger.info("DATE LIMITE DEPASSÉE", { numsession: req.session.id, label: filename});
        req.session.datelimitedepass = "oui";
        res.locals.datelimitedepass = "oui";
    } else {
        logger.info("DATE LIMITE NON DEPASSÉE", { numsession: req.session.id, label: filename});
        req.session.datelimitedepass = "non";
        res.locals.datelimitedepass = "non";
    }

    // affichage du template ejs /views/page_templates/inscription_accueil.ejs
    res.render('stagenat/page_templates/inscrip_formulaire_identifier');
});

// SCRIPT SUR CLIC "S'INSCRIRE" POUR CHECKER IDENTIFIANT
router.post('/identifier', SN_checkidentifiant.SN_checkidentifiant);



/////////////////////////////////////////////////
// 2 - SAISIE DU FORMULAIRE D'INSCRIPTION AU STAGE

router.get('/formulaire', function(req, res) {
    if (req.session.err_mineur == "oui") {
        res.locals.err_mineur = 'Vous ne pouvez pas inscrire un enfant de moins de 14 ans (âge au premier jour du stage) sans adulte pour l\'accompagner.';
        req.session.err_mineur = "non";
    }
    if (req.session.err_campado_capa == "oui") {
        res.locals.err_campado_capa = "Il n'y a plus de place disponible pour inscrire une personne en Camp Ado";
        req.session.err_campado_capa = "non";
    }

    logger.info("res.locals.err_campado_capa : " + res.locals.err_mineur, { numsession: req.session.id, label: filename});
    logger.info("req.session.err_campado_capa : " + req.session.err_campado_capa, { numsession: req.session.id, label: filename});

    res.locals.identifiant = req.session.identifiant;
    logger.info("req.session.association_suzuki : " + req.session.association_suzuki);
    res.locals.association_suzuki = req.session.association_suzuki;

    // INITIALISÉ LES VARIABLES DE SESSION AVANT DE REMPLIR LE FORMULAIRE
    req.session.adherent_nom = "";
    req.session.adherent_prenom = "";
    req.session.adherent_adresse_rue = "";
    req.session.adherent_adresse_complement = "";
    req.session.adherent_adresse_codepostal = "";
    req.session.adherent_adresse_ville = "";
    req.session.adherent_adresse_pays = "";
    req.session.adherent_telephone_mobile = "";
    req.session.adherent_telephone_fixe = "";
    req.session.adherent_mail = "";
    req.session.hebergement = "";
    req.session.commentaire_adherent = "";
    req.body.adherent_nom = "";
    req.body.adherent_prenom = "";
    req.body.adherent_adresse_rue = "";
    req.body.adherent_adresse_complement = "";
    req.body.adherent_adresse_codepostal = "";
    req.body.adherent_adresse_ville = "";
    req.body.adherent_adresse_pays = "";
    req.body.adherent_telephone = "";
    req.body.adherent_mail = "";
    req.body.hebergement = "";
    req.body.commentaire_adherent = "";
    for (let toto = 1; toto < 7; toto++) {
        req.session[`personne_${toto}_nom`] = "";
        req.session[`personne_${toto}_prenom`] = "";
        req.session[`personne_${toto}_prise_charge_nom`] = "";
        req.session[`personne_${toto}_datenaissance`] = "";
        req.session[`personne_${toto}_transport_mode`] = "";
        req.session[`personne_${toto}_transport_arret`] = "";
        req.session[`personne_${toto}_role`] = "";
        req.session[`personne_${toto}_profil`] = "";
        req.session[`personne_${toto}_instrument`] = "";
        req.session[`personne_${toto}_instrument_ensemble`] = "";
        req.session[`personne_${toto}_instrument_collectif`] = "";
        req.session[`personne_${toto}_cahier`] = "";
        req.session[`personne_${toto}_piece`] = "";
        req.session[`personne_${toto}_prof`] = "";
        req.session[`personne_${toto}_mail_prof`] = "";
        req.session[`personne_${toto}_option_coursindividuel`] = "";
        req.session[`personne_${toto}_option_decouvrealto`] = "";
        req.session[`personne_${toto}_option_concertsolo_cahier`] = "";
        req.session[`personne_${toto}_option_concertsolo_piece`] = "";
        req.session[`personne_${toto}_activite_1`] = "";
        req.session[`personne_${toto}_activite_2`] = "";
        req.session[`personne_${toto}_age`] = undefined;
        req.session[`personne_${toto}_opt_chantchoral`] = "";
        req.session[`personne_${toto}_opt_dalcroze`] = "";
        req.session[`personne_${toto}_opt_eveil`] = "";
        req.session[`personne_${toto}_opt_collectif`] = "";
        req.session[`personne_${toto}_opt_ensemble`] = "";
        req.session[`personne_${toto}_opt_particulier`] = "";

        for (let i = 1; i <= 5; i++) {
            req.session[`personne_${toto}_activite_${i}`] = "";
            req.body[`personne_${toto}_activite_${i}`] = "";
        }
    
        req.body[`personne_${toto}_nom`] = "";
        req.body[`personne_${toto}_prenom`] = "";
        req.body[`personne_${toto}_prise_charge_nom`] = "";
        req.body[`personne_${toto}_datenaissance`] = "";
        req.body[`personne_${toto}_transport_mode`] = "";
        req.body[`personne_${toto}_transport_arret`] = "";
        req.body[`personne_${toto}_role`] = "";
        req.body[`personne_${toto}_profil`] = "";
        req.body[`personne_${toto}_instrument`] = "";
        req.body[`personne_${toto}_instrument_ensemble`] = "";
        req.body[`personne_${toto}_instrument_collectif`] = "";
        req.body[`personne_${toto}_cahier`] = "";
        req.body[`personne_${toto}_piece`] = "";
        req.body[`personne_${toto}_prof`] = "";
        req.body[`personne_${toto}_mail_prof`] = "";
        req.body[`personne_${toto}_option_coursindividuel`] = "";
        req.body[`personne_${toto}_option_decouvrealto`] = "";
        req.body[`personne_${toto}_option_concertsolo_cahier`] = "";
        req.body[`personne_${toto}_option_concertsolo_piece`] = "";
        req.body[`personne_${toto}_activite_1`] = "";
        req.body[`personne_${toto}_activite_2`] = "";
    }

    // affichage du template ejs /views/page_templates/inscription_accueil.ejs
    res.render('stagenat/page_templates/inscrip_formulaire');
});

// SCRIPT SUR CLIC "VALIDER" APRÈS AVOIR REMPLI LE FORMULAIRE
router.post('/formulaire', SN_checkinfos.SN_checkinfos);

/////////////////////////////////////////////////
// 3 - AFFICHER PAGE RÉCAP

router.get('/recap', function(req, res) {
    req.session.onpeutcontinuer = "non";
    res.locals.identifiant = req.session.identifiant;
    res.locals.association_suzuki = req.session.association_suzuki;
    if (req.session.hebergement == 'flocons verts partagé') {
        res.locals.hebergement = "Centre les Flocons Verts - hébergement partagé";
    }
    if (req.session.hebergement == 'flocons verts non partagé') {
        res.locals.hebergement = "Centre les Flocons Verts - hébergement non partagé";
    }
    if (req.session.hebergement == 'independant') {
        res.locals.hebergement = "indépendant";
    }

    for (let toto = 1; toto < 7; toto++) {
        if (req.session["personne_"+toto+"_nom"]) {
            res.locals["personne_"+toto+"_numero"] = toto;
            res.locals["personne_"+toto+"_nom"] = req.session["personne_"+toto+"_nom"];
            res.locals["personne_"+toto+"_prenom"] = req.session["personne_"+toto+"_prenom"];
            res.locals["personne_"+toto+"_prise_charge_nom"] = req.session["personne_"+toto+"_prise_charge_nom"];
            res.locals["personne_"+toto+"_profilage"] = req.session["personne_"+toto+"_profilage"];
            
            // AFFICHER ACTIVITÉ
            if (req.session["personne_"+toto+"_role"] === 'accompagnant_activite_adulte') {
                res.locals["personne_"+toto+"_role"] = "accompagnant adulte avec activité";
                res.locals["personne_"+toto+"_categorie"] = "accompagnant adulte avec activité";
            } else if (req.session["personne_"+toto+"_role"] === 'accompagnant') {
                res.locals["personne_"+toto+"_role"] = "accompagnant sans activité";
                res.locals["personne_"+toto+"_categorie"] = "accompagnant sans activité";
            } else if (req.session["personne_"+toto+"_role"] === 'accompagnant_activite_enfant') {
                res.locals["personne_"+toto+"_role"] = "Accompagnant enfant avec activité";
                res.locals["personne_"+toto+"_categorie"] = "Accompagnant enfant avec activité";
            } else if (req.session["personne_"+toto+"_role"] === 'instrumentiste_camp') {
                res.locals["personne_"+toto+"_role"] = "instrumentiste";
                res.locals["personne_"+toto+"_categorie"] = "instrumentiste";
            } else if (req.session["personne_"+toto+"_role"] === 'prof_invite') {
                res.locals["personne_"+toto+"_role"] = "Professeur invité";
                res.locals["personne_"+toto+"_categorie"] = "Professeur invité";
            } else {
                res.locals["personne_"+toto+"_role"] = req.session["personne_"+toto+"_role"];
                res.locals["personne_"+toto+"_categorie"] = req.session["personne_"+toto+"_role"];
            }

            if (req.session["personne_"+toto+"_profil"] == 'professeur') {
                res.locals["personne_"+toto+"_profil"] = "professeur";
            }
            if (req.session["personne_"+toto+"_profil"] == 'non_professeur') {
                res.locals["personne_"+toto+"_profil"] = "externe";
            }
            if (req.session["personne_"+toto+"_profil"] == 'Camp ado') {
                res.locals["personne_"+toto+"_profil"] = "Camp ado";
            }
            
            // AFFICHER MODE DE TRANSPORT
            res.locals["personne_"+toto+"_transport_mode"] = req.session["personne_"+toto+"_transport_mode"];
            if (req.session["personne_"+toto+"_transport_arret"] == 'arret_7_quai_lassagne_lyon') {
                res.locals["personne_"+toto+"_transport_arret"] = "7 quai Lassagne Lyon";
            }
            if (req.session["personne_"+toto+"_transport_arret"] == 'arret_partdieu_lyon') {
                res.locals["personne_"+toto+"_transport_arret"] = "Gare Part Dieu Lyon";
            }

            // AFFICHER INSTRUMENT
            res.locals["personne_"+toto+"_instrument"] = req.session["personne_"+toto+"_instrument"];
            res.locals["personne_"+toto+"_instrument_ensemble"] = req.session["personne_"+toto+"_instrument_ensemble"];
            res.locals["personne_"+toto+"_instrument_collectif"] = req.session["personne_"+toto+"_instrument_collectif"];
            res.locals["personne_"+toto+"_cahier"] = req.session["personne_"+toto+"_cahier"];
            res.locals["personne_"+toto+"_option_concertsolo_cahier"] = req.session["personne_"+toto+"_option_concertsolo_cahier"];
            res.locals["personne_"+toto+"_option_concertsolo_piece"] = req.session["personne_"+toto+"_option_concertsolo_piece"];
            res.locals["personne_"+toto+"_option_decouvrealto"] = req.session["personne_"+toto+"_option_decouvrealto"];


            // AFFICHER COUT
            res.locals["personne_"+toto+"_cout_hebergement"] = req.session["personne_"+toto+"_cout_hebergement"];
            res.locals["personne_"+toto+"_cout_transport"] = req.session["personne_"+toto+"_cout_transport"];
            res.locals["personne_"+toto+"_eveil_cout"] = req.session["personne_"+toto+"_eveil_cout"];
            res.locals["personne_"+toto+"_dalcroze_cout"] = req.session["personne_"+toto+"_dalcroze_cout"];
            res.locals["personne_"+toto+"_chant_cout"] = req.session["personne_"+toto+"_chant_cout"];
            res.locals["personne_"+toto+"_observation_cout"] = req.session["personne_"+toto+"_observation_cout"];
            res.locals["personne_"+toto+"_cours_cout"] = req.session["personne_"+toto+"_cours_cout"];
            res.locals["personne_"+toto+"_coursindividuel_cout"] = req.session["personne_"+toto+"_coursindividuel_cout"];
            res.locals["personne_"+toto+"_option_decouvrealto_cout"] = req.session["personne_"+toto+"_option_decouvrealto_cout"];
            res.locals["personne_"+toto+"_collectif_cout"] = req.session["personne_"+toto+"_collectif_cout"];
            res.locals["personne_"+toto+"_ensemble_cout"] = req.session["personne_"+toto+"_ensemble_cout"];
            res.locals["personne_"+toto+"_particulier_cout"] = req.session["personne_"+toto+"_particulier_cout"];
            
        }
    }
    res.locals.frais_dossier = req.session.frais_dossier
    res.locals.cout_total = req.session.cout_total;
    res.locals.remise = req.session.remise;
    res.locals.accompte_frais_bancaire = req.session.accompte_frais_bancaire;
    res.locals.montant_accompte = req.session.montant_accompte;
    res.locals.cout_total_dalcroze_surplace = req.session.cout_total_dalcroze_surplace;
    res.locals.cout_supplement_hebergement = req.session.cout_supplement_hebergement;
    if (req.session.montant_accompte > 1) {
        res.render('stagenat/page_templates/recap');    
    }
    if (req.session.montant_accompte < 1) {
        res.render('stagenat/page_templates/recapssacompte');    
    }
    
});


// PAGE RÉCAP - CLICK SUR LE BOUTON MODIFIER
// EN CAS DE LIEN avec plusieurs / comme /stagenational/recap, il faut rappeler le premier (ici /stagenational)
// voir l'URL du POST dans l'onglet network de Chrome (type = document)
router.post('/post_recap_modifier', function(req, res){
    res.redirect('/stagenational/identifier');
});

// PAGE RÉCAP - CLICK SUR LE BOUTON PAYER ACCOMPTE
router.post('/post_payer_accompte', post_payer_accompte.post_payer_accompte);

// PAGE RÉCAP - CLICK SUR LE BOUTON ENREGISTRER
//router.post('/post_enregistrer_inscrip_ssacompte', post_enregistrer_inscrip_ssacompte);
router.post('/post_enregistrer_inscrip_ssacompte', post_payer_accompte.post_payer_accompte);

/////////////////////////////////////////////////
// 4 - PAIEMENT DE L'ACOMPTE

router.get('/paiement_reussi', SN_retour_stripe.SN_accompte_reussi);
router.get('/paiement_annule', SN_retour_stripe.SN_accompte_annule);

/////////////////////////////////////////////////
// 4 - SANS ACOMPTE INSCRIPTION RÉUSSIE
router.get('/inscription_reussi', function(req, res) {

    res.locals.identifiant = req.session.identifiant;
    logger.info("req.session.association_suzuki : " + req.session.association_suzuki);
    res.locals.association_suzuki = req.session.association_suzuki;
    res.locals.adherent_nom = req.session.adherent_nom;
    res.locals.adherent_prenom = req.session.adherent_prenom;
    res.locals.adherent_adresse_rue = req.session.adherent_adresse_rue;
    res.locals.adherent_adresse_complement = req.session.adherent_adresse_complement;
    res.locals.adherent_adresse_codepostal = req.session.adherent_adresse_codepostal;
    res.locals.adherent_adresse_ville = req.session.adherent_adresse_ville;
    res.locals.adherent_adresse_pays = req.session.adherent_adresse_pays;
    res.locals.adherent_telephone_mobile = req.session.adherent_telephone_mobile;
    res.locals.adherent_telephone_fixe = req.session.adherent_telephone_fixe;
    res.locals.adherent_mail = req.session.adherent_mail;
    res.locals.hebergement = req.session.hebergement;
    res.locals.commentaire_adherent = req.session.commentaire_adherent;



    for (let toto = 1; toto < 7; toto++) {
        res.locals["personne_"+toto+"_numero"] = toto;
        res.locals["personne_"+toto+"_nom"] = req.session["personne_"+toto+"_nom"];
        res.locals["personne_"+toto+"_prenom"] = req.session["personne_"+toto+"_prenom"];
        res.locals["personne_"+toto+"_prise_charge_nom"] = req.session["personne_"+toto+"_prise_charge_nom"];
        res.locals["personne_"+toto+"_profilage"] = req.session["personne_"+toto+"_profilage"];
        res.locals["personne_"+toto+"_categorie"] = req.session["personne_"+toto+"_categorie"];
        res.locals["personne_"+toto+"_profil"] = req.session["personne_"+toto+"_profil"];
        res.locals["personne_"+toto+"_transport_mode"] = req.session["personne_"+toto+"_transport_mode"];
        res.locals["personne_"+toto+"_transport_arret"] = req.session["personne_"+toto+"_transport_arret"]
        res.locals["personne_"+toto+"_instrument"] = req.session["personne_"+toto+"_instrument"];
        res.locals["personne_"+toto+"_instrument_ensemble"] = req.session["personne_"+toto+"_instrument_ensemble"];
        res.locals["personne_"+toto+"_instrument_collectif"] = req.session["personne_"+toto+"_instrument_collectif"];
        res.locals["personne_"+toto+"_cahier"] = req.session["personne_"+toto+"_cahier"];
        res.locals["personne_"+toto+"_option_coursindividuel"] = req.session["personne_"+toto+"_option_coursindividuel"];
        res.locals["personne_"+toto+"_option_concertsolo"] = req.session["personne_"+toto+"_option_concertsolo"];
        res.locals["personne_"+toto+"_activite_1"] = req.session["personne_"+toto+"_activite_1"];
        res.locals["personne_"+toto+"_activite_2"] = req.session["personne_"+toto+"_activite_2"];
        res.locals["personne_"+toto+"_cout_hebergement"] = req.session["personne_"+toto+"_cout_hebergement"];
        res.locals["personne_"+toto+"_cout_transport"] = req.session["personne_"+toto+"_cout_transport"];
        res.locals["personne_"+toto+"_activite_cout"] = req.session["personne_"+toto+"_activite_cout"];
        res.locals["personne_"+toto+"_observation_cout"] = req.session["personne_"+toto+"_observation_cout"];
        res.locals["personne_"+toto+"_cours_cout"] = req.session["personne_"+toto+"_cours_cout"];
    }
    res.locals.cout_total = req.session.cout_total;
    res.locals.frais_dossier = req.session.frais_dossier;
    res.locals.accompte_frais_bancaire = req.session.accompte_frais_bancaire;
    res.locals.montant_accompte = req.session.montant_accompte;
    res.locals.cout_total_dalcroze_surplace = req.session.cout_total_dalcroze_surplace;
    
    


    // affichage du template ejs /views/page_templates/inscription_accueil.ejs
    res.render('stagenat/page_templates/inscription_reussi');
});



/////////////////////////////////////////////////
// EXPORT DE MODULE POUR QU'IL SOIT LU DANS LES AUTRES FICHIERS JS
/////////////////////////////////////////////////
module.exports = router;