<?php

/**
 * OBJECT QUI GERE LA PARTIE AFFICHAGE ET MODIFICATION D'UNE FICHE 
 */

class Class_newFiche
{
    private $pdo = null;
    private $month;
    private $userId;

    public function __construct(PdoGsb $pDO, string $userId, string $month)
    {
        $this->pdo = $pDO->getPdoGsb();
        $this->month = $month;
        $this->userId = $userId;

        $this->existingFile();
    }

    /**
     * Test et ajoute la fiche si elle n'existe pas dans remboursement 
     * et les ligneForfaitaires nulles liées
     */
    private function existingFile(): void
    {
        $req = 'SELECT newremboursement(:idUser, :month); ';

        $result = $this->pdo->prepare($req);
        $result->bindParam('idUser', $this->userId);
        $result->bindParam('month', $this->month);
        $result->execute();
    }

    /**
     * Liste les frais forfaitaires
     */
    public function listFraisForfaitaires(): array
    {
        $req = 'SELECT "fLibelle", NULL AS "lfQuantite", round("fMontant", 2) AS "fMontant", 0 AS "fTotal", "fId" FROM forfait';
        $result = $this->pdo->prepare($req);
        $result->execute();

        return $result->fetchAll();
    }

    /**
     * Liste les frais Forfetaires d'un user pour 1 mois
     */
    public function listFraisForfaitForU(): array
    {
        $req = 'SELECT "fLibelle", "lfQuantite", ROUND("fMontant", 2) AS "fMontant", ROUND("lfQuantite" * "fMontant", 2) AS "fTotal", "fId"
            FROM remboursement
            INNER JOIN "ligne_forfait" ON "rVisiteur" = "lfVisiteur" AND "rMois" = "lfMois"
            INNER JOIN "forfait" ON "fId" = "lfForfait"
            WHERE "rVisiteur" = :idUser AND "rMois" = :monthF;';

        $result = $this->pdo->prepare($req);
        $result->bindParam('idUser', $this->userId);
        $result->bindParam('monthF', $this->month);
        $result->execute();

        return $result->fetchAll();
    }

    /**
     * Liste les frais hors forfait d'un user pour 1 mois
     */
    public function listFraisHF(): array
    {
        $req = 'SELECT "lhId", to_char("lhDate", \'YYYY-mm-dd\') AS "lhDate", 
            "lhLibelle", ROUND("lhMontant",2) as "lhMontant", 
            "lhJustificatif", "lhRefus"
            FROM remboursement 
            inner join "ligne_hors_forfait" on "rVisiteur" = "lhVisiteur" 
            AND "rMois" = "lhMois"
            
            WHERE "rVisiteur" = :idVisiteur AND "rMois" = :mois 
            ORDER BY "lhDate";';

        $result = $this->pdo->prepare($req);
        $result->bindParam(':idVisiteur', $this->userId);
        $result->bindParam(':mois', $this->month);
        $result->execute();

        return $result->fetchAll();
    }

    /**
     * Fonction qui renvoie le prix total validé d'une fiche de frais
     * 
     * AJOUTER UNE FONCTION QUI CREE LA FICHE DE FRAIS LORS DU SELECT SI CELLE CI N'EXISTE PAS 
     */
    public function getMontantValide(): float
    {
        $req = 'SELECT "rMontantValide"
        FROM remboursement 
        WHERE "rVisiteur" = :idVisiteur AND "rMois" = :mois ;';

        $result = $this->pdo->prepare($req);
        $result->bindParam(':idVisiteur', $this->userId);
        $result->bindParam(':mois', $this->month);
        $result->execute();

        return $result->fetch()['rMontantValide'];
    }

    /**
     * RETOURNE LE STATUS DE LA FICHE 
     */
    public function getStatus(): string
    {
        $req = 'select etat."eId" from remboursement 
        INNER JOIN etat on etat."eId" = remboursement."rEtat"
        WHERE "rVisiteur" = :idVisiteur AND "rMois" = :mois ;';

        $result = $this->pdo->prepare($req);
        $result->bindParam(':idVisiteur', $this->userId);
        $result->bindParam(':mois', $this->month);
        $result->execute();

        return $result->fetch()['eId'];
    }

    /**
     * UPDATE LES INFOS DE LA FICHE
     */
    public function updateFile(int $nbJustif, float $mttValid): bool
    {
        $req = 'UPDATE remboursement
            SET "rNbJustificatifs" = :nbJustif,
            "rMontantValide" = :mttValid,
            "rDateModif" = NOW()
            WHERE "rVisiteur" = :idVisiteur AND "rMois" = :mois;';

        $result = $this->pdo->prepare($req);

        $result->bindParam(':nbJustif', $nbJustif);
        $result->bindParam(':mttValid', $mttValid);
        $result->bindParam(':idVisiteur', $this->userId);
        $result->bindParam(':mois', $this->month);

        return $result->execute();
    }

    /**
     * AJOUTE LES LIGNES HF
     */
    public function addFraisHF(string $libelle, string $date, float $montant): bool
    {
        $req = 'SELECT MAX("lhId")+1
        FROM ligne_hors_forfait';
        $result = $this->pdo->prepare($req);
        $result->execute();
        $idLigne = $result->fetch()[0];

        $req = 'INSERT INTO ligne_hors_forfait
        VALUES (:idLigne, :userId, :monthF, :libelle, :dateL, :mttF, \'true\', \'false\');';
        $result = $this->pdo->prepare($req);
        $result->bindParam(':idLigne', $idLigne);
        $result->bindParam(':libelle', $libelle);
        $result->bindParam(':dateL', $date);
        $result->bindParam(':mttF', $montant);
        $result->bindParam(':userId', $this->userId);
        $result->bindParam(':monthF', $this->month);

        return $result->execute();
    }

    /**
     * Refuse ou accepte un frais HF
     */
    public function accceptFrais(int $idFrais, bool $state)
    {
        $req = 'UPDATE ligne_hors_forfait
        SET "lhRefus" = :stateF
        WHERE "lhId" = :idFrais;';
        $result = $this->pdo->prepare($req);
        $state = ($state) ? 'false' : 'true';
        $result->bindParam(':stateF', $state);
        $result->bindParam(':idFrais', $idFrais);

        return $result->execute();
    }

    /**
     * UPDATE LA LIGNE FRAIS FORFAITAIRES 
     */
    public function updateFraisF(int $qttF, int $montant, string $idForfait): bool
    {
        $req = 'UPDATE ligne_forfait
        SET "lfQuantite" = :qttF,
        "lfMontant" = :mttF
        WHERE "lfVisiteur" = :userId AND "lfMois" = :monthF AND "lfForfait" = :idForfait;';
        $result = $this->pdo->prepare($req);
        $result->bindParam(':qttF', $qttF);
        $result->bindParam(':mttF', $montant);
        $result->bindParam(':idForfait', $idForfait);
        $result->bindParam(':userId', $this->userId);
        $result->bindParam(':monthF', $this->month);

        return $result->execute();
    }

    /**
     * SUPPRIME LA LIGNE HF
     */
    public function suprLigneHF(int $idLine): bool
    {
        $req = 'DELETE FROM ligne_hors_forfait
        WHERE "lhId" = :idLine';
        $result = $this->pdo->prepare($req);
        $result->bindParam(':idLine', $idLine);

        return $result->execute();
    }
}
