<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

class StageController 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(){

        $services = $this->gestStage->getServices();
        $etudiants= $this->gestStage->getEtudiants("");
        $stage_Encadrant_Etudiant= Encadrant_Service_Etudiant::all();

        return view('affectStage')
            ->with('services',$services)
            ->with('etudiants',$etudiants)
            ->with('stage_Encadrant_Etudiant',$stage_Encadrant_Etudiant);
    }

    /**
     * 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)
    {
        $etudiantIDs = $this->getInputsIdEtudiants($request);
        $id_Service = Input::get('id_Service');

    
        $this->gestStage->affectServiceToEtudiants($id_Service,$etudiantIDs);
        return redirect('/encadrant/create') ;
    }
    
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($encadrantId = null,$serviceId = null)
    {
        if(is_null($serviceId)){
            return Encadrant_Service_Etudiant::join('Service', 'Service.id_Service', '=',
                'Encadrant_Service_Etudiant.id_Stage')->select('Service.*')
                ->where('Encadrant_Service_Etudiant.id_Enseignant','=',$encadrantId)
                ->distinct()->get();
        } else {
            $service=Service::find($serviceId);
            return is_null($stage) ?  response()->
            json(['error' => 'stage Note not found'], HttpResponse::HTTP_NOT_FOUND) : $service;
        }
    }

    /**
     * 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 getInputsIdEtudiants(Request $request){
        $etudiants = $this->gestStage->getEtudiants("");
        $listEt = array();
        foreach ($etudiants as $etudiant) {
            if(Input::get($etudiant->id_Etudiant)=='yes'){
                array_push($listEt,$etudiant->id_Etudiant);
            }
        }
        return $listEt ;
    }
}
