<?php

namespace App\Http\Controllers\HR;

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

class HrPositionController extends Controller
{

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

    public function get_position()
    {
        $count_total = HrPositionModel::where('status', 1)->count();
        $count_filter = HrPositionModel::where('status', 1)->count();
        $data = HrPositionModel::where('status', 1)->orderBy('id', 'DESC')->take(20);
        return DataTables::of($data)
            ->addIndexColumn()
            ->with([
                'recordsTotal' => $count_total,
                'recordsFiltered' => $count_filter
            ])
            ->addColumn('action', function ($data) {
                $btn  = "";
                if(Gate::check('Position Edit')){
                    $btn .= '<a data-url="'.route('hr-position.edit',$data->id).'" class="button_warning btn_edit" title="Edit"><span class="fa fa-pencil-square-o"></span> '.__('administrator.edit').'</a>';
                }
                if(Gate::check('Position Delete')) {
                    $btn .= '<a class="button_danger btn_delete_position" data-id="' . $data->id . '" data-target="" title="Delete"><span class="fa fa-trash-o"></span> '.__('administrator.delete').'</a>';
                }
                return $btn;
            })
            ->rawColumns(['action' => 'action'])
            ->make(true);
    }

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

    public function store(Request $request)
    {
        $this->validate($request,[
            'code'          =>'nullable|unique:nso007_hr_position,code',
            'name_en'       =>'required',
            'name_kh'       =>'required',
        ]);

        try{
            $position                    = new HrPositionModel();
            $position->code              = $request->code;
            $position->english_name      = $request->name_en;
            $position->khmer_name        = $request->name_kh;
            $position->description       = $request->description;
            $position->status            = 1;
            $position->save();

            if($request->ajax()){
                $data = HrPositionModel::select('khmer_name', 'id')->orderBy('id', 'DESC')->get();
                return response()->json(['status' => 200, 'data' => $data, 'message' => 'Insert Success']);
            }

            return response()->json(['status' => 200, 'message' => 'Position was created!']);
        }catch (\Exception $ex){
            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }

    }

    public function show($id)
    {

    }


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

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

        $this->validate($request,[
            'code'          =>'nullable|unique:nso007_hr_position,code,'.$id,
            'name_en'       =>'required',
            'name_kh'       =>'required',
        ]);

        try{
            $position                    = HrPositionModel::find($id);
            $position->code              = $request->code;
            $position->english_name      = $request->name_en;
            $position->khmer_name        = $request->name_kh;
            $position->description       = $request->description;
            $position->update();

            return response()->json(['status' => 200, 'message' => 'Position was updated!']);
        }catch(\Exception $ex){
            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }
    }


    public function destroy($id)
    {
        HrPositionModel::where('id', $id)->delete();
        return response()->json(['status' => 200, 'message' => 'Record was deleted!']);
    }

    //delete position
    public function delete_position(Request $request)
    {

    }
}
