<?php

namespace App\Http\Controllers\HR;

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

class TimePolicyController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Time Policy List', ['only' => ['index']]);
      $this->middleware('permission:Time Policy Create', ['only' => ['create','store']]);
      $this->middleware('permission:Time Policy Edit', ['only' => ['edit','update']]);
    }
    public function index(Request $request)
    {
        if($request->ajax())
        {
            $count_total = TimePolicyModel::count();
            $count_filter = TimePolicyModel::count();
            $data = TimePolicyModel::orderBy('created_at', 'DESC')->take(20);
            return DataTables::of($data)
            ->addIndexColumn()
            ->with([
                'recordsTotal' => $count_total,
                'recordsFiltered' => $count_filter
            ])
            ->addColumn('action', function($data){
                $btn_edit="";
                $btn_delete="";
                if(Gate::check('Time Policy Edit')){
                    $btn_edit .= '<a data-url="'.route('hr-time-policy.edit', $data->id).'"  class="button_warning btn_edit"><span class="fa fa-pencil-square-o"></span> '.__('administrator.edit').'</a>';
                }
                if(Gate::check('Time 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.time-policy.index');
    }


    public function create()
    {
        return view('hr.time-policy.create');
    }


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

        try{

            $time_policy                = new TimePolicyModel();
            $time_policy->name          = $request->name;
            $time_policy->time_in       = $request->time_in;
            $time_policy->time_out      = $request->time_out;
            $time_policy->ot_time_in    = $request->ot_time_in;
            $time_policy->ot_time_out   = $request->ot_time_out;
            $time_policy->note          = $request->note;
            $time_policy->save();

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

            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)
    {
        $time_policy = TimePolicyModel::findOrFail($id);
        if($time_policy){
            return response()->json($time_policy);
        }else{
            return response()->json(['status' => 404, 'message' => 'Policy not found.']);
        }

    }


    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);

        try{

            $time_policy                = TimePolicyModel::findOrFail($id);
            $time_policy->name          = $request->name;
            $time_policy->time_in       = $request->time_in;
            $time_policy->time_out      = $request->time_out;
            $time_policy->ot_time_in    = $request->ot_time_in;
            $time_policy->ot_time_out   = $request->ot_time_out;
            $time_policy->note          = $request->note;
            $time_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)
    {
        //
    }

    public function delete_time_policy(Request $request)
    {
        $id = $request->delete_id;
        TimePolicyModel::where('id', $id)->delete();
        return redirect()->route('hr-time-policy.index')->with('success', 'Time Policy was deleted Successful!');
    }
}
