<?php

namespace App\Http\Controllers\HR;

use App\Model\Hr\HrDepartmentModel;
use App\Model\Hr\HrEmployeeRotateModel;
use App\Model\Hr\HrPositionModel;
use App\Model\setup\BranchModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Gate;

class  HrEmployeeRotateController extends Controller
{
    function __construct()
    {
       $this->middleware('permission:Employee Rotate List', ['only' => ['index', 'get_rotate']]);
       $this->middleware('permission:Employee Rotate Show', ['only' => ['show_rotate']]);
       $this->middleware('permission:Employee Rotate Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        return view('hr.employee-rotate.index');
    }

    //get rotate
    public function get_rotate()
    {
        $count_total    = HrEmployeeRotateModel::count();
        $count_filter   = HrEmployeeRotateModel::count();
        $data           = HrEmployeeRotateModel::orderBy('id', 'DESC')->take(20);
        return DataTables::of($data)
                        ->addIndexColumn()
                        ->with([
                            'recordsTotal' => $count_total,
                            'recordsFiltered' => $count_filter
                        ])
                        ->editColumn('rotate_date', function($data){
                            return $data->rotate_date ? date('d-m-Y', strtotime($data->rotate_date)) : "";
                        })
                        ->addColumn('current_branch', function($data){
                            return optional($data->from_branch)->english_name;
                        })
                        ->addColumn('current_department', function($data){
                            return optional($data->from_department)->english_name;
                        })
                        ->addColumn('current_position', function($data){
                            return optional($data->from_position)->english_name;
                        })
                        ->addColumn('transfer_branch', function($data){
                            return optional($data->to_branch)->english_name;
                        })
                        ->addColumn('transfer_department', function($data){
                            return optional($data->to_department)->english_name;
                        })
                        ->addColumn('transfer_position', function($data){
                            return optional($data->to_position)->english_name;
                        })
                        ->addColumn('action', function($data){
                            $btn_view="";
                            $btn_delete="";
                            if(Gate::check('Employee Rotate Show')){
                            $btn_view .= '<a class="button_success btn-show" data-id="'.$data->id.'"><span class="fa fa-eye"></span></a>';
                            }
                            if(Gate::check('Employee Rotate Delete')) {
                                $btn_delete .= '<a class="button_danger btn-delete" data-id="' . $data->id . '"><span class="fa fa-trash-o"></span></a>';
                            }
                            return $btn_view.$btn_delete;
                        })
                        ->rawColumns(['action' => 'Action'])
                        ->make(true);
    }

    public function create()
    {
        //
    }


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


    public function show(HrEmployeeRotateModel $hrEmployeeRotateModel)
    {
        //
    }


    public function edit($id)
    {

        $emp_rotate     = User::find($id);
        $position       = HrPositionModel::pluck('english_name', 'id');
        $department     = HrDepartmentModel::pluck('english_name','id');
        $branch         = BranchModel::pluck('name', 'id');
        return view('hr.employee-rotate.edit', compact('id', 'emp_rotate','position','department','branch'));
    }


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

        try{
            if($request->type == '1')
            {
                $emp = User::where('id', $id)->first();

                $employee_rotate = new HrEmployeeRotateModel();

                $employee_rotate->parent_id                 = $emp->parent_id;
                $employee_rotate->leader_id                 = $emp->leader_id;
                $employee_rotate->group_id                  = $emp->group_id;

                $employee_rotate->current_branch_id         = $emp->branch_id;
                $employee_rotate->current_department_id     = $emp->department_id;
                $employee_rotate->current_position_id       = $emp->position_id;

                $employee_rotate->employment_id             = $emp->employment_id;
                $employee_rotate->user_year                 = $emp->user_year;
                $employee_rotate->code                      = $emp->code;
                $employee_rotate->name                      = $emp->name;
                $employee_rotate->name_kh                   = $emp->name_kh;
                $employee_rotate->phone                     = $emp->phone;
                $employee_rotate->email                     = $emp->email;
                $employee_rotate->nationality               = $emp->nationality;
                $employee_rotate->date_of_birth             = $emp->date_of_birth;
                $employee_rotate->address                   = $emp->address;
                $employee_rotate->place_of_birth            = $emp->place_of_birth;
                $employee_rotate->gender                    = $emp->gender;
                $employee_rotate->marital_status            = $emp->marital_status;
                $employee_rotate->id_card                   = $emp->id_card;
                $employee_rotate->avartar                   = $emp->avartar;
                $employee_rotate->joined_date               = $emp->joined_date;
                $employee_rotate->department                = $emp->department;
                $employee_rotate->role                      = $emp->role;
                $employee_rotate->supervisor                = $emp->supervisor;
                $employee_rotate->certificate               = $emp->certificate;
                $employee_rotate->skill                     = $emp->skill;
                $employee_rotate->dependent                 = $emp->dependent;
                $employee_rotate->note                      = $emp->note;
                $employee_rotate->status                    = $emp->status;
                $employee_rotate->user_type                 = $emp->user_type;
                $employee_rotate->basic_salary              = $emp->basic_salary;
                $employee_rotate->language                  = $emp->language;
                $employee_rotate->educate                   = $emp->educate;
                $employee_rotate->emergency_contact         = $emp->emergency_contact;
                $employee_rotate->family                    = $emp->family;
                $employee_rotate->family_full               = $emp->family_full;
                $employee_rotate->work_experience           = $emp->work_experience;
                $employee_rotate->spouse                    = $emp->spouse;
                $employee_rotate->child                     = $emp->child;
                $employee_rotate->increment                 = $emp->increment;
                $employee_rotate->meal_transport_allowance  = $emp->meal_transport_allowance;
                $employee_rotate->position_allowance        = $emp->position_allowance;
                $employee_rotate->other_allowance           = $emp->other_allowance;
                $employee_rotate->status                    = $emp->status;

                $employee_rotate->transfer_branch_id        = $request->branch_id;
                $employee_rotate->transfer_department_id    = $request->department_id;
                $employee_rotate->transfer_position_id      = $request->position_id;
                $employee_rotate->rotate_date               = date('Y-m-d',strtotime($request->rotate_date));
                $employee_rotate->reason                    = $request->reason;
                $employee_rotate->type                      = $request->type;
                $employee_rotate->save();
            }

            if($request->type == '1')
            {
                $employee = User::where('id', $id)->first();

                $employee->branch_id        = $request->branch_id;
                $employee->department_id    = $request->department_id;
                $employee->position_id      = $request->position_id;
                $employee->update();
            }

            return redirect()->route('hr-emp-rotate.index')->with('success', 'Rotation Successful!');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }
    }


    public function destroy($id)
    {
        //
    }

    //show rotate
    public function show_rotate(Request $request)
    {
        $data = HrEmployeeRotateModel::find($request->id);
        $data->load(['from_branch', 'from_department','from_position','to_branch', 'to_department', 'to_position']);
        return response()->json($data);
    }

    //delete rotate
    public function delete_rotate(Request $request)
    {
        $id = $request->delete_id;
        HrEmployeeRotateModel::where('id', $id)->delete();
        return redirect()->route('hr-emp-rotate.index')->with('succss', 'Deleted Successful!..');
    }
}
