<?php namespace Manage\Api;
use Dever;
use Manage\Lib\Auth;
class Menu extends Auth
{
    public function info()
    {
        # 如果后续增加Root,就要这里置为false
        $this->top = true;
        $top = Dever::db('menu', 'manage')->select(array('parent_id' => '0', 'module_id' => $this->user['select']['module_id']));
        $result = $menu = array();
        $result[] = array
        (
            'path' => '/',
            'name' => 'Root',
            'component' => 'Layout',
            'meta' => array
            (
                'title' => '首页',
                'icon' => 'home-2-line',
                'breadcrumbHidden' => true,
            ),
            'children' => array
            (
                array
                (
                    'path' => 'index',
                    'name' => 'Index',
                    'component' => '@/dever/index/index',
                    'meta' => array
                    (
                        'title' => '控制台',
                        'icon' => 'home-2-line',
                        'noClosable' => true,
                    )
                ),
            )
        );
        $this->opened = array();
        foreach ($top as $v) {
            $v = $this->getMenu($v, '');
            if ($v) {
                $result[] = $v;
            }
        }
        return array('list' => $result, 'opened' => $this->opened);
    }
    private function getMenu($v, $parent = '')
    {
        $info = array
        (
            'path' => $parent ? '/' . $parent . '/' . $v['key'] : $v['key'],
            'name' => $parent ? $parent . '_' . $v['key'] : $v['key'],
            'meta' => array
            (
                'title' => $v['name'],
                'icon' => $v['icon'],
                //'noClosable' => true,
                'breadcrumbHidden' => false,
                'dynamicNewTab' => true,
            )
        );
        if ($v['show'] > 1) {
            $info['meta']['hidden'] = true;
        }
        if (isset($v['active']) && $v['active']) {
            $info['meta']['activeMenu'] = $v['active'];
        }
        if ($v['parent_id'] <= 0) {
            if ($this->top) {
                $info['path'] = '/' . $v['key'];
            } else {
                $this->top = true;
                $info['path'] = '/';
            }
            $info['component'] = 'Layout';
        }
        $where = array('parent_id' => $v['id'], 'module_id' => $this->user['select']['module_id']);
        $child = Dever::db('menu', 'manage')->select($where);
        if ($child) {
            foreach ($child as $v1) {
                if ($v1['level'] == 3 && $v1['show'] <= 2 && $this->checkMenu($v1['id'])) {
                    continue;
                }
                if (!$parent) {
                    $this->opened[] = '/' . $v['key'] . '/' . $v1['key'];
                }
                $children = $this->getMenu($v1, $v['key']);
                if ($children) {
                    $info['children'][] = $children;
                }
            }
            if (empty($info['children'])) {
                return array();
            }
        } elseif ($v['path']) {
            $info['component'] = '@/dever/page/' . $v['path'];
        }
        if (!$child) {
            if ($v['level'] == 3 && $v['show'] <= 2 && $this->checkMenu($v['id'])) {
                return false;
            }
        }
        return $info;
    }
}