<?php

namespace App\Http\Controllers\HR;

use App\Model\Hr\HrTrainingCourseModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DateTime;

class HrTrainingCourseController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Training Course Create', ['only' => ['create', 'store']]);
      $this->middleware('permission:Training Course List', ['only' => ['index']]);
      $this->middleware('permission:Training Course Edit', ['only' => ['update','edit']]);
    }
    public function index()
    {
        $training_course = HrTrainingCourseModel::get();
        return view('hr.training-course.index', compact('training_course'));
    }


    public function create()
    {
        return view('hr.training-course.create');
    }


    public function store(Request $request)
    {

        $this->validate($request, [
            'name'          => 'required',
            'description'   => 'required'
        ]);

        try{
            $training_course = new HrTrainingCourseModel();
            $training_course->name          = $request->name;
            $training_course->description   = $request->description;
            $training_course->date          = $request->date != null ? date('Y-m-d', strtotime($request->date)):date(now());
            $training_course->save();

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


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        $training_course = HrTrainingCourseModel::find($id);
        return view('hr.training-course.edit', compact('training_course'));
    }


    public function update(Request $request, $id)
    {

        $this->validate($request, [
            'name'          => 'required',
            'description'   => 'required'
        ]);

        try{
            $training_course = HrTrainingCourseModel::find($id);
            $training_course->name          = $request->name;
            $training_course->description   = $request->description;
            $training_course->date          = date('Y-m-d', strtotime($request->date));
            $training_course->update();

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

        }

    }


    public function destroy(HrTrainingCourseModel $hrTrainingCourseModel)
    {
        //
    }
}
