<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\HrOvertimeRequestModel;
use App\User;
use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;

class HrOvertimeRequestController extends Controller
{
    public function index()
    {
        return view('hr.overtime-request.index');
    }
    public function get_overtime()
    {
        $data = HrOvertimeRequestModel::get();
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                    $view = "បង្ហាញ";
                } else {
                    $edit = "Edit";
                    $view = "View";
                }
                $bedit = '';
                if($data->status == 0){
                    $bedit = '<a href="'.route('hr-overtime-request.edit',encrypt($data->id)).'" class="button_warning">'.$edit.'</a>';
                }
                return ' <a class="button_success viewot" role="button" data-id="' . $data->id . '" data-toggle="modal tooltip" data-target="#viewModal" data-placement="top" title="'.$view.'">'.$view.'</a>'.$bedit;
            })
            ->editColumn('status', function ($data) {
                $status = '';
                $label = '';
                $check_language = Fun::lang();
                if ($check_language == 'km') {
                    $pending = "រង់ចាំ";
                    $approve = "ទទួលយកសំណើរ";
                    $reject = "បដិសេធសំណើរ";
                } else {
                    $pending = "Pending";
                    $reject = "Rejected";
                    $approve = "Approved";
                }
                if($data->status == 0){
                    $status = $pending;
                    $label = 'warning';
                }
                if($data->status == 1){
                    $status = $approve;
                    $label = 'success';
                }
                if($data->status == 2){
                    $status = $reject;
                    $label = 'danger';
                }
                return ' <a class="viewPopLink label label-'.$label.'" role="button" data-id="' . $data->id . '" data-status="' . $data->status . '"  data-toggle="modal tooltip" data-target="#myModal" data-placement="top" title="'.$status.'">'.$status.'</a>';

            })
            ->editColumn('emp_id', function ($data) {
                return optional($data->employee)->user_firstname.' '.optional($data->employee)->user_lastname;
            })
            ->editColumn('start', function ($data) {
                return substr_replace($data->start,' ',5,1);
            })
            ->editColumn('end', function ($data) {
                return substr_replace($data->end,' ',5,1);
            })
            ->rawColumns(['action' => 'action', 'status' => 'status'])
            ->make(true);
    }
    public function create()
    {
        $employee = User::select(DB::raw('CONCAT(user_firstname," ",user_lastname) AS name'), 'id')
            ->where('status',1)->pluck('name','id');
        return view('hr.overtime-request.create',compact('employee'));
    }
    public function store(Request $request)
    {
        
        $this->validate($request,[
            'emp_id'=>'required',
            'project'=>'required',
            'f_hh'=>'required',
            'f_mm'=>'required',
            'f_shift'=>'required',
            't_hh'=>'required',
            't_mm'=>'required',
            't_shift'=>'required',
            'date'=>'required'
        ]);
        $ot = new HrOvertimeRequestModel();
        $ot->emp_id = $request->emp_id;
        $ot->project = $request->project;
        $ot->date = date('Y-m-d',strtotime($request->date));
        $ot->note = $request->note;
        $start = $request->f_hh.':'.$request->f_mm.':'.$request->f_shift;
        $end = $request->t_hh.':'.$request->t_mm.':'.$request->t_shift;
        $ot->start = $start;
        $ot->end = $end;
        $ot->hours = $request->hour;
        $ot->save();
        return redirect()->route('hr-overtime-request.index')->with('success','Overtime request was submit successful...');
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        $overtime = HrOvertimeRequestModel::find(decrypt($id));
        $employee = User::select(DB::raw('CONCAT(user_firstname," ",user_lastname) AS name'), 'id')
            ->where('status',1)->pluck('name','id');
        $start[] = explode(':',$overtime->start);
        $end[] = explode(':',$overtime->end);
        $ot_type = $overtime->ot_type;
        $ot_rate = $overtime->ot_rate;
        $hour = $overtime->hours;
        return view('hr.overtime-request.edit',compact('overtime','employee','start','end','ot_type','ot_rate','hour'));
    }
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'emp_id'=>'required',
            'project'=>'required',
            'f_hh'=>'required',
            'f_mm'=>'required',
            'f_shift'=>'required',
            't_hh'=>'required',
            't_mm'=>'required',
            't_shift'=>'required',
            'date'=>'required',
        ]);
        $ot = HrOvertimeRequestModel::find($id);
        $ot->emp_id = $request->emp_id;
        $ot->project = $request->project;
        $ot->date = date('Y-m-d',strtotime($request->date));
        $ot->note = $request->note;
        $start = $request->f_hh.':'.$request->f_mm.':'.$request->f_shift;
        $end = $request->t_hh.':'.$request->t_mm.':'.$request->t_shift;
        $ot->start = $start;
        $ot->end = $end;
        $ot->hours = $request->hour;
        $ot->update();
        return redirect()->route('hr-overtime-request.index')->with('success','Overtime request was updated successful...');
    }
    public function destroy($id)
    {
        //
    }
    public function view_overtime(){
        $overtime = HrOvertimeRequestModel::find($_GET['id']);
        $employee = optional($overtime->employee);
        $status = '';
        $check_language = Fun::lang();
        if ($check_language == 'km') {
            $pending = "រង់ចាំ";
            $approve = "ទទួលយកសំណើរ";
            $reject = "បដិសេធសំណើរ";
        } else {
            $pending = "Pending";
            $reject = "Rejected";
            $approve = "Approved";
        }
        if($overtime->status == 1){
            $status = '<span class="label label-success">'.$approve.'</span>';
        }
        if($overtime->status == 0){
            $status = '<span class="label label-warning">'.$pending.'</span>';
        }
        if($overtime->status == 2){
            $status = '<span class="label label-danger">'.$reject.'</span>';
        }
        $start = substr_replace($overtime->start,' ',5,1);
        $end = substr_replace($overtime->end,' ',5,1);
        $data = [
            'employee'=>$employee->user_firstname.' '.$employee->user_lastname,
            'project'=>$overtime->project,
            'dates'=>$overtime->date,
            'starts'=>$start,
            'ends'=>$end,
            'note'=>$overtime->note,
            'statuss'=>$status,
        ];
        return response()->json(['datas' => $data]);
    }

    public function overtime_status(Request $request)
    {
        $id = $_GET['id'];
        $overtime = HrOvertimeRequestModel::find($id);
        $overtime->status = $request->status;
        $overtime->update();
        return redirect()->route('hr-overtime-request.index')->with('success', 'Overtime status was changed ...');
    }
}
