<?php

namespace App\Http\Controllers\HR;

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

class HrDepartmentController extends Controller
{
    function __construct()
    {
      $this->middleware('permission:Department List', ['only' => ['index','get_data_department']]);
      $this->middleware('permission:Department Create', ['only' => ['create','store']]);
      $this->middleware('permission:Department Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        return view('hr.department.index');
    }

    // get department
    public function get_data_department()
    {
        $count_total = HrDepartmentModel::where('delete_status', 1)->count();
        $count_filter = HrDepartmentModel::where('delete_status', 1)->count();
        $data = HrDepartmentModel::where('delete_status', 1)->orderBy('id', 'DESC')->take(20);

        return DataTables::of($data)
            ->addIndexColumn()
            ->with([
                'recordsTotal' => $count_total,
                'recordsFiltered' => $count_filter
            ])
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                    $delete = "លុបចោល";
                } else {
                    $edit = "Edit";
                    $delete = "Delete";
                }
                $_edit ="";
                $_delete ="";
                if(Gate::check('Department Edit')){
                    $_edit .= '<a data-url="'.route('hr-department.edit', $data->id).'" data-id="'.$data->id.'" class="button_warning btn_edit"><span class="fa fa-pencil-square-o"></span> '.__('administrator.edit').'</a>';
                }
                if(Gate::check('Department Delete')) {
                    $_delete .= '<a class="button_danger btn_delete_department" data-id="' . $data->id . '" data-target=""><span class="fa fa-trash-o"></span> '.__('administrator.delete').'</a>';
                }
                return $_edit.$_delete;
            })
            ->rawColumns(['action' => 'action'])
            ->make(true);
    }

    public function create()
    {

        return view('hr.department.create');
    }


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

        try{
            $department                 = new HrDepartmentModel();
            $department->code           = $request->code;
            $department->khmer_name     = $request->name_kh;
            $department->english_name   = $request->name_en;
            $department->note           = $request->note;
            $department->save();

            if($request->ajax()){
                $data = HrDepartmentModel::select('khmer_name', 'id')->orderBy('id', 'DESC')->get();
                return response()->json(['status' => 200, 'data' => $data, 'message' => 'Insert Success']);
            }
            return response()->json(['status' => 200, 'message' => 'Department Successful!']);
        }catch(\Exception $ex){
            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }

    }


    public function show($id)
    {

    }


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


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

        try{
            $department                 = HrDepartmentModel::find($id);
            $department->code           = $request->code;
            $department->khmer_name     = $request->name_kh;
            $department->english_name   = $request->name_en;
            $department->note           = $request->note;
            $department->update();

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


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

    //delete department
    public function delete_department(Request $request)
    {
        HrDepartmentModel::where('id', $request->delete_id)->update(['delete_status' => 0]);
        return redirect()->route('hr-department.index')->with('success', 'Deleted Successful!..');
    }
}
