<?php namespace Manage\Api;
use Dever;
use Manage\Lib\Auth;
class Menu extends Auth
{
    public function info()
    {
        $this->top = false;
        $top = Dever::db('menu')->select(array('parent_id' => '0', 'system_id' => $this->user['select']['system_id']));
        $result = $menu = array();
        foreach ($top as $v) {
            $menu = $this->getMenu($v);
        }
        if ($menu) {
            $result[] = $menu;
        }
        $result[] = array
        (
            'path' => '/:pathMatch(.*)*',
            'component' => '@/views/403',
            'name' => 'NotFound',
            'meta' => array
            (
                'hidden' => true,
            )
        );
        return array('list' => $result);
    }
    private function getMenu($v, $parent = '')
    {
        $info = array
        (
            'path' => $parent ? '/' . $parent . '/' . $v['key'] : $v['key'],
            'name' => $v['key'],
            'meta' => array
            (
                'title' => $v['name'],
                'icon' => $v['icon'],
                //'noClosable' => true,
                'breadcrumbHidden' => true,
                'dynamicNewTab' => true,
            )
        );
        if ($v['show'] == 2) {
            $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'], 'system_id' => $this->user['select']['system_id']);
        $child = Dever::db('menu')->select($where)->fetchAll();
        if ($child) {
            foreach ($child as $v1) {
                if ($v1['func'] == 1 && $this->checkMenu($v1['id'])) {
                    continue;
                }
                $info['children'][] = $this->getMenu($v1, $v['key']);
            }
        } elseif ($v['path']) {
            $info['component'] = '@/views/page/' . $v['path'];
        }
        return $info;
    }
}