<?php
// +----------------------------------------------------------------------
// | 权限菜单
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\auth;
use app\controller\SuperClass\Admin;

class Menu_list extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/auth/menu_list", method="GET|POST", title="权限菜单")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->showlist();
        }
        return $this->display();
    }

    // 菜单列表
    private function showlist() {
        $Db = new \x\Db();
        $param = \x\Request::post();
        $where = $this->_where($param);
        $list = $Db->name('auth_menu')->where($where)->order('path ASC')->select();
        $Db->return();
        $count = count($list);
        foreach ($list as $k=>$v) {
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $list[$k]['status'] = $v['status']==1?'开启':'关闭';
            $list[$k]['display'] = $v['display']==1?'显示':'隐藏';
            $list[$k]['public'] = $v['public']==1?'公共':'过滤';
            $nbsp = '';
            for ($i=1; $i<$v['level']; $i++) {
                $nbsp .= '&nbsp;&nbsp;';
            }
            if (!empty($nbsp)) {
                $list[$k]['name'] = $nbsp.'├─'.$v['name'];
            }
        }
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];
        if (!empty($param['name'])) {
            $where[] = ['name', '=', $param['name']];
        }
        if (!empty($param['controller'])) {
            $where[] = ['controller', '=', $param['controller']];
        }
        if (!empty($param['action'])) {
            $where[] = ['action', '=', $param['action']];
        }

        return $where;
    }

}