<?php

namespace Controllers;

use Bases\Controller;
use Models\Categorie;
use Models\Commentaire;
use Models\Plat;
use Models\Tag;
use Utils\Upload;

class PlatsController extends Controller
{

    /**
     * Affiche la page principal
     *
     */
    public function index()
    {


        $modele_plat = new Plat;
        $entrees = $modele_plat->platsEntrees();
        $repas = $modele_plat->platsRepas();
        $desserts = $modele_plat->platsDessert();

        $modele_commentaire = new Commentaire;
        $commentaires = $modele_commentaire->commentaireAleatoire();

        // Inclure la vue index.view.php
        include("views/index.view.php");
    }

    /**
     * Traite l'ajout d'une activité
     */
    public function store()
    {
        // Protection de la route
        if (empty($_SESSION["utilisateur_id"])) {
            $this->rediriger("compte-connexion");
        }


        // Valider les champs
        if (
            empty($_POST["nom"]) ||
            empty($_POST["description"]) ||
            empty($_POST["prix"]) ||
            empty($_POST["categorie_id"]) ||
            empty($_POST["tags"])
        ) {
            $this->rediriger("plats-modifier?infos_requises");
        }

        $tags = $_POST['tags'];


        // Traitement de l'image
        $media = null;
        if (!empty($_FILES["media"])) {
            $upload = new Upload("media");
            $media = $upload->placerDans("uploads");
        }

        $succes = (new Plat)->inserer(
            $_POST["nom"],
            $_POST["description"],
            $_POST["prix"],
            $media,
            $_POST["categorie_id"]
        ) && (new Plat)->insererTags($tags);

        if (!$succes) {
            $this->rediriger("gestion?echec_creation_plat");
        }

        $this->rediriger("gestion?succes_creation_plat");
    }

    /**
     * Affiche le formulaire d'update
     *
     */
    public function edit()
    {

        // Valider
        if (empty($_GET["id"])) {
            $this->rediriger("gestion?id_inexistant");
        }

        $modele_categorie = new Categorie;
        $categories = $modele_categorie->toutesCategories();

        $modele_tags = new Tag;
        $tags = $modele_tags->tousTags();
        $tagsParPlat = $modele_tags->tagParPlat($_GET["id"]);

        $this->vue("gestion/editPlat", [
            "plat" => (new Plat)->platParId($_GET["id"]),
            "categories" => $categories,
            "tags" => $tags,
            "tagsParPlat" => $tagsParPlat,
        ]);
    }

    /**
     * Traite le formulaire de modification
     */
    public function update()
    {

        // Valider
        if (empty($_POST["id"])) {
            $this->rediriger("gestion?id_inexistant");
        }

        if (
            empty($_POST["nom"]) ||
            empty($_POST["description"]) ||
            empty($_POST["prix"]) ||
            empty($_POST["categorie_id"]) ||
            empty($_POST["tags"])
        ) {
            $this->rediriger("gestion?infos_requises");
        }

        $tags = $_POST['tags'];


        // Traitement de l'image
        $plat = (new Plat)->platParId($_POST["id"]);

        $upload = new Upload("media");
        $media = $upload->estValide() ? 
                     $upload->placerDans("uploads") :
                     $plat->image;

        $succes = (new Plat)->modifier(
            $_POST["id"],
            $_POST["nom"],
            $_POST["description"],
            $_POST["prix"],
            $media,
            $_POST["categorie_id"]
        ) && (new Plat)->supprimerTags($_POST["id"]) && (new Plat)->modifierTags($_POST["id"], $tags);

        if (!$succes) {
            $this->rediriger("gestion?echec_modification_plat");
        }

        $this->rediriger("gestion?succes_modification_plat");
    }

    /**
     * Traite la suppression d'un plat
     *
     */
    public function destroy()
    {

        // Protection de la route
        if (empty($_SESSION["utilisateur_id"])) {
            $this->rediriger("compte-connexion");
        }

        // Valider
        if (empty($_GET["id"])) {
            $this->rediriger("gestion?id_inexistant");
        }

        $succes = (new Plat)->supprimerTags($_GET["id"]) && (new Plat)->supprimer($_GET["id"]);

        if (!$succes) {
            $this->rediriger("gestion?echec_suppression_plat");
        }

        $this->rediriger("gestion?succes_suppression_plat");
    }
}
