<?php

namespace Garradin;

use Garradin\Plugin\RecusFiscaux\Activite;
use Garradin\Plugin\RecusFiscaux\Personne;
use Garradin\Plugin\RecusFiscaux\Tarif;
use Garradin\Plugin\RecusFiscaux\Utils;

// vérifier si l'année a bien été sélectionnée au préalable
$_SESSION['annee_recu'] = f('annee_recu');
if (! isset($_SESSION['annee_recu']) || $_SESSION['annee_recu'] == "") {
    \Garradin\Utils::redirect(PLUGIN_URL . 'index.php');
}

// champs pour le nom et prénom
$confNoms = (array) $plugin->getConfig('nomChamps');
uasort($confNoms, function ($a, $b)
{
    return $a->position - $b->position;
});
$champsNom = array();
foreach ($confNoms as $nom => $champ)
{
    if ($champ->position != 0) { $champsNom[] = $nom; }
}

// récupérer les infos du formulaire
$tarifsSelectionnes = f('tarifs') ?: [];

// taux de réduction associés
$tauxSelectionnes = array();
foreach ($tarifsSelectionnes as $idTarif) {
    $nomRadio = "taux_reduction_" . $idTarif;
    $valRadio = f("$nomRadio");
    $tauxSelectionnes[$idTarif] = $valRadio;
}
$_SESSION['tauxSelectionnes'] = $tauxSelectionnes;

// obtenir les instances de tarifs correspondant à la sélection
$lesTarifs = array();
foreach (Utils::getTarifs($tarifsSelectionnes) as $ot) {
    $lesTarifs[$ot->id] = Tarif::copier($ot);
}
$_SESSION['lesTarifs'] = $lesTarifs;

// activités correspondants aux tarifs sélectionnés
$lesActivites = array();
foreach (Utils::getActivites($tarifsSelectionnes) as $activite) {
    $lesActivites[$activite->id] = Activite::copier($activite);
}
$_SESSION['lesActivites'] = $lesActivites;

// versements correspondants aux tarifs sélectionnés
$_SESSION['lesVersements'] = Utils::getVersementsTarifs($_SESSION['annee_recu'],
                                                        $tarifsSelectionnes,
                                                        $champsNom);

// membres donateurs
$versementsMembres = Utils::getDonateurs($_SESSION['annee_recu'],
                                         $champsNom);
$membresDonateurs = array();
foreach ($versementsMembres as $versement) {
    $membresDonateurs[$versement->idUser] = new Personne($versement->idUser,
                                                         $versement->nom,
                                                         $versement->adresse,
                                                         $versement->codePostal,
                                                         $versement->ville);
}
$_SESSION['membresDonateurs'] = $membresDonateurs;

// ------------------------------------------------------------------------
// fonctions pour l'affichage

// afficher les informations d'une activité et d'un tarif
$tpl->register_function('afficher_debut_tarif', function ($params)
{
    $versement = $params['versement'];
    $idTarif = $versement->idTarif;
    $tarif = $_SESSION['lesTarifs'][$idTarif];
    $idActivite = $tarif->idActivite;
    $activite = $_SESSION['lesActivites'][$idActivite];

    $out = '<details open="open">
        <summary class="activite">';
    $out .= sprintf('
            <h3>Activité « %s »</h3>', $activite->label);
    if (!empty($activite->description)) {
        $out .= sprintf('
                <h4>%s</h4>', $activite->description);
    }
    $out .= sprintf('
                <h4>tarif « %s »', $tarif->label);
    if ($tarif->montant > 0) {
        $out .= sprintf(' montant : %.2f €', $tarif->montant/100);
    } else {
        $out .= ' montant : libre';
    }
    $out .= '</h4>
          </summary>';
    return $out;
});

// afficher les informations d'une personne
$tpl->register_function('afficher_debut_personne', function ($params)
{
    $versement = $params['versement'];
    $idUser = $versement->idUser;
    $personne = $_SESSION['membresDonateurs'][$idUser];
    $idVersement = $versement->idTarif . "_" . $versement->idUser; 
    $out = sprintf('<h3 class="personne">Versements de %s : <span id="total_%s">0,00 €</span></h3>',
                   $personne->nomPrenom,
                   $idVersement);
    $out .= sprintf('
            <fieldset class="versements" id="versements_%s">',
                    $idVersement);
    $out .= sprintf('
            <input type="checkbox" class="check_%s" id="check_%s"',
                    $idVersement,
                    $idVersement);
    $out .= sprintf(' onclick="cocherDecocherPersonne(check_%s, total_%s)" />',
                    $idVersement,
                    $idVersement);
    $out .= sprintf('
            <label for="check_%s">Cliquer pour cocher toutes les lignes</label>',
                    $idVersement);
    $out .= '<br />
             <hr>';
    return $out;
});

// afficher un versement
$tpl->register_function('afficher_versement', function ($params)
{
    $versement = $params['versement'];
    $rang = $params['rang'];
    $idVersement = $versement->idTarif . "_" . $versement->idUser; 
    $out = '<div class="';
    $out .= ($rang%2==0) ? 'pair">' : 'impair">';
    $out .= sprintf('
                <input type="checkbox" class="check_%s" id="check_%s_%s"
                    name="selected[]" value="%s"
                    onclick="cocherDecocherVersement(check_%s_%s, total_%s)" />
                <label for="check_%s_%s"></label>
                <span class="montant">%.2f</span>
                <span>%s</span>
            </div>',
                    $idVersement, $idVersement,
                    $rang, $rang,
                    $idVersement, $rang, $idVersement, $idVersement, $rang,
                    $versement->versement/100,
                    date_format(date_create($versement->date),"d/m/Y"));
    return $out;
});
// ------------------------------------------------------------------------

// préparation de l'affichage
$tpl->assign('lesActivites', $lesActivites);
$tpl->assign('lesTarifs', $lesTarifs);
$tpl->assign('lesVersements', $_SESSION['lesVersements']);
$tpl->assign('plugin_css', ['style.css']);

// envoyer au template
$tpl->display(PLUGIN_ROOT . '/templates/versements_activites.tpl');