<?php

namespace App\Http\Controllers\Admin;

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

class SignataireController 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)) {
            $signataire = Signataire::where('nom', 'LIKE', "%$keyword%")
                ->join('fonction', ['fonction.idFonction'=> 'gestjury_signataires.fonction'])
                ->orWhere('prenoms', 'LIKE', "%$keyword%")
                ->orWhere('email', 'LIKE', "%$keyword%")
                ->orWhere('phone', 'LIKE', "%$keyword%")
                ->orWhere('sexe', 'LIKE', "%$keyword%")
                ->orWhere('fonction', 'LIKE', "%$keyword%")
                ->latest()->paginate($perPage);
        } else {
            $signataire = Signataire::latest()
            ->join('africawebstudent_universite.fonction as fonction','fonction.idFonction','=','gestjury_signataires.fonction')
            ->paginate($perPage);
        }

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        $grades = DB::connection(session::get('gestjury_database'))->table('grade')->get();
        $fonctions = DB::connection('africawebstudent_universite')->table('fonction')->get();
        //return view('admin.signataire.create')->with('fonctions', $fonctions);
        return view('admin.signataire.create')->with([
            'grades'=>$grades,
            'fonctions'=>$fonctions,
        ]);
    }

    /**
     * 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_signataires')
            ->where([
                ['nom', '=', $requestData['nom']],
                ['prenoms', '=', $requestData['prenoms']],
            ])->exists();
        $already_exist2 = DB::connection(session::get('gestjury_database'))->table('gestjury_signataires')
            ->where([
                ['email', '=', $requestData['email']],
            ])->exists();
        if ($already_exist || $already_exist2) {
            return redirect('admin/signataire')->with('error_message', 'Un signataire avec ces informations existe déjà !');
        }
        DB::connection(session::get('gestjury_database'))->table('gestjury_signataires')->insert([
            'nom' => $requestData['nom'], 
            'prenoms'=> $requestData['prenoms'],
            'email'=> $requestData['email'],
            'phone'=> $requestData['phone'],
            'grade'=> $requestData['id_grade'],
            'fonction'=> $requestData['idFonction'],
            'sexe'=> $requestData['sexe'],
        ]);
        return redirect('admin/signataire')->with('flash_message', 'Signataire ajouté!');
    }

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $signataire = Signataire::findOrFail($id);
        //$fonctions = DB::connection(session::get('gestjury_database'))->table('fonction')->get();
        $grades = DB::connection(session::get('gestjury_database'))->table('grade')->get();
        $fonctions = DB::connection('africawebstudent_universite')->table('fonction')->get();
        return view('admin.signataire.edit', compact('signataire'))->with([
            'grades'=>$grades,
            'fonctions'=>$fonctions,
        ]);
    }

    /**
     * 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();
        $signataire = Signataire::findOrFail($id);
        $already_exist = DB::connection(session::get('gestjury_database'))->table('gestjury_signataires')
            ->where([
                ['nom', '=', $requestData['nom']],
                ['prenoms', '=', $requestData['prenoms']],
                ['id', '!=', $id]
            ])->exists();
        $already_exist2 = DB::connection(session::get('gestjury_database'))->table('gestjury_signataires')
            ->where([
                ['email', '=', $requestData['email']],
                ['id', '!=', $id]
            ])->exists();
        if ($already_exist || $already_exist2) {
            return redirect('admin/signataire')->with('error_message', 'Un signataire avec ces informations existe déjà !');
        }
        DB::connection(session::get('gestjury_database'))->table('gestjury_signataires')
        ->where(['id'=> $signataire->id])
        ->update([
            'nom' => $requestData['nom'], 
            'prenoms'=> $requestData['prenoms'],
            'email'=> $requestData['email'],
            'phone'=> $requestData['phone'],
            'grade'=> $requestData['id_grade'],
            'fonction'=> $requestData['idFonction'],
            'sexe'=> $requestData['sexe'],
        ]);
        return redirect('admin/signataire')->with('flash_message', 'Signataire modifié!');
    }

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

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