<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\Model\Hr\HrJobEducationLevelModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Gate;

class HrJobEducationLevelController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Education level Create', ['only' => ['create', 'store']]);
      $this->middleware('permission:Education level List', ['only' => ['index','get_education_level']]);
      $this->middleware('permission:Education level Edit', ['only' => ['update','edit']]);
    }
    public function index()
    {
        return view('hr.job-education-level.index');
    }
    public function get_education_level()
    {
        $data = HrJobEducationLevelModel::get();
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                } else {
                    $edit = "Edit";
                }
                $btn_edit="";
                if(Gate::check('Education level Edit')) {
                    $btn_edit .= '<a href="' . route('hr-job-education-level.edit', encrypt($data->id)) . '" class="button_warning">' . $edit . '</a>';
                }
                return $btn_edit;
            })

            ->rawColumns(['action' => 'action'])
            ->make(true);
    }

    public function create()
    {
        return view('hr.job-education-level.create');
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
        ]);

        try{
            $ben = new HrJobEducationLevelModel();
            $ben->name = $request->name;
            $ben->note = $request->note;
            $ben->save();
            return redirect()->route('hr-job-education-level.index')->with('success','Education level was inserted successful...');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $ben = HrJobEducationLevelModel::find(decrypt($id));
        return view('hr.job-education-level.edit',compact('ben'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'=>'required',
        ]);

        try{
            $ben = HrJobEducationLevelModel::find($id);
            $ben->name = $request->name;
            $ben->note = $request->note;
            $ben->update();
            return redirect()->route('hr-job-education-level.index')->with('success','Education level was updated successful...');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }
    }


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