<?php namespace Manage\Api;
use Dever;
use Manage\Lib\Auth;
class Admin extends Auth
{
    public function info()
    {
        $this->user['system']['show'] = true;
        $this->user['system']['id'] = $this->user['select']['system_id'];
        $this->user['system']['name'] = '当前系统';
        $this->user['system']['list'] = $this->system();
        return $this->user;
    }

    # 获取当前的系统列表
    public function system()
    {
        $where = array();
        if ($this->user['auth']['system']) {
            $where['id'] = array('in', $this->user['auth']['system']);
        }
        $result = array();
        $system = Dever::db('system')->select($where);
        $i = 0;
        foreach ($system as $k => $v) {
            $child = Dever::db($v['relation_table'])->select([])->fetchAll();
            if ($child) {
                $data = array();
                foreach ($child as $k1 => $v1) {
                    $v1['select'] = false;
                    if ($v['id'] == $this->user['select']['system_id'] && $v1['id'] == $this->user['select']['relation_id']) {
                        $this->user['system']['name'] = $v1['name'];
                        $v1['select'] = true;
                    }
                    $key = $v['id'] . '-' . $v1['id'];
                    if ($this->user['system_relation']) {
                        if (strstr($this->user['system_relation'], $key)) {
                            $data[] = $v1;
                        }
                    } else {
                        $data[] = $v1;
                    }
                }
                if ($data) {
                    $result[$i] = $v;
                    $result[$i]['child'] = $data;
                    $i++;
                }
            }
        }
        if ($i <= 1) {
            $this->user['system']['show'] = false;
        }
        return $result;
    }

    # 根据角色获取子系统
    public function getSystem($value = false)
    {
        if (!$value) {
            $result['system_relation']['option'] = array();
            return $result;
        }
        $result = array();
        $role = Dever::db('role')->select(array('id' => array('in', $value)))->fetchAll();
        if ($role) {
            $info = $system = array();
            foreach ($role as $k => $v) {
                if ($v['system']) {
                    $child = Dever::db('system')->select(array('id' => array('in', $v['system'])))->fetchAll();
                    if ($child) {
                        foreach ($child as $k1 => $v1) {
                            if (isset($info[$v1['id']])) {
                                continue;
                            }
                            $info[$v1['id']] = true;
                            $v1['value'] = 's-' . $v1['id'];
                            $v1['label'] = $v1['name'];
                            $v1['children'] = array();
                            $data = Dever::db($v1['relation_table'])->select([], array('col' => 'concat('.$v1['id'].', "-", id) as value, name as label'))->fetchAll();
                            if ($data) {
                                $v1['children'] = array_merge($v1['children'], $data);
                            }
                            $system[] = $v1;
                        }
                    }
                }
            }
            $result['system_relation']['option'] = $system;
        }
        
        return $result;
    }

    # 切换系统
    public function setSystem()
    {
        $system_id = Dever::input('system_id');
        $this->checkSystem($this->user['select']['system_id']);
        $relation_id = Dever::input('relation_id');
        if ($this->user['system_relation'] && !strstr($this->user['system_relation'], $relation_id)) {
            Dever::error('无系统权限');
        }
        $result = Dever::load('common')->token($this->user['id'], $this->user['mobile'], $system_id, $relation_id);
        return $result;
    }
}