<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Models\Item;
use Illuminate\Http\Request;
use Illuminate\Http\Response as HttpResponse;
use App\Metier\GestSession;

class ItemController extends Controller
{
    protected $gestSession; 
    public function __construct(GestSession $gestSession){
        $this->gestSession=$gestSession;
    }

    public function show($banqueId = null, $itemId = null)
    {
        if (is_null($itemId)) {
            // display all the items
            return $this->gestSession->getItems();
        } else {
            // display specific banque
            $item =$this->gestSession->getItem($itemId);
            return is_null($item) ?
                response()->json(['error' => 'Item not found'], HttpResponse::HTTP_NOT_FOUND) : $item;
        }
    }

    public function store(Request $request, $banqueId)
    {
        // verify request inputs
        if (!$request->has([
                'label',
                'valeur',
                'id_Competence',
                'id_TitreGItem'
            ]) || is_null($banqueId)
        ) {
            return response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST);
        } else {
            $item = Item::create([
                'label' => $request->input('label'),
                'valeur' => $request->input('valeur'),
                'id_Competence' => $request->input('id_Competence'),
                'id_TitreGItem' => $request->input('id_TitreGItem'),
                'id_Banque' => $banqueId
            ]);

            return is_null($item) ?
                response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST) : $item;
        }
    }

    public function edit(Request $request, $itemId)
    {
        // verify request inputs
        if (!$request->has([
                'label',
                'valeur',
                'id_Competence',
            ]) || is_null($itemId)
        ) {
            return response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST);
        } else {
            $item = Item::where('id_Item', '=', $itemId)->update([
                'label' => $request->input('label'),
                'valeur' => $request->input('valeur'),
                'id_Competence' => $request->input('id_Competence'),
            ]);

            return is_null($item) ?
                response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST) : $item;
        }
    }

    public function update(Request $request, $itemId)
    {
        //
    }

    public function destroy($itemId)
    {
        //
    }
}
