<?php namespace Manage\Lib;
use Dever;
class Role extends Auth
{
    public function update($db, $data)
    {
        if ($data['auth']) {
            $auth = explode(',', $data['auth']);
            $data['auth'] = [];
            $module = $menu = $func = [];
            foreach ($auth as $k => $v) {
                if (!strstr($v, '-')) {
                    if (strstr($v, 'v')) {
                        $menu[] = trim($v, 'v');
                    } else {
                        $func[] = $v;
                    }
                    $data['auth'][] = $v;
                }
            }
            if ($func) {
                $funcData = Dever::db('menu_func', 'manage')->select(array('id' => ['in', $func]), ['group' => 'menu_id']);
                foreach ($funcData as $k => $v) {
                    $menu[] = $v['menu_id'];
                }
                $menuData = Dever::db('menu', 'manage')->select(array('id' => ['in', $menu]), ['group' => 'module_id']);
                foreach ($menuData as $k => $v) {
                    $module[] = $v['module_id'];
                }
            }
            $data['auth'] = implode(',', $data['auth']);
            $data['menu'] = implode(',', $menu);
            $data['module'] = implode(',', $module);
        }
        return $data;
    }

    public function getAuthData()
    {
        $result = [];
        $extend = Dever::load('util', 'manage')->extend();
        if ($extend && $extend['system_id']) {
            $system_id = $extend['system_id'];
        } else {
            $system_id = 1;
        }
        $info = Dever::db('system', 'manage')->find($system_id);
        $where = [];
        $where['system'] = $info['key'];
        $module = Dever::db('system_module', 'manage')->select($where);
        foreach ($module as $k => $v) {
            $result[$k]['value'] = 's-' . $v['id'];
            $result[$k]['label'] = $v['name'];
            $result[$k]['children'] = Dever::db('menu', 'manage')->tree(array('module_id' => $v['id'], 'show' => ['<', '3']), ['parent_id', '0', 'id'], [$this, 'getAuthInfo'], ['col' => 'id,name as label,parent_id,`key`,func']);
        }
        return $result;
    }
    public function getAuthInfo($k, $info)
    {
        if ($info['func'] == 1) {
            $info['value'] = 'm-' . $info['id'];
            $info['children'] = Dever::db('menu_func', 'manage')->select(['menu_id' => $info['id']], ['col' => 'id as value,name as label']);
            if (!$info['children']) {
                return [];
            }
        } else {
            $info['value'] = 'v' . $info['id'];
        }
        return $info;
    }
    # 展示系统
    public function showSystem($data)
    {
        return Dever::db('system', 'manage')->show(array('id' => ['in', $data]));
    }
    # 展示系统模块
    public function showModule($data)
    {
        return Dever::db('system_module', 'manage')->show(array('id' => ['in', $data]));
    }
    # 展示菜单
    public function showMenu($data)
    {
        return Dever::db('menu', 'manage')->show(array('id' => ['in', $data]));
    }
    # 展示权限
    public function showFunc($data)
    {
        return Dever::db('menu_func', 'manage')->show(array('id' => ['in', $data]));
    }
}