<?php
/**
 * Created by PhpStorm.
 * User: p0wontnx
 * Date: 12/21/15
 * Time: 3:01 PM
 */

namespace App\Metier;


use App\Models\Encadrant_Service_Etudiant;
use App\Models\Enseignant;
use App\Models\Etudiant;
use App\Models\Service;
use App\Models\Stage;
use App\Models\Enseignant_Privilege;
use App\Models\Privilege;
use Illuminate\Support\Facades\DB;

class GestStage
{

    protected $enseignant;
    protected $etudiant;
    protected $encadrant_service_etudiant;

    function __construct()
    {
        $this->enseignant = new Enseignant();
        $this->etudiant = new Etudiant();
        $this->encadrant_service_etudiant = new Encadrant_Service_Etudiant();
    }

    public function affectServiceToEtudiants($id_Service, $etudiantIDs){

        foreach ($etudiantIDs as $etudiantID) {
            $this->affectService($id_Service,$etudiantID);
        }

    }

    public function affectService($id_Service, $id_Etudiant)
    {

        // create stage affectation
        $this->encadrant_service_etudiant->create([

            // id enseignant encadrant
            'id_Service' => $id_Service,

            // id etudiant concerne
            'id_Etudiant' => $id_Etudiant,

            // isValid = -1
            'isValid' => '-1' 

        ]);

    }

    public function UpdatevalidateServiceEtudiants($etudiantIds, $serviceId,$values)
    {
        $i = 0;
        foreach ($etudiantIds as $etudiantId) {
            $this->validateService($etudiantId, $serviceId,$values[$i]);
            $i++;
        }
    }

    public function validateService($id_Etudiant, $id_Service,$value)
    {

        // retrieve corresponding row
        $this->encadrant_service_etudiant = Encadrant_Service_Etudiant::where('id_Service', $id_Service)
            ->where('id_Etudiant', $id_Etudiant)
            ->firstOrFail();

        // set stage validation
        $this->encadrant_service_etudiant->isValid = $value;
        $this->encadrant_service_etudiant->save();


    }

    public function isStageValid($id_Etudiant, $id_Stage)
    {

        // retrieve all student stages
        $stagesAffecte = Encadrant_Service_Etudiant::join('Service', 'Service.id_Service', '=', 'Encadrant_Service_Etudiant.id_Service')
            ->select('isValid')
            ->where('id_Etudiant', $id_Etudiant)
            ->where('id_Stage', $id_Stage)
            ->get();

        foreach ($stagesAffecte as $stage) {
            if (!$stage->isValid)
                return false;
        }

        return true;

    }

    public function getEncadrants()
    {
        return Enseignant::join('Enseignant_Privilege', 'Enseignant_Privilege.id_Enseignant', '=', 'Enseignant.id_Enseignant')
            ->join('Privilege','Privilege.id_Privilege','=','Enseignant_Privilege.id_Privilege')
            ->where('Privilege.nom', '=', 'encadrant')
            ->select(DB::raw('id_Enseignant ,CIN ,    Enseignant.nom as nom_enc  , prenom  
                 '))
            ->get();
    }

    public function getStages()
    {
        return Service::all();
    }

    public function getEtudiants($rech)
    {
        return Etudiant::join('Niveau', 'Etudiant.id_Niveau', '=', 'Niveau.id_Niveau')
            ->where('Etudiant.nom','LIKE',$rech.'%')
            ->get();
    }

    public function getEtudiantNValid($encadrantId)
    {
       
        $serviceId =$this->getServiceEnseig($encadrantId) ;


        return Encadrant_Service_Etudiant::where('Encadrant_Service_Etudiant.id_Service', '=', $serviceId)
            ->join('Etudiant', 'Etudiant.id_Etudiant', '=', 'Encadrant_Service_Etudiant.id_Etudiant')
            ->where('Encadrant_Service_Etudiant.isValid', '=','0')
            ->orWhere('Encadrant_Service_Etudiant.isValid','=','-1')
            ->get();
    }

    public function getStagesWithNiveau()
    {
        return stage::join('Niveau', 'Niveau.id_Niveau', '=', 'Stage.id_Niveau')
            ->get();
    }
    public function getServices(){
        return Service::all();
    }
    public function getServiceEnseig($encadrantId){
        $encadrant = Enseignant::where('Enseignant.id_Enseignant','=',$encadrantId)->first();
        $serviceId = $encadrant->id_Service ;

        return $serviceId ;
    }
    public function getJurys(){
        return Enseignant::join('Enseignant_Privilege', 'Enseignant_Privilege.id_Enseignant', '=', 'Enseignant.id_Enseignant')
            ->join('Privilege','Privilege.id_Privilege','=','Enseignant_Privilege.id_Privilege')
            ->where('Privilege.nom','=','jury')
            ->select('Enseignant.*')
            ->get();
    }

    public function getEnseignant($rech){
        //Return enseignant Expect Admin
        return Enseignant::where('Enseignant.nom','LIKE',$rech.'%')
            ->orderBy('Enseignant.id_Enseignant')
            ->get();
        
    }

    public function getPrivileges(){
        return Privilege::all();
    }
    public function getEnsPrivi(){
        return Enseignant_Privilege::join('Enseignant','Enseignant.id_Enseignant','=','Enseignant_Privilege.id_Enseignant')
            ->orderBy('Enseignant.id_Enseignant')
            ->get();
    }
    public function UpdatePrivileges($enseignantId,Array $privileges){
        foreach ($privileges as $p) {
             $enseignant_privilege = new Enseignant_Privilege ;
             $enseignant_privilege->id_Enseignant = $enseignantId;
             $enseignant_privilege->id_Privilege= $p;
             $enseignant_privilege->save();
        }
    }
    public function deletePrivileges($enseignantId){
        Enseignant_Privilege::where('Enseignant_Privilege.id_Enseignant','=',$enseignantId)
            ->delete();
    }

}