<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Session;
use App\Salle;
use Illuminate\Http\Request;

class SalleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $salle = Salle::where('nom', 'LIKE', "%$keyword%")
                ->orWhere('description', 'LIKE', "%$keyword%")
                ->latest()->paginate($perPage);
        } else {
            $salle = Salle::latest()->paginate($perPage);
        }

        return view('admin.salle.index', compact('salle'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('admin.salle.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        
        $requestData = $request->all();
        $already_exist = DB::connection(session::get('gestjury_database'))->table('gestjury_salle')
            ->where([
                ['nom', '=', $requestData['nom']],
            ])->exists();
        if ($already_exist) {
            return redirect('admin/salle')->with('error_message', 'Une salle avec ce nom existe déjà !');
        }
        DB::connection(session::get('gestjury_database'))->table('gestjury_salle')->insert([
            'nom' => $requestData['nom'], 
            'description'=> $requestData['description'],
        ]);
        return redirect('admin/salle')->with('flash_message', 'Salle ajouté!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $salle = Salle::findOrFail($id);

        return view('admin.salle.show', compact('salle'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $salle = Salle::findOrFail($id);

        return view('admin.salle.edit', compact('salle'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {
        
        $requestData = $request->all();
        
        $salle = Salle::findOrFail($id);
        $already_exist = DB::connection(session::get('gestjury_database'))->table('gestjury_salle')
            ->where([
                ['nom', '=', $requestData['nom']],
                ['id', '!=', $id]
            ])->exists();
        if ($already_exist) {
            return redirect('admin/salle')->with('error_message', 'Une salle avec ce nom existe déjà !');
        }
        $salle->update($requestData);

        return redirect('admin/salle')->with('flash_message', 'Salle modifié!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        Salle::destroy($id);

        return redirect('admin/salle')->with('flash_message', 'Salle supprimé!');
    }
}
