<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\ExamenRequest;
use App\Http\Controllers\Controller;
use App\Metier\GestSession ;

use App\Metier\GestStage ;
use App\Models\Examen ;
use App\Models\Stage ;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Support\Facades\Input;

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

    protected $gestSession ;
    protected $gestStage ;
    
    public function __construct(GestSession $gestSession,GestStage $gestStage){
        $this->gestSession = $gestSession;
        $this->gestStage = $gestStage ;
    }

     public function index($sessionId,$examenId=null)
    {
        $stages =$this->gestStage->getStagesWithNiveau();
        $jurys = $this->gestStage->getJurys();
        if(!$examenId){
             
                return view('createExamen')->with(compact("sessionId","stages","jurys"));
        }else {
            $examen = $this->gestSession->getInfoExamen($examenId);
            return view('updateExamen')->with(compact("sessionId","stages","jurys","examen"));
        }
    }
       

    /**
     * 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,$sessionId)
    {
            $date = $this->recuper_dateExamen($request);

            $id_Stage = $request->input('id_Stage');
            $max_Places = $request->input('max_Place');
            $id_Jury = $request->input('id_Jury') ;
       
            $examen=$this->gestSession->createExamen($id_Stage,$date,$sessionId,$max_Places,$id_Jury);
           
             if($examen) {
                $examens = $this->gestSession->getExamens($sessionId);
                 return redirect('/session/'.$sessionId.'/examen');
             }
     
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($sessionId= null,$examenId = null)
      {
        if (is_null($examenId)) {
            // display the hole examen in Specific Session
            $examens = $this->gestSession->getExamens($sessionId);
            return view('examenView')->with('sessionId',$sessionId)->with('examens',$examens);
        } else {
            // display specific session
            $examen =$this->$gestSession->getExamen($examenId);
            return is_null($examen) ?
                response()->json(['error' => 'Examen not found'], HttpResponse::HTTP_NOT_FOUND) : $examen;
        }
      }

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

            $id_Stage = $request->input('id_Stage');
            $max_Places = $request->input('max_Place');
            $id_Jury = $request->input('id_Jury') ;
       
            $examen=$this->gestSession->updateExamen($examenId,$id_Stage,$date,$sessionId,$max_Places,$id_Jury);
             return redirect('/session/'.$sessionId.'/examen');
    }

    /**
     * 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=null,$examenId=null)
    {
        $this->gestSession->destroyExamen($examenId);
         return redirect('/session/'.$sessionId.'/examen');
    }

    public function choixItems(){
        $id = Input::get('id') ;
        $id_examen = $this->examen->id_Examen ;
            
        return view('SelectItem',compact('id'));

    }
     public function recuper_dateExamen(Request $request){

         $date = $request->input('date');
         str_replace(',','',$date);
         $date =date('Y-m-d', strtotime($date));

         return $date ;
    }

}
