<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\HrJobModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PhpParser\Node\Expr\FuncCall;
use Yajra\DataTables\DataTables;
use Gate;
class HrJobController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:Job-List');
         $this->middleware('permission:Job-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Job-Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        return view('hr.job.index');
    }

    public function get_job()
    {
        $data = HrJobModel::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('Jop-Edit'))return '<a href="'.route('hr-job.edit',encrypt($data->id)).'" class="button_warning">'.$edit.'</a>';
                return '';
            })
//            ->addColumn('parent_id', function ($data) {
//                @$p = HrCompanyModel::find($data->parent_id);
//                return @$p->name;
//            })
            ->rawColumns(['action' => 'action'])
            ->make(true);
    }

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

    public function store(Request $request)
    {
        $this->validate($request,[
            'code'=>'required',
            'name'=>'required',
        ]);
        $job = new HrJobModel();
        $job->code = $request->code;
        $job->job_title = $request->name;
        $job->description = $request->description;
        $job->specification = $request->specification;
        if($job->save()){
            return redirect()->route('hr-job.index')->with('success','Job was inserted successful...');
        }else{
            return redirect()->route('hr-job.index')->with('danger','Job was not inserted...');
        }
    }

    public function show($id)
    {
        //
    }


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

    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'code'=>'required',
            'job_title'=>'required',
        ]);
        $job = HrJobModel::find($id);
        $job->code = $request->code;
        $job->job_title = $request->job_title;
        $job->description = $request->description;
        $job->specification = $request->specification;
        if($job->update()){
            return redirect()->route('hr-job.index')->with('success','Job was updated successful...');
        }else{
            return redirect()->route('hr-job.index')->with('danger','Job was not updated...');
        }
    }

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