<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Encadrant_Service_Etudiant;
use App\Models\Enseignant;
use App\Metier\GestStage ;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Support\Facades\Input;

class StageEncadrantController 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($encadrantId)
    {

        $etudiantStage = $this->gestStage->getEtudiantNValid($encadrantId);
        $checked = $this->verifChecked($etudiantStage) ;
        return view('validationStage')
            ->with('etudiants',$etudiantStage)
            ->with('checked',$checked);
    }

    /**
     * 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,$encadrantId)
    {
        $etudiantIDs=$this->getInputsIdEtudiants($request);
        $values = $this->getInputsRadio($request) ;
        $serviceId = $this->gestStage->getServiceEnseig($encadrantId);
       
       
        $this->gestStage->UpdatevalidateServiceEtudiants($etudiantIDs,$serviceId,$values);

        return redirect('/encadrant/'.$encadrantId.'/stage/show');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($encadrantId = null)
    {
        //afficher all stage of a specifique encadrant
        if(is_null($encadrantId)){
            return  Encadrant_Service_Etudiant::join('Enseignant', 'Enseignant.id_Enseignant', '=',
                'Encadrant_Service_Etudiant.id_Enseignant')->select('Enseignant.*')->distinct()->get();
        } else {
            $encadrant=Enseignant::find($encadrantId);
            return is_null($encadrant) ?  response()->
            json(['error' => 'encadrant Note not found'], HttpResponse::HTTP_NOT_FOUND) : $encadrant;
        }
    }

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

    /**
     * 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 getInputsIdEtudiantStage(Request $request,$encadrantId){
        $etudiants = $this->gestStage->getEtudiantNValid($encadrantId);

        $listEt = array();
        foreach ($etudiants as $etudiant) {
            if(Input::get($etudiant->id_Etudiant."_".$etudiant->id_Stage)=='yes'){
                array_push($listEt,$etudiant->id_Etudiant."_".$etudiant->id_Stage);
            }
        }
        return $listEt ;
    }
    public function getIdsEtudiants($array){
        $etudiants = array();
        foreach ($array as $e) {
            $e = substr($e,0, strpos($e,'_'));
            array_push($etudiants, $e);
        }
        return $etudiants;
    }
    public function getIdsStages($array){
        $stages = array();
        foreach ($array as $e) {
            $e = substr($e,strpos($e,'_')+1, strlen($e)-1);
            array_push($stages, $e);
        }
        return $stages;
    }
    public function getInputsIdEtudiants(Request $request){
        $etudiants = $this->gestStage->getEtudiants("");
        $listEt = array();
        foreach ($etudiants as $etudiant) {
            if(Input::get($etudiant->id_Etudiant)!=''){
                array_push($listEt,$etudiant->id_Etudiant);
            }
        }
        return $listEt ;
    }
    public function getInputsRadio(Request $request){
        $etudiants = $this->gestStage->getEtudiants("");
        $values = array();
        foreach ($etudiants as $etudiant) {
            if(Input::get($etudiant->id_Etudiant)!=''){
                array_push($values,Input::get($etudiant->id_Etudiant));
            }
        }
        return $values ;
    }
    public function verifChecked($etudiantStage){
        $checked = array();
        foreach ($etudiantStage as $e ) {
            if($e->isValid==0){
                array_push($checked,"checked");
            }else {
                array_push($checked,"");
            }
        }
        return $checked ;
    }
}
