<?php

namespace App\Http\Controllers\HR;

use App\Models\EmployeeLeaveTotal;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;

class LeaveTotalController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = EmployeeLeaveTotal::latest()->get();
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {

                    $btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View</a>';

                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('hr.total-leave.index');
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        //
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        //
    }

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

    public function destroy($id)
    {
        //
    }

    function generate(){
        //dd(11);
        $c_year = date("Y");
        $c_month = date("m");
        $c_day = date("d");

        $emp = User::selectRaw('id, joined_date as effect_d, YEAR(joined_date) as y,
                                MONTH(joined_date) as m, DAY(joined_date) as d,
                                DATEDIFF( NOW(),joined_date) as t_day')->get();
       // dd($emp);

        foreach ($emp as $row) {

            $k = 0;
            $m = $row->m;
            $d = $row->d;
            $y = $row->y;

            if ($y > 0) {
                if ($y > 0 || $y >= 2001) {
                //dd($y);
                    for ($i = $y; $i <= $c_year; $i++) {
                        //==================== process save =============

                        $d = null;

                        $sick = 0;
                        $special = 0;
                        $unpaid = 0;
                        $annual = 0;
                        $other = 0;
                        $mat = 0;
                        //if ($i == date('Y')) {
                        ///dd($i== date('Y'));
                        $d = \App\Models\EmployeeLeaveTotalDetail::/*where('leave_year', $i) ->*/
                        selectRaw('sum(special_leave) as special, sum(sick_leave) 
                         as sick, sum(maternity_leave) as mat 
                         , sum(annual_leave) as annual  , sum(unpaid_leave) as  un
                            , sum(other_leave) other 
                        ')->where('emp_id', $row->id)->first();
                        $sick = $d->sick;
                        $special = $d->special;
                        $other = $d->other;
                        $annual = $d->annual;
                        $mat = $d->mat;
                        $unpaid = $d->un;

                        $m = \App\Models\EmLeaveType::where('emp_id', $row->id)
                            ->where('leave_year', $i)->first();
                        if ($m == null) {
                            $m = new EmployeeLeaveTotal();
                        }
                        if ($m != null) {

                            $special_leave = 5 + $special;
                            $sick_leave = 60 + $sick;
                            $maternity_leave = 90 + $mat;
                            $annual_leave = 18 + $annual;
                            $unpaid_leave = 180 + $unpaid;
                            $other_leave = 180 + $other;

                            $m->emp_id = $row->id;
                            $m->leave_year = $i;
                            $m->special_leave = $special_leave;
                            $m->sick_leave = $sick_leave;
                            $m->maternity_leave = $maternity_leave;
                            $m->annual_leave = $annual_leave;
                            $m->unpaid_leave = $unpaid_leave;
                            $m->other_leave = $other_leave;
                            $m->save();
                        }
                    }
                }
            }
        }
        return redirect()->back()->with(['success'=>'Success Generate Leave Total']);

    }

}
