<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\HrJobBenefitModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use Gate;
class HrJobBenefitController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
         $this->middleware('permission:Job-Benefit-List');
         $this->middleware('permission:Job-Benefit-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Job-Benefit-Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        return view('hr.job-benefit.index');
    }
    public function get_benefit()
    {
        $data = HrJobBenefitModel::get();
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                } else {
                    $edit = "Edit";
                }
                if(Gate::check('Benefit-Edit'))return '<a href="'.route('hr-job-benefit.edit',encrypt($data->id)).'" class="button_warning">'.$edit.'</a>';
                return '';
                
            })
            ->addColumn('amount', function ($data) {
                return '$'.$data->amount;
            })
            ->rawColumns(['action' => 'action'])
            ->make(true);
    }

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

    public function store(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
        ]);
        $ben = new HrJobBenefitModel();
        $ben->name = $request->name;
        $ben->name_kh = $request->name_kh;
        $ben->amount = $request->amount;
        $ben->note = $request->note;
        $ben->save();
        return redirect()->route('hr-job-benefit.index')->with('success','Benefit was inserted successful...');
    }

    public function show($id)
    {
        //
    }

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

    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'=>'required',
        ]);
        $ben = HrJobBenefitModel::find($id);
        $ben->name = $request->name;
        $ben->name_kh = $request->name_kh;
        $ben->amount = $request->amount;
        $ben->note = $request->note;
        $ben->update();
        return redirect()->route('hr-job-benefit.index')->with('success','Benefit was updated successful...');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
