<?php

namespace App\Http\Controllers\HR;

use App\Model\Hr\HrCompanyModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use App\User;
use App\Fun\Fun;
use Auth;


class HrCompanyController extends Controller
{
    public function index()
    {
        return view('hr.company-structure.index');
    }

    public function get_company_structure()
    {
        $data = HrCompanyModel::take(20);
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($data) {
                $check_language = Fun::lang();

                if ($check_language == 'km') {
                    $edit = "កែសម្រួល";
                } else {
                    $edit = "Edit";
                }
                $del        = '';
                $btn_edit   = '';
                $btn_level  = '';
                $btn_level  = '<a href="#" class="' . ($data->level == 1 ? 'button_primary' : 'button_info') . '" data-id="' . $data->id . '" title="set" onclick="set_root(' . $data->id . ')">
                     ' . ($data->level == 1 ? '<span class="fa fa-check"></span> Root ' : '<span class="fa fa-thumb-tack"></span> Sub') . '</a>';

                $btn_edit = '<a href="' . route('hr-company-structure.edit', encrypt($data->id)) . '" class="button_warning"><i class="fa fa-edit"></i> ' . $edit . '</a>';
                return $btn_edit . ' ' . $btn_level;
            })
            ->addColumn('parent_id', function ($data) {
                @$p = HrCompanyModel::find($data->parent_id);
                return @$p->name;
            })
            ->rawColumns(['action' => 'action', 'complete_status' => 'complete_status'])
            ->make(true);
    }

    public function create()
    {
        $parent = HrCompanyModel::pluck('name', 'id');
        return view('hr.company-structure.create', compact('parent'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
        ]);

        $com            = new HrCompanyModel();
        $com->name      = $request->name;
        $com->parent_id = $request->parent;
        $com->note      = $request->note;
        $com->save();
        return redirect()->route('hr-company-structure.index')->with('success', 'Structure was added successful...');
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $company = HrCompanyModel::find(decrypt($id));
        $parent = HrCompanyModel::where('id', '!=', $company->id)->pluck('name', 'id');
        return view('hr.company-structure.edit', compact('company', 'parent'));
    }

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

        $com            = HrCompanyModel::find($id);
        $com->name      = $request->name;
        $com->parent_id = $request->parent_id;
        $com->note      = $request->note;
        $com->update();

        return redirect()->route('hr-company-structure.index')->with('success', 'Structure was updated successful...');
    }

    public function destroy($id)
    {
        //
    }
    public function org_chart()
    {
        $parent  = HrCompanyModel::orderBy('parent_id')->groupBy('parent_id')->get();
        return view('hr.company-structure.org_chart', compact('parent'));
    }


    public function change_level_root(Request $request)
    {
        HrCompanyModel::where('level', 1)->update(['level' => 0]);
        HrCompanyModel::where('id', $request->id)->update(['level' => 1]);
        return response()->json(['status' => 200, 'success' => 'Status change successfully.']);
    }
}
