<?php
namespace Manage\Src;

use Dever;

class Top
{
    /**
     * 获取所有的头部菜单权限
     *
     * @return array
     */
    public function all()
    {
        $top = Dever::db('manage/top')->main();

        $result = array();
        if ($top) {
            $child = Dever::db('manage/top')->child();

            foreach ($top as $k => $v) {
                $k = str_replace('/', '-', $k);
                $result[$k] = $v;
                if (isset($child[$v['id']])) {
                    $c = 0;
                    foreach ($child[$v['id']] as $i => $j) {
                        $i = str_replace('/', '-', $i);
                        $result[$k]['child'][$i] = $j;
                        $c++;
                    }

                    if (!isset($result[$k]['child'])) {
                        unset($result[$k]);
                    }
                }
            }
        }

        return $result;
    }

    /**
     * menu 从数据库中得到所有权限
     *
     * @return array
     */
    public function get($auth = true)
    {
        # 得到当前的项目和表
        $config = Dever::load('manage/database.config');

        if (empty($config['top']) || is_array($config['top'])) {
            return array();
        }

        $auth = true;
        if ($auth) {
            # 需要验证权限
            $auth = Dever::load('manage/auth.top');
        }

        $top = Dever::db('manage/top')->main();

        if ($top) {
            $child = Dever::db('manage/top')->child();

            foreach ($top as $k => $v) {
                /*
                if ($auth && strpos($auth, $k . '_') === false) {
                    unset($top[$k]);
                    continue;
                }
                */

                if (isset($child[$v['id']])) {
                    # 如果已经保存了，就直接取出来吧
                    $info = Dever::load('manage/auth.getTop', $v['key']);
                    if ($info) {
                        $top[$k]['name'] = $info['name'];
                    }

                    $c = 0;
                    foreach ($child[$v['id']] as $i => $j) {
                        $i = str_replace('/', '-', $i);
                        if (($auth && strpos($auth . ',', $i . ',') === false) || ($info && $info['id'] == $j['id'])) {

                        } elseif (!$info && $c == 0) {
                            $top[$k]['name'] = $j['name'];
                            Dever::load('manage/auth._setTop', $j);
                            $c++;
                        } else {
                            $top[$k]['child'][$i] = $j;
                            $c++;
                        }
                    }

                    if (!isset($top[$k]['child'])) {
                        unset($top[$k]);
                    }
                }
            }
        }

        //print_r($top);die;

        return $top;
    }

    /**
     * 更新数据到数据库
     *
     * @return array
     */
    public function update($param = array())
    {
        if (isset($param['key'])) {
            $info = Dever::db('manage/top')->key(array('where_key' => $param['key']));
            //print_r($info);die;
            if (!$info) {
                $update['add_key'] = $param['key'];
                $update['add_name'] = $param['name'];
                $update['add_top_id'] = isset($param['top']) ? $param['top'] : -1;
                $update['add_value'] = $param['value'];
                $update['add_state'] = isset($param['state']) ? $param['state'] : 1;

                $info['id'] = Dever::db('manage/top')->insert($update);
            } else {
                $update['set_name'] = $param['name'];
                $update['set_value'] = $param['value'];
                $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
                $update['where_id'] = $info['id'];
                Dever::db('manage/top')->update($update);
            }

            return $info['id'];
        }

        return false;
    }

    /**
     * 同步子权限更新到数据库
     *
     * @return array
     */
    public function sync($id, $name, $param = array())
    {
        if ($id && isset($param['top'])) {
            $key = $param['top']['key'];

            $info = Dever::db('manage/top')->key(array('where_key' => $key));

            if ($info) {
                $update['value'] = Dever::input('where_id', $id);

                $update['name'] = Dever::param('name', $param);

                $update['top'] = $info['id'];

                $update['key'] = $key . '_' . $update['value'];

                $update['state'] = Dever::param('state', $param);

                $this->update($update);
            }
        }
    }

    /**
     * 更新当前使用的top菜单
     *
     * @return array
     */
    public function update_action_api($id = false)
    {
        $id = $id ? $id : Dever::input('id');

        $id = intval($id);
        if ($id > 0) {
            # 需要验证权限
            $auth = Dever::load('manage/auth.top');

            $info = Dever::db('manage/top')->one($id);

            $info['key'] = str_replace('/', '-', $info['key']);
            if ($info && $info['top_id'] > 0 && (!$auth || strpos($auth . ',', $info['key'] . ',') !== false)) {
                Dever::load('manage/auth._setTop', $info);
            }

            return $info['value'];
        }
    }
}
