<?php

namespace App\Http\Controllers\Administrator;
use App\DepartmentModel;
use App\User;
use App\UserGroupModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Fun\Fun;
use Gate;
use Yajra\DataTables\DataTables;
class UserGroupController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:User-Group-List');
         $this->middleware('permission:User-Group-Create', ['only' => ['create','store']]);
         $this->middleware('permission:User-Group-Edit', ['only' => ['edit','update']]);
         $this->middleware('permission:User-Group-Delete', ['only' => ['delete_user_group']]);
    }
    public function index()
    {
        return view('administrator.usergroup.index');
    }

    public function create()
    {
        $department = DepartmentModel::pluck('department_name','id')->all();
        $group = UserGroupModel::pluck('group_name','id')->all();
        return view('administrator.usergroup.create',compact('group','department'));
    }

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

        $usergroup= new UserGroupModel();
        $usergroup->parent_id = $request->parent_id;
        $usergroup->group_name = $request->group_name;
        $usergroup->department_id = $request->department_id;
        $usergroup->note = $request->note;
        if($usergroup->save()){
            return redirect()->route('user-group.index')->with('success','Lead has been created....');
        }else{
            return redirect()->route('user-group.index')->with('danger','Failed create lead !!!');
        }

    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $usergorup = UserGroupModel::find($id);
        $group = UserGroupModel::where('id','!=',$usergorup->id)->pluck('group_name','id')->all();
        return view('administrator.usergroup.edit',compact('usergorup','group'));
    }
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'group_name' => 'required',
        ]);

        $usergroup=  UserGroupModel::find($id);
        $usergroup->parent_id = $request->parent_id;
        $usergroup->group_name = $request->group_name;
        $usergroup->note = $request->note;
        if($usergroup->save()){
            return redirect()->route('user-group.index')->with('success','Lead has been created....');
        }else{
            return redirect()->route('user-group.index')->with('danger','Failed create lead !!!');
        }
    }

    public function destroy($id)
    {
        //
    }
    public function datable_user_group(Request $request)
    {
        $data = UserGroupModel::where('delete_status',0)->get();

        if ($request->isMethod('get')) {

            return DataTables::of($data)

                ->editColumn('parent_id', function ($group) {
                    $p = $group->parent_id;
                    $name = UserGroupModel::find($p);
                    if($name){
                        return $name->group_name?$name->group_name:'N/A';
                    }else{
                        return 'N/A';
                    }
                })
                ->editColumn('note', function ($group) {
                    return $group->note?$group->note:'N/A';
                })
                ->editColumn('department_id', function ($group) {
                    return optional($group->department)->department_name?optional($group->department)->department_name:'N/A';
                })
                ->addColumn('action', function ($group) {
                    $check_language = Fun::lang();
                    if ($check_language == 'km') {
                        $delete = 'លុប';
                        $edit = "កែសម្រួល";
                        $task = "ភារកិច្ច";
                    } else {
                        $delete = 'Delete';
                        $edit = "Edit";
                        $task = "Task";
                    }
                    $btn_edit = '';
                    $btn_delete = '';
                    if(Gate::check('User-Group-Edit')){
                        $btn_edit = '<a href="' . route("user-group.edit", $group->id) . '" title="Edit"><span class="button_info">' . $edit . '</span></a>';
                    }
                    if(Gate::check('User-Group-Delete')){
                        $btn_delete = '<button type="button" class="button_danger delete_products" data-toggle="modal" 
                        data-id="' . $group->id . '" > ' . $delete . ' </button>';
                    }
                    return $btn_edit.$btn_delete;
                })
                ->addIndexColumn()
                ->rawColumns(['action' => 'action'])
                ->make(true);
        }
    }
    public function delete_user_group(Request $request){
        $st = UserGroupModel::find($request->id);
        $st->delete_status = 1;
        $st->update();
    }
}
