<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Metier\GestSession ;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use App\Models\Session ;
use Illuminate\Http\Response as HttpResponse;


class SessionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    protected $gestSession ;
   
    public function __construct(GestSession $gestSession){
       $this->gestSession = $gestSession ;
    }
     public function index($sessionId=null){   
        
        $niveaux=$this->gestSession->getNiveaux();
        $medecin_observers = $this->gestSession->getMedObserver("");
        if(!$sessionId){    
            return view('createSession')
                ->with(compact('niveaux','medecin_observers','error')) ;
        }else {
            $session = $this->gestSession->getInfoSession($sessionId);
            $med_obs = $this->gestSession->getMedObserSession($sessionId);
            $array=array();
            $i=0;
            foreach ($medecin_observers as $med) {
                foreach ($med_obs as $s) {
                    if($s->id_Enseignant==$med->id_Enseignant){
                        $array[$i]=true;
                        break;
                    }
                   
                }
               if(count($array)==$i){
                 $array[$i]=false;
               }
                $i++;
            }

            return view('updateSession')->with(compact('niveaux','medecin_observers','session','array'));
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() //Creation d'un nouvel Session 
    {
      

        
    }

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



        $nom = $this->recuper_nomSession($request);

        $id_Niveau = $request->input('id_niveau');


        $medSess = $this->recuper_ListMedSession($request);
       
        $patient = $this->recuper_ListPatientSession($request);
        

        //$error = $this->verif($date,$id_Niveau,$medSess,$patient);
        
        
            $session=$this->gestSession->createSession($nom,$id_Niveau);
            if(!is_null($session)){
                $this->gestSession->affectMedSession($medSess,$session->id_Session);
                $this->gestSession->affectPatientsSimule($patient,$session->id_Session);
                $sessions = $this->gestSession->getSessions();
                return redirect('/session');
            }else {
                return response()->json(['error' => 'Session exist'], HttpResponse::HTTP_NOT_FOUND) ;
            }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
      public function show($sessionId = null)
      {
        if (is_null($sessionId)) {
            // display the hole session
            $sessions=$this->gestSession->getSessions();
            return view('sessionView',compact('sessions'));

        } else {
            // display specific session
            $session = $this->gestSession->getSessionWithId($sessionId);
            return is_null($session) ?
                response()->json(['error' => 'Session not found'], HttpResponse::HTTP_NOT_FOUND) : $session;
        }
      }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request,$sessionId)
    {
         
        $nom = $this->recuper_nomSession($request);

        $id_Niveau = $request->input('id_niveau');


        $medSess = $this->recuper_ListMedSession($request);
       
        $patient = $this->recuper_ListPatientSession($request);
        

        //$error = $this->verif($date,$id_Niveau,$medSess,$patient);
        
        
            $session=$this->gestSession->updateSession($sessionId,$nom,$id_Niveau);
            $this->gestSession->destroyMedObserver($sessionId);
            $this->gestSession->affectMedSession($medSess,$sessionId);
            $this->gestSession->affectPatientsSimule($patient,$sessionId);
            return redirect('/session');
          
    }

    /**
     * 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($sessionId)
    {
         $this->gestSession->destroySession($sessionId);
         return redirect('/session');
    }

    public function UpdateSession(){
        return view('ButtonPMModif');
    }
     //Function Controllers 
    public function recuper_ListMedSession(Request $request){
         //Liste des medecins Observer dans la session 
        $medecin_observers = $this->gestSession->getMedObserver("");
        $medSess = array();
        foreach ($medecin_observers as $observer) {
           if(Input::get($observer->id_Enseignant)=='yes'){
             array_push($medSess,$observer->id_Enseignant);
           }
        }

        return $medSess;
    }
    public function recuper_ListPatientSession(Request $request){
        //Remplissage des Patients
        $i=1;
        $patient = [];
        
        while(Input::get("input_patient_CIN".$i)!=""){
           
            $patient[$i-1] =  [
            "CIN" =>  Input::get("input_patient_CIN".$i) ,
            "nom" => Input::get("input_patient_nom".$i) ,
            "prenom"  => Input::get("input_patient_prenom".$i) ,
            ] ;
            $i++;
        }
        return $patient ;
    }
    public function recuper_nomSession(Request $request){

         $nom = $request->input('nom_session');
      
        
         return $nom ;
    }
  
  
}
