<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Admin\Menus;
use App\Services\Admin\Acl;

class BaseController extends Controller
{
    protected $_user;
    protected $_serviceAdminRole;

    public function __construct() {
        $this->middleware(function ($request, $next) {
            $this->_user = Auth::guard('admin')->user();
            view()->share('_user',$this->_user);
            return $next($request);
        });
    }

    public function getMenus()
    {
        if($this->_user['is_root']) {
            $obj = new Menus();
            $menus = $obj->search(array('level'=>2,'display'=>1),$orderby=array('sort'=>'desc'),$pagesize = 100000);
            $menus = $menus->toArray();
            $menus = list_to_tree($menus['data']);
        }else{
            $obj = new Acl();
            $data = $obj->getRoleMenu($this->_user['admin_role_id']);
            $menus = list_to_tree($data);
        }
        return $menus;
    }
}
