<?php

namespace app\home\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 . 'home/lang/' . config('default_lang') . '.php');
        $this->admin_info = $this->systemLogin();
        if ($this->admin_info['service_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(
            'service_id' => session('service_id'),
            'service_name' => session('service_name'),
            'service_group_id' => session('service_group_id'),
            'service_is_super' => session('service_is_super'),
            'service_pid' => session('service_pid'),
        );

        if (empty($admin_info['service_id']) || empty($admin_info['service_name']) || !isset($admin_info['service_group_id'])) {
            $this->redirect('home/Login/index');
        }

        return $admin_info;
    }

    /**
     * 侧边栏
     */
    public function setMenuList()
    {
        $menu_list = $this->menuList();
        //权限组单组管理(已有权限)
        $admin_group_model = model('admingroup');
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => session('service_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('service_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'),
                    ),
                ),
				'url' => url('Config/index'),
				'children' => array(
					'config' => array(
						'text' => lang('ds_config'),
						'url' => url('Config/index'),
					),
				),
            ),
            'personnel_manage' => array(
                'name' => 'personnel_manage',
                'text' => lang('ds_personnel_manage'),
                'children' => array(
                    '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' => "常用语",
                        'url' => url('Product/index'),
                    ),
                    'News' => array(
                        'text' => lang('ds_product'),
                        'url' => url('News/index'),
                    ),
                ),
            ),
            'operation_manage' => array(
                'name' => 'operation_manage',
                'text' => lang('ds_operation_manage'),
                'children' => array(
                    'message' => array(
                        'text' => lang('ds_message'),
                        'url' => url('Message/index'),
                    ),
                    'Service' => array(
                        'text' => lang('ds_service'),
                        'url' => url('Service/index'),
                    ),
                    'records' => array(
                        'text' => lang('ds_records'),
                        'url' => url('Jobcv/index'),
                    ),
                    'Visitkf' => array(
                        'text' => "访客列表",
                        'url' => url('Visitkf/index'),
                    ),
                    'Glservice' => array(
                        'text' => "客服统计",
                        'url' => url('Glservice/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('service_name');
            $admin_id = session('service_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);
    }
}

?>
