<?php

namespace App\Http\Controllers\HR;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Hr\OvertimePolicyModel;
use App\Model\Hr\OvertimeTypeModel;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Gate;

class OvertimePolicyController extends Controller
{

    function __construct()
    {
          $this->middleware('permission:OT Policy List', ['only' => ['index']]);
          $this->middleware('permission:OT Policy Create', ['only' => ['create','store']]);
          $this->middleware('permission:OT Policy  Edit', ['only' => ['edit','update']]);
    }
    public function index(Request $request)
    {
        if($request->ajax())
        {
            $count_total    = OvertimePolicyModel::where('status', 1)->count();
            $data           = OvertimePolicyModel::where('status', 1)->orderBy('created_at', 'DESC');
            $count_filter   = $data->count();
            return DataTables::of($data->take(20))
                        ->addIndexColumn()
                        ->with([
                            'recordsTotal' => $count_total,
                            'recordsFiltered' => $count_filter
                        ])
                        ->addColumn('action', function($data){
                            $btn_edit ="";
                            $btn_delete ="";
                            if(Gate::check('OT Policy  Edit')){
                                $btn_edit .= '<a data-url="'.route('overtime-policy.edit', $data->id).'" class="button_warning btn_edit "><span class="fa fa-pencil-square-o"></span> '.__('administrator.edit').'</a>';
                            }
                            if(Gate::check('OT Policy  Delete')) {
                                $btn_delete .= '<a class="button_danger btn_delete" data-id="' . $data->id . '"><span class="fa fa-trash-o"></span> '.__('administrator.delete').'</a>';
                            }
                            return $btn_edit.$btn_delete;
                        })

                        ->rawColumns(['action', 'action'])
                        ->make(true);
        }
        return view('hr.overtime-policy.index');
    }


    public function create()
    {
        $overtime_type = OvertimeTypeModel::pluck('en_name', 'id')->prepend('Please Select', '');
        return view('hr.overtime-policy.create', compact('overtime_type'));
    }


    public function store(Request $request)
    {
        $this->validate($request, [
            'name'              => 'required',
            'rate_normal_ot'    => 'required',
            'rate_sunday_ot'    => 'required',
            'rate_holiday_ot'   => 'required',
            'effect_date'       => 'required'
        ]);

        try{

            $ot_policy                      = new OvertimePolicyModel();
            $ot_policy->name                = $request->name;
            $ot_policy->normal_ot_rate      = $request->rate_normal_ot;
            $ot_policy->sunsat_ot_rate      = $request->rate_sunday_ot;
            $ot_policy->holiday_ot_rate     = $request->rate_holiday_ot;
            $ot_policy->effect_date         = $request->effect_date != null ? date('Y-m-d', strtotime($request->effect_date)) : null;
            $ot_policy->save();
            return response()->json(['status' => 200, 'message' => 'Created Successful.']);
        }catch(\Exception $ex){
            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }

    }


    public function show($id)
    {
        //
    }


    public function edit($id)
    {

        $overtime_policy    = OvertimePolicyModel::findOrFail($id);
        if($overtime_policy){
            $overtime_type      = OvertimeTypeModel::pluck('en_name', 'id')->prepend('Please Select', '');
            return response()->json($overtime_policy);
        }else{
            return response()->json(['status' => 404, 'message' => 'Overtime policy not found']);
        }


    }


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

        $this->validate($request, [
            'name'              => 'required',
            'rate_normal_ot'    => 'required',
            'rate_sunday_ot'    => 'required',
            'rate_holiday_ot'   => 'required',
            'effect_date'       => 'required'
        ]);

        try{
            $ot_policy                      = OvertimePolicyModel::findOrFail($id);
            $ot_policy->name                = $request->name;
            $ot_policy->normal_ot_rate      = $request->rate_normal_ot;
            $ot_policy->sunsat_ot_rate      = $request->rate_sunday_ot;
            $ot_policy->holiday_ot_rate     = $request->rate_holiday_ot;
            $ot_policy->effect_date         = $request->effect_date != null ? date('Y-m-d', strtotime($request->effect_date)) : null;
            $ot_policy->update();
            return response()->json(['status' => 200, 'message' => 'Updated Successful.']);
        }catch(\Exception $ex){
            return response()->json(['status' => 400, 'message' => $ex->getMessage()]);
        }

    }


    public function destroy($id)
    {
        //
    }
    // get benefit type
    public function get_overtime_type(Request $request)
    {
        $data = OvertimeTypeModel::where('id', $request->id)->first();
        return response()->json($data);
    }
    public function delete_overtime_policy(Request $request)
    {
        $id = $request->delete_id;
        OvertimePolicyModel::where('id', $id)->update(['status' => 0]);
        return redirect()->route('overtime-policy.index')->with('success', 'Data was deleted successful!');

    }
}
