<?php

namespace App\Http\Controllers\HR;

use App\Model\Hr\ExternalLecturerModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Gate;

class ExternalLecturerController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:External Lecture Create', ['only' => ['create', 'store']]);
      $this->middleware('permission:External Lecture List', ['only' => ['index']]);
      $this->middleware('permission:External Lecture Edit', ['only' => ['update','edit']]);
    }

    public function index()
    {
        $external_lecturer = ExternalLecturerModel::get();
        return view('hr.external-lecturer.index', compact('external_lecturer'));
    }


    public function create()
    {
        return view('hr.external-lecturer.create');
    }


    public function store(Request $request)
    {
       $this->validate($request, [
           'full_name_kh' => 'required',
           'full_name_en' => 'required',
           'trainer_type' => 'required',
           'phone'        => 'required',
           'email'        => 'required',
           'address'      => 'required',
           'expertise'    => 'required'
       ]);

       try{
           $external_lecturer = new ExternalLecturerModel();

           $external_lecturer->full_name_kh = $request->full_name_kh;
           $external_lecturer->full_name_en = $request->full_name_en;
           $external_lecturer->trainer_type = $request->trainer_type;
           $external_lecturer->phone        = $request->phone;
           $external_lecturer->email        = $request->email;
           $external_lecturer->address      = $request->address;
           $external_lecturer->expertise    = $request->expertise;

           $external_lecturer->save();

           return redirect()->route('hr-external-lecturer.index')->with('success', 'Added Successful!');
       }catch(\Exception $ex){
           return back()->withErrors($ex->getMessage())->withInput();
       }

    }


    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $ex_lecturer = ExternalLecturerModel::find($id);
        return view('hr.external-lecturer.edit', compact('ex_lecturer'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'full_name_kh' => 'required',
            'full_name_en' => 'required',
            'trainer_type' => 'required',
            'phone'        => 'required',
            'email'        => 'required',
            'address'      => 'required',
            'expertise'    => 'required'
        ]);

        try{
            $external_lecturer = ExternalLecturerModel::find($id);
            $external_lecturer->full_name_kh = $request->full_name_kh;
            $external_lecturer->full_name_en = $request->full_name_en;
            $external_lecturer->trainer_type = $request->trainer_type;
            $external_lecturer->phone        = $request->phone;
            $external_lecturer->email        = $request->email;
            $external_lecturer->address      = $request->address;
            $external_lecturer->expertise    = $request->expertise;

            $external_lecturer->update();

            return redirect()->route('hr-external-lecturer.index')->with('success', 'Updated Successful!');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }

    }


    public function destroy(ExternalLecturerModel $externalLecturerModel)
    {
        //
    }
}
