<?php

namespace App\Http\Controllers;


use App\Helpers\IDate;
use App\Models\EmpLeave;
use App\Models\EmpLeaveDetail;
use App\Models\Employee;
use App\Models\GenAttend;
use App\Models\LeaveType;
use App\User;
use Illuminate\Support\Optional;
use phpDocumentor\Reflection\DocBlock\Tags\Method;
use Yajra\Datatables\Datatables;

class TakeLeaveAuthorizeController extends Controller
{

    public function getRowAttributesData()
    {
        $r = EmpLeave::with(['employee', 'leave_type'])->select('emp_leaves.*');
        //dd($r);
        return Datatables::of($r)
            ->editColumn('emp_id', function ($d) {
                return optional(User::find($d->emp_id))->user_firstname . optional(User::find($d->emp_id))->user_lastname;
            })
            ->editColumn('request_to', function ($d) {
                return optional(User::find($d->request_to))->user_firstname . optional(User::find($d->request_to))->user_lastname;
            })
            ->editColumn('from_date', function ($l) {
                return date('Y-m-d', strtotime(EmpLeave::find($l->id)->from_date));
            })
            ->editColumn('to_date', function ($l) {
                return date('Y-m-d', strtotime(EmpLeave::find($l->id)->to_date));
            })
            ->editColumn('request_date', function ($l) {
                return date('Y-m-d', strtotime(EmpLeave::find($l->id)->request_date));
            })
            ->editColumn('leave_type', function ($l) {
                return str_replace('_', ' ', Optional(LeaveType::find($l->leave_type))->name);
            })
            ->editColumn('review', function ($l) {
                $d = '';
                $t = $l->review;
                switch ($t){
                    case 1:
                        $d = '<span><i class="fa fa-check-circle "></i></span>';
                        break;
                    case 0:
                        $d = '<span style=" color: #ffffff;"><i class="fa fa-circle "></i></span>';
                        break;
                }
                return $d;
            })
            ->editColumn('approve', function ($l) {
                $d = '';
                $t = $l->approve;
                switch ($t){
                    case 1:
                        $d = '<span><i class="fa fa-check-circle "></i></span>';
                        break;
                    case 0:
                        $d = '<span style=" color: #ffffff;"><i class="fa fa-circle "></i></span>';
                        break;
                }
                return $d;
            })
            ->addColumn('action', function ($l) {
                return '<div class="text-center">
                <a class="btn btn-xs btn-social-icon btn-bitbucket" title="Review" href="' . url('take-leave-authorize-review/' . $l->id ) . '"><i class="fa fa-eye"></i></a>
                <a class="btn btn-xs btn-social-icon btn-bitbucket" title="Edit" href="' . url('take-leave/' . $l->id . '/edit') . '"><i class="fa fa-edit"></i></a>
                <a class="btn btn-xs  btn-social-icon btn-dropbox btn-delete" title="Delete"  onclick="remove_leave('.$l->id.')"><i class="fa fa-trash"></i></a>';
            })
            ->escapeColumns([])
            ->make(true);
    }


    public function getTotalLeaveByType(Request $request)
    {
        $emp_id = $request->emp_id;
        $start_date = $request->start_date;
        $end_date = $request->end_date;
        $total_day = dateDiff($start_date, $end_date) + 1;


        $year = IDate::getYear($start_date);


        $leaves = EmpLeaveDetail::where('emp_id', $emp_id)
            // ->where(DB::raw("DATE_FORMAT(leave_date ,'%Y')"), $year)
            ->select(DB::raw('SUM(day_num) as num_leave,leave_type,emp_id'))
            ->groupBy(DB::raw('leave_type,emp_id'))
            ->get();

        $leaveTotal = DB::table('emp_leave_total')
            ->select('*')
            ->where('emp_id', $emp_id)
            ->where('leave_year', $year)
            ->first();
        //==========
        $c_year = date("Y");
        $c_month = date("m");
        $c_day = date("d");
        $start_date = $c_year . '/' . $c_month . '/01';
        //  $end_date =$c_year.'/'.$c_month.'/'.$c_day  ;
        //==========
        $status = '';
        $job_status = '';
        $pols = Employee::selectRaw('pol_id,marital_status,job_status')->find($emp_id);
        if ($pols != null) {
            $pol_id = $pols->pol_id;
            $status = $pols->marital_status;
            $job_status = $pols->job_status;

        }
        $total_working_day = 0;

        $sat_sun = GenAttend::getSat($start_date, $end_date);
        if (array($sat_sun)) {
            // $sat = $sat_sun['sat'];
            $sun = $sat_sun['sun'];
            $sun_sat = $sat_sun['sun_sat'];
        }
        $total_day_cal = GenAttend::getAttLeave($pol_id, $emp_id, $start_date, $end_date);
        //$total_off_day = GenAttend::getOffDay($emp_id, $start_date, $end_date);

        $getLeaveUse = GenAttend::getUseLeave($emp_id, "$year/$c_month");
        $getLeave = GenAttend::getLeaveByMonth($emp_id, $end_date, $year);
        $getAnnualLeaveByMonth = GenAttend::getAnnualLeaveByMonth($emp_id, "$year/$c_month");
        $getTotalSickLeave = GenAttend::totalSickLeave($emp_id, $year);
        $getSickLeaveUse = GenAttend::getTotalSickLeaveUse($emp_id, "$year/$c_month");
        if ($getSickLeaveUse->num_leave == 5) {
            if ($getLeave <= 0 && $getSickLeaveUse->num_leave == 5) {
                $l_id = DB::table('emp_leave_total')
                    ->select(DB::raw('id'))
                    ->where('emp_id', $emp_id)
                    ->where(DB::raw('leave_year', '%Y'), $c_year)
                    ->first();
                DB::table('emp_leave_total')
                    ->where('id', $l_id->id)
                    ->update(['sick_leave' => 11]);
            }
        }

        if ($pol_id == 1) {
            $total_working_day = $total_day_cal + $sun;
        }

        if ($leaveTotal || $leaves) {
            return response()->json([
                'leaveTotal' => $leaveTotal,
                'leaves' => $leaves,
                'LeaveUse' => $getLeaveUse,
                'status' => $status,
                'day_work' => $total_working_day,
                'job_status' => $job_status,
                't_leave' => $getLeave,
                'annual_l' => $getAnnualLeaveByMonth,
                't_sick_leave' => $getTotalSickLeave

            ]);
        }
    }




}
