<?php

namespace App\controleur;
use App\config\Validation;
use App\gateway\ImageManager;
use App\modele\MembreModele;

class MembreControleur extends UtilisateurControleur
{
    public function __construct()
    {
    }

    public function deconnexion()
    {
        global $twig;
        session_unset();
        session_destroy();

        header("Location: /SAE_2A_FA-Reseau_ALICA/php/");
        exit();
    }
    public function createOfferForm()
    {
        global $twig;
        echo $twig->render("CreerOffre.html", []);
    }

    public function createOffer()
    {
        global $twig;
        $taberror = [];

        $requiredFields = ['name', 'entreprise', 'description','typeContrat', 'descriptPoste', 'profilRecherche', 'choixExp', 'education', 'mail', 'num'];

        $error = false;
        foreach ($requiredFields as $field) {
            if (empty($_POST[$field])) {
                $error = true;
                $taberror[] = "Le champ {$field} est requis !";
            }
        }
        if($error)
        {
            echo $twig->render("CreerOffre.html", ['tabError' => $taberror ]);
            return;
        }

        if(!Validation::verifierEmail($_POST["mail"]))
        {
            //echo $twig->render("CreerOffre.html", ['errMsg' => "Email non valide !" ]);
            $taberror[] = "Email non valide !";
        }

        if(!Validation::validateNumber($_POST["num"]))
        {
            $taberror[] = "Numero non valide !";
            //echo $twig->render("CreerOffre.html", ['errMsg' => "Numero non valide !" ]);
        }


        if (!Validation::validateImage("image"))
        {
            if(isset($_FILES['image']['name'])) {$taberror[] = "Image non valide !";}
            else {$taberror[] = "Inserez une image !";}
        }
        if(!Validation::validateImage("logo")) {
            if(isset($_FILES['logo']['name'])) {$taberror[] = "Logo non valide !";}
            else {$taberror[] = "Inserez un logo !";}
            return;
        }

        if(count($taberror) > 0)
        {
            echo $twig->render("CreerOffre.html", ['tabError' => $taberror ]);

        }
        else{
            $saveImg1 = ImageManager::SaveImage("image");
            $saveImg2 = ImageManager::SaveImage("logo");
            if($saveImg1[0] && $saveImg2[0]) {
                $mbrModel = new MembreModele();


                $offre = $mbrModel->publishOffer($saveImg1[1], $saveImg2[1]);

                echo $twig->render("OffreDetailTest.html", ['offre' => $offre]);
            }
            else
            {
                $taberror[] = "Erreur lors de l'upload des images";
                echo $twig->render("CreerOffre.html", ['tabError' => $taberror ]);
            }
        }
    }

    protected function proposerOffre()
    {
        //TODO
    }

    protected function consulterProfil()
    {
        //TODO
    }

    protected function modifierProfil()
    {
        //TODO
    }

    protected function signaler()
    {
        //TODO
    }
}