<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Models\Banque;
use App\Models\Competence;
use App\Models\Contexte;
use App\Models\Critere;
use App\Models\Domaine;
use App\Models\Item;
use App\Models\Systeme;
use App\Models\File;
use Illuminate\Http\Request;
use Illuminate\Http\Response as HttpResponse;

class BanqueController extends Controller
{

    public function show($banqueId = null)
    {
        if (is_null($banqueId)) {
            // display the hole banque
            return view('banque', [
                'banqueItems' => Banque::all(),
                'contexteItems' => Contexte::all(),
                'critereItems' => Critere::all(),
                'domaineItems' => Domaine::all(),
                'systemeItems' => Systeme::all(),
                'competenceItems' => Competence::all()
            ]);

        } else {
            // display specific banque
            $banque = Banque::find($banqueId);
            $banque->items = Item::join('TitreGItem', 'TitreGItem.id_TitreGItem', '=', 'Item.id_TitreGItem')
                ->where('Item.id_Banque', '=', $banque->id_Banque)->get();
            return is_null($banque) ?
                response()->json(['error' => 'Banque not found'], HttpResponse::HTTP_NOT_FOUND) : $banque;
        }
    }

    public function store(Request $request)
    {
        // verify request inputs
        if (!$request->has([
            'label',
            'situation_Clinique',
            'instruction_Etudiant',
            'id_Domaine',
            'id_Critere',
            'id_Systeme',
            'id_Contexte'
        ])
        ) {
            return response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST);
        } else {

            $banque = Banque::create([
                'label' => $request->input('label'),
                'situation_Clinique' => $request->input('situation_Clinique'),
                'instruction_Etudiant' => $request->input('instruction_Etudiant'),
                'id_Domaine' => $request->input('id_Domaine'),
                'id_Critere' => $request->input('id_Critere'),
                'id_Systeme' => $request->input('id_Systeme'),
                'id_Contexte' => $request->input('id_Contexte')
            ]);


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

    public function edit(Request $request, $banqueId)
    {
        // verify request inputs
        if (!$request->has([
            'label',
            'situation_Clinique',
            'instruction_Etudiant',
            'id_Domaine',
            'id_Critere',
            'id_Systeme',
            'id_Contexte'
        ])
        ) {
            return response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST);
        } else {

            $banque = Banque::where('id_Banque', '=', $banqueId)->update(['label' => $request->input('label'),
                'situation_Clinique' => $request->input('situation_Clinique'),
                'instruction_Etudiant' => $request->input('instruction_Etudiant'),
                'id_Domaine' => $request->input('id_Domaine'),
                'id_Critere' => $request->input('id_Critere'),
                'id_Systeme' => $request->input('id_Systeme'),
                'id_Contexte' => $request->input('id_Contexte')]);

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

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

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