<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Metier\GestStage ;
use Illuminate\Support\Facades\Input;

class PrivilegeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $gestStage ;
   
    public function __construct(GestStage $gestStage){
       $this->gestStage = $gestStage ;
    }
    public function index()
    {
        //Construction d'un tableau contient tous les privileges de l'enseignant
        $array_Privilege = $this->gestStage->getEnsPrivi();
        $struct_Privilege = $this->getEnsListPrivi($array_Privilege);
        $struct_Privilege = json_encode($struct_Privilege);
        //returnrn enseignant 
        $enseignants=$this->gestStage->getEnseignant("");
        //Return privileges 
        $privileges=$this->gestStage->getPrivileges();
        return view('listeEnsPrivi')
            ->with(compact("enseignants","privileges","struct_Privilege"));
    }
    

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $req,$enseignantId)
    {
       

        //On supprime tous les privileges ancien de l'enseignant
        $this->gestStage->deletePrivileges($enseignantId);
        //On attribue les nouvelles privileges a partir du formulaire saisit
        $privileges = $this->getInputs($req);

        //Update Table
        $this->gestStage->UpdatePrivileges($enseignantId,$privileges);
        
        return redirect('/privileges');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
     public function getInputs(Request $req){
         //Liste des medecins Observer dans la session 
        $privileges = $this->gestStage->getPrivileges();
        $privi = array();
        foreach ($privileges as $p) {
           if(Input::get($p->id_Privilege)=='yes'){
             array_push($privi,$p->id_Privilege);
           }
        }

        return $privi;
    }
    public function getEnsListPrivi($array_Privilege){
        $array= array();
        $i=0;
        while($i<count($array_Privilege)){

            $x=$array_Privilege[$i]['id_Enseignant'];
            
            $priv = array();
            while($i<count($array_Privilege) && $x==$array_Privilege[$i]['id_Enseignant']){
                array_push($priv,$array_Privilege[$i]['id_Privilege']);
                $i++;
            }  
            $array[$x]=$priv;
        }
        return $array;
    }
}
