<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\Helpers\IString;
use App\Http\Requests\TakeLeaveAuthorizeRequest;
use App\Model\Hr\EmpLeave;
use App\Model\Hr\EmpLeaveDetail;
use App\Model\Hr\Employee;
use App\Model\Hr\EmployeeLeaveTotal;
use App\Model\Hr\LeaveType;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use App\Http\Requests\TakeLeaveRequest ;
use Illuminate\Support\Facades\Gate;

class TakeLeaveAuthorizeController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Take Leave Authorize List', ['only' => ['index']]);
      $this->middleware('permission:Take Leave Authorize Create', ['only' => ['create','store']]);
      $this->middleware('permission:Take Leave Authorize Edit', ['only' => ['edit','update']]);
      $this->middleware('permission:Take Leave Approve', ['only' => ['change_status']]);
      $this->middleware('permission:Take Leave Show', ['only' => ['review']]);
    }
    public function index(Request $request)
    {
        $search     = $request->input('search.value');
        if($request->ajax())
        {
            $count_total    = EmpLeave::where('review', 1)->count();
            $data           = EmpLeave::select('emp_leaves.*', 'users.name')
                                ->join('users', 'users.id', 'emp_leaves.emp_id')
                                ->where(function ($query) use ($search){
                                    $query->orWhere('users.name', 'LIKE', "%$search%");
                                })
                                ->where('review', 1);
            $count_filter   = $data->count();
            return Datatables::of($data->take(20))
                            ->addIndexColumn()
                            ->with([
                                'recordsTotal' => $count_total,
                                'recordsFiltered' => $count_filter
                            ])
                            ->editColumn('emp_id',function ($d){
                                return optional(User::find($d->emp_id))->name;
                            })
                            ->editColumn('request_to',function ($d){
                                return optional(User::find($d->request_to))->name;
                            })
                            ->editColumn('leave_type', function($data){
                                return IString::getUpperCaseCharacter(optional($data->leave_typee)->name);
                            })
                            ->editColumn('from_date', function($data){
                                return $data->from_date ? date('d-m-Y', strtotime($data->from_date)) : "";
                            })
                            ->editColumn('to_date', function($data){
                                return $data->to_date ? date('d-m-Y', strtotime($data->to_date)) : "";
                            })
                            ->editColumn('request_date', function($data){
                                return $data->request_date ? date('d-m-Y', strtotime($data->request_date)) : "";
                            })
                            ->editColumn('review', function($data){
                                $review  = $data->review?$data->review: "";
                                if($review == 0)
                                {
                                    $status = '<small class="label label-primary">pending</small>';

                                }elseif($review == 1){
                                    $status = '<small class="label label-success">approved</small>';

                                }elseif($review == 2){
                                    $status = '<small class="label label-danger">rejected</small>';
                                }

                                return $status;

                            })
                            ->editColumn('approve', function($data){
                                $approve  = $data->approve?$data->approve: "";
                                if($approve == 0)
                                {
                                    $status = '<small class="label label-primary">pending</small>';

                                }elseif($approve == 1){
                                    $status = '<small class="label label-success">approved</small>';

                                }elseif($approve == 2){
                                    $status = '<small class="label label-danger">rejected</small>';
                                }

                                return $status;

                            })
                            ->addColumn('action' ,function($data){
                                $check_language = Fun::lang();

                                if ($check_language == 'km') {
                                    $status = "ស្ថានភាព";
                                    $view = "បង្ហាញ";
                                    $delete = "លុបចោល";
                                    $deduct​ = "កាត់លុយ";
                                }else{
                                    $status = "Status";
                                    $view = "View";
                                    $delete = "Delete";
                                    $deduct​ = "Deduct";
                                }
                                $btn = '';
                                if(Gate::check('Take Leave Authorize Status')) {
                                    if ($data->approve == 0) {
                                        $btn .= '<a class="button_primary btn_status_authorize" title="' . $status . '" data-id="' . $data->id . '"><span class="fa fa-exchange"></span></a>';
                                    }else{
                                        $btn .= '';
                                    }
                                }
                                if(Gate::check('Take Leave Authorize Show')) {
                                    $btn .= '<a href="' . route('take-leave-form', $data->id) . '" target="_blank" class="button_success" title="' . $view . '"><span class="fa fa-eye"></span></a>';
                                }
                                if(Gate::check('Take Leave Authorize Approve')) {
                                    $btn .= '<a class="button_danger btn_deduct" title="' . $deduct​ . '" data-id="' . $data->id . '"><span class="fa fa-usd"></span></a>';
                                }

                                return $btn;
                            })
                            ->rawColumns(['action' => 'action', 'review' => 'review', 'approve' => 'approve'])
                            ->make(true);
        }
        return view('hr.take-leave-authorize.index');
    }

    public function create()
    {
        $job = Employee::pluck('name', 'id');
        return view('hr.take-leave-authorize.create', compact('job'));

    }

    public function store(Request $r)
    {

        EmpLeave::saveDetail( null, $r);
        return redirect('take-leave-authorize')->with('success', 'Employee leave success full');
    }

    public function show($id)
    {

    }

    public function edit($id)
    {
        $d = EmpLeave::find($id);
        return view('hr.take-leave-authorize.create', compact('d'));
    }

    public function update(Request $r, $id)
    {
        EmpLeave::saveDetail( $id, $r);
        return redirect('take-leave-authorize')->with('success', 'Employee leave success full');
    }

    public function destroy($id)
    {

        $d = EmpLeave::findOrFail($id);
        $d->delete();

        return redirect('take-leave-authorize')->with('success', 'Employee leave is successfully deleted');
    }

    function delete($id){
        EmpLeave::find($id)->delete();
        return response()->json([
            'success' => true,
            'message' => 'Employee leave is successfully deleted'
        ]);
    }

    function review($id){
        EmpLeave::saveReview($id);
        $emp_leave = EmpLeave::find($id);
        $emp = Employee::pluck('name', 'id');
        $leave_type = LeaveType::pluck('name','id');
        return view('hr.take-leave-authorize.review', compact('leave_type','emp_leave', 'emp'));
    }
    function approve(Request $request, $id)
    {
        $approve = $request->approve;
        $emp_leave = EmpLeave::find($id);
        $emp_leave_detail = EmpLeaveDetail::where('emp_leave_id',$id)->get();

        if($emp_leave){
            $emp_leave->approve = $approve;
            $emp_leave->save();
        }
        foreach ($emp_leave_detail as $row){
            $row->approve = $approve;
            $row->save();
        }

        return redirect('take-leave-authorize')->with('success', 'Employee leave is successfully updated');

    }
    public function change_status(Request $request)
    {
        $id         = $request->id;
        $status     = $request->status;
        EmpLeave::where('id', $id)->update(['approve' => $status]);
        //EmpLeaveDetail::where('emp_leave_id',$id)->update(['approve' => $status]);
        return redirect('take-leave-authorize')->with('success', 'Employee leave is Changed successfully');
    }

    public function deduct_status(Request $request)
    {
        $id         = $request->ids;
        $deduct     = $request->deduct;
        EmpLeave::where('id', $id)->update(['deduct' => $deduct]);
        //EmpLeaveDetail::where('emp_leave_id',$id)->update(['deduct' => $deduct]);
        return redirect('take-leave-authorize')->with('success', 'Employee updated successfully');
    }
}
