<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Station ;
use App\Models\Systeme ;
use App\Models\Contexte ;
use App\Models\Domaine ;
use App\Models\Critere ;
use App\Models\Item;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Response as HttpResponse;
use App\Metier\GestSession ;
use Illuminate\Support\Facades\Input;
class StationController 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()
    {
        $domaines = $this->gestSession->getDomaines();
        $criteres = $this->gestSession->getCriteres();
        $systemes = $this->gestSession->getSystemes();
        $contextes = $this->gestSession->getContextes();
        $competences = $this->gestSession->getCompetences() ;
        $banques = $this->gestSession->getAllBanques();
        $competenceBanque = $this->gestSession->getComp(); //Tableau contient des string pour chaque banque l'ensemble des compétence associé
        $competenceBanqueId=$this->gestSession->getCompId();
        return view('createStation')
            ->with('domaines',$domaines)
            ->with('criteres',$criteres)
            ->with('contextes',$contextes)
            ->with('systemes',$systemes)
            ->with('competences',$competences)
            ->with('banques',$banques)
            ->with('competenceBanque',$competenceBanque)
            ->with('competenceBanqueId',$competenceBanqueId);;
    }

    /**
     * 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,$examenId)
    {       

            $banqueIDs = $this->getInputsStation($request);
            $ponderations = $this->getPonderation($request);
            $station=$this->gestSession->createStations($banqueIDs,$ponderations,$examenId);
            
            return redirect('/session/'.$sessionId.'/examen/'.$examenId.'/station');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($sessionId= null,$examenId = null,$stationId=null)
      {

        if (is_null($stationId)) {
            // display the hole examen in Specific Session
            $stations = $this->gestSession->getStations($examenId);
            return view('stationView')
                ->with('stations',$stations)
                ->with('sessionId',$sessionId)
                ->with('examenId',$examenId);
        } else {
            // display specific session
            $station = $this->gestSession->getStation($stationId);
            $contexte = $this->gestSession->getContexte($station->id_Contexte);
            $critere =  $this->gestSession->getCritere($station->id_Critere);
            $domaine = $this->gestSession->getDomaine($station->id_Domaine);
            $systeme = $this->gestSession->getSysteme($station->id_Systeme);
            $items=Item::whereIdBanque($station->id_Banque)
                ->join('Competence','Item.id_Competence','=','Competence.id_Competence')
                ->get();
            return view('stationDetailView')
                ->with('station', $station)
                ->with('sessionId',$sessionId)
                ->with('examenId',$examenId)
                ->with('stationId',$stationId)
                ->with('contexte',$contexte)
                ->with('critere',$critere)
                ->with('domaine',$domaine)
                ->with('systeme',$systeme)
                ->with('items',$items);
        }
      }
    /**
     * 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, $sessionId= null,$examenId = null,$stationId=null)
    {
        $station=$this->gestSession->getStation($stationId);
	    $station->ponderation=$request->input('ponderation');
	    $station->save();
	return redirect('/session/'.$sessionId.'/examen/'.$examenId.'/station');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($sessionId= null,$examenId = null,$stationId=null)
    {
        Station::whereIdStation($stationId)->delete();
	return redirect('/session/'.$sessionId.'/examen/'.$examenId.'/station');
    }
     public function getInputsStation(Request $request){
        $banqueIDs = array(); 
        $banques = $this->gestSession->getBanques();
        foreach ($banques as $banque) {
            if (Input::get($banque->id_Banque."B")=='yes'){
                array_push($banqueIDs, $banque->id_Banque);
            }
        }
        
        return $banqueIDs ;

    } 
    public function getPonderation(Request $request){
        $ponderations = array(); 
        $banques = $this->gestSession->getBanques();
        foreach ($banques as $banque) {
            if (Input::get($banque->id_Banque."P")!=''){
                array_push($ponderations, Input::get($banque->id_Banque."P"));
            }
        }
        
        return $ponderations ;
    }
}
