<?php

namespace app\admin\controller;

use think\Controller;
use think\Lang;

class AdminControl extends Controller
{

    /**
     * 管理员资料 name id group
     */
    protected $admin_info;

    public function _initialize()
    {
        if (in_array(cookie('ds_admin_lang'), array('zh-cn', 'en-us'))) {
            config('default_lang', cookie('ds_admin_lang'));
        }
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '.php');
        $this->admin_info = $this->systemLogin();
        if ($this->admin_info['admin_id'] != 1) {
            // 验证权限
//            $this->checkPermission();
        }
        $this->setMenuList();
    }

    /**
     * 取得当前管理员信息
     *
     * @param
     * @return 数组类型的返回结果
     */
    protected final function getAdminInfo()
    {
        return $this->admin_info;
    }

    /**
     * 系统后台登录验证
     *
     * @param
     * @return array 数组类型的返回结果
     */
    protected final function systemLogin()
    {
        $admin_info = array(
            'admin_id' => session('admin_id'),
            'admin_name' => session('admin_name'),
            'admin_group_id' => session('admin_group_id'),
            'admin_is_super' => session('admin_is_super'),
            'admin_pid' => session('admin_pid'),
        );
        if (empty($admin_info['admin_id']) || empty($admin_info['admin_name']) || !isset($admin_info['admin_group_id']) || !isset($admin_info['admin_is_super'])) {
            $this->redirect('Admin/Login/index');
        }

        return $admin_info;
    }

    /**
     * 侧边栏
     */
    public function setMenuList()
    {
        header("content-type:text/html;charset=utf-8");//设置编码
        $menu_list = $this->menuList();
        //权限组单组管理(已有权限)
        $admin_group_model = model('admingroup');
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => session('admin_group_id')));
        $hlimit = ds_decrypt($group_info['group_limits'], MD5_KEY . md5($group_info['group_name']));
        $allkzq = $group_info['group_limits'] = explode('|', $hlimit);
        for ($i=0; $i < count($allkzq); $i++) { 
            $oallkzq[] = strtolower($allkzq[$i]);
        }

        $this->assign('gupinfo',$oallkzq);
        $this->assign('menu_list', $menu_list);
    }

    //所有权限
    public function qxhans(){
        //权限组单组管理(已有权限)
        $admin_group_model = model('admingroup');
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => session('admin_group_id')));
        $hlimit = ds_decrypt($group_info['group_limits'], MD5_KEY . md5($group_info['group_name']));
        $allkzq = $group_info['group_limits'] = explode('|', $hlimit);
        for ($i=0; $i < count($allkzq); $i++) { 
            $oallkzq[] = strtolower($allkzq[$i]);
        }

        return $oallkzq;
    }

    /**
     * 当前选中的栏目
     */
    protected function setAdminCurItem($curitem = '')
    {
        $this->assign('admin_item', $this->getAdminItemList());
        $this->assign('curitem', $curitem);
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        return array();
    }

    /**
     * 侧边栏列表
     */
    function menuList()
    {
        return array(
            'dashboard_manage' => array(
                'name' => 'dashboard_manage',
                'text' => lang('ds_dashboard_manage'),
                'children' => array(
                    'wlcome' => array(
                        'text' => lang('ds_welcome'), 'url' => url('Wlcome/index'),
                    ),
                ),
            ),
            'setting_manage' => array(
                'name' => 'setting_manage',
                'text' => lang('ds_setting_manage'),
                'children' => array(
                    'config' => array(
                        'text' => lang('ds_config'),
                        'url' => url('Config/index'),
                    ),
                    'db' => array(
                        'text' => lang('ds_db'),
                        'url' => url('Db/index'),
                    ),
                    'adminlog' => array(
                        'text' => lang('ds_adminlog'),
                        'url' => url('Adminlog/index'),
                    ),
                ),
				'url' => url('Config/index'),
				'children' => array(
					'config' => array(
						'text' => lang('ds_config'),
						'url' => url('Config/index'),
					),
					'db' => array(
						'text' => lang('ds_db'),
						'url' => url('Db/index'),
					),
					'adminlog' => array(
						'text' => lang('ds_adminlog'),
						'url' => url('Adminlog/index'),
					),
				),
            ),
            'personnel_manage' => array(
                'name' => 'personnel_manage',
                'text' => lang('ds_personnel_manage'),
                'children' => array(
                    'member' => array(
                        'text' => lang('ds_member'),
                        'url' => url('Member/index'),
                    ),
                    'admin' => array(
                        'text' => lang('ds_admin'),
                        'url' => url('Admin/index'),
                    ),
                    'admingroup' => array(
                        'text' => lang('ds_admin_group'),
                        'url' => url('Admingroup/index'),
                    ),
                ),
            ),
            'content_manage' => array(
                'name' => 'content_manage',
                'text' => lang('ds_content_manage'),
                'children' => array(
                    'product' => array(
                        'text' => lang('ds_product'),
                        'url' => url('Product/index'),
                    ),
                    'adv' => array(
                        'text' => lang('ds_adv'),
                        'url' => url('Adv/adv_manage'),
                    ),
                ),
            ),
            'operation_manage' => array(
                'name' => 'operation_manage',
                'text' => lang('ds_operation_manage'),
                'children' => array(
                    'message' => array(
                        'text' => lang('ds_message'),
                        'url' => url('Message/index'),
                    ),
                ),
            ),
            
        );
    }

    /**
     * 记录系统日志
     *
     * @param $lang 日志语言包
     * @param $state 1成功0失败null不出现成功失败提示
     * @param $admin_name
     * @param $admin_id
     */
    protected final function log($lang = '', $state = 1, $admin_name = '', $admin_id = 0)
    {
        if ($admin_name == '') {
            $admin_name = session('admin_name');
            $admin_id = session('admin_id');
        }
        $data = array();
        if (is_null($state)) {
            $state = null;
        } else {
            $state = $state ? '' : lang('nc_fail');
        }
        $data['content'] = $lang . $state;
        $data['admin_name'] = $admin_name;
        $data['createtime'] = TIMESTAMP;
        $data['admin_id'] = $admin_id;
        $data['ip'] = request()->ip();
        $data['url'] = request()->controller() . '&' . request()->action();
        $data['lang'] = config('default_lang');
        return db('adminlog')->insertGetId($data);
    }
}

?>
