<?php

namespace App\Http\Controllers\HR;

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

class HrJobBenefitController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Benefit type List', ['only' => ['index','get_benefit']]);
      $this->middleware('permission:Benefit type Create', ['only' => ['create','store']]);
      $this->middleware('permission:Benefit type Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        return view('hr.job-benefit.index');
    }
    public function get_benefit()
    {

        $data  = HrJobBenefitModel::orderBy('created_at', 'DESC')->take(20);
        return DataTables::of($data)
            ->addIndexColumn()
            ->with([
                'recordsTotal' => $data->count(),
                'recordsFiltered' => $data->count()
            ])
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                    $delete = "លុបចោល";
                } else {
                    $edit = "Edit";
                    $delete = "Delete";
                }
                $btn_edit ="";
                $btn_delete="";
                if(Gate::check('Benefit type Edit')){
                    $btn_edit .= '<a data-url="'.route('hr-job-benefit.edit', $data->id).'" class="button_warning btn_edit" title="'.$edit.'"><span class="fa fa-pencil-square-o"></span></a>';
                }
                if(Gate::check('Benefit type Delete')) {
                    $btn_delete .= '<a class="button_danger btn_job_benefit" role="button" data-id="' . $data->id . '" title="' . $delete . '"><span class="fa fa-trash"></span></a>';
                }

                return $btn_edit.$btn_delete;
            })

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

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

    public function store(Request $request)
    {

        $this->validate($request,[
            'name_en'   =>'required',
            'name_kh'   =>'required',
            'amount'    =>'required',
        ]);

       try{
           $ben             = new HrJobBenefitModel();
           $ben->name       = $request->name_en;
           $ben->name_kh    = $request->name_kh;
           $ben->amount     = $request->amount;
           $ben->note       = $request->note;
           $ben->save();
           return response()->json(['status' => 200, 'message' => 'Created Benefit Success']);
       }catch(\Exception $ex){
           return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
       }
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $ben = HrJobBenefitModel::find($id);
        return response()->json($ben);
    }

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

        $this->validate($request,[
            'name_en'   =>'required',
            'name_kh'   =>'required',
            'amount'    =>'required',
        ]);
        try{
            $ben            = HrJobBenefitModel::find($id);
            $ben->name      = $request->name_en;
            $ben->name_kh   = $request->name_kh;
            $ben->amount    = $request->amount;
            $ben->note      = $request->note;
            $ben->update();

            return response()->json(['status' => 200, 'message' => 'Update Success']);

        }catch(\Exception $ex){

            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }
    }


    public function destroy($id)
    {
        //
    }

    //delete job benefit
    public function delete_job_benefit(Request $request)
    {
        $id = $request->delete_id;
        HrJobBenefitModel::where('id', $id)->delete();
        return redirect()->route('hr-job-benefit.index')->with('success', 'Deleted Successful!..');
    }
}
