<?php
namespace app\home\controller;

use think\Lang;

class Admingroup extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/admingroup.lang.php');
    }

    /**
     * 管理权限组
     * @return mixed
     */
    public function index()
    {
        $model_admingroup = Model('admingroup');
        $condition = array();
        $condition['lang'] = config('default_lang');
        $condition['suosgs'] = session('service_pid');
        $admingroup_list = $model_admingroup->getAdminGroupList($condition,'*');
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('admin_group_list', $admingroup_list);
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    public function getControllers($dir) {
        $pathList = glob($dir . '/*.php');
        $res = [];
        foreach($pathList as $key => $value) {
            $res[] = basename($value, '.php');
        }
        return $res;
    }

    /**
     * 添加权限组
     * @return mixed
     */
    public function add()
    {
        if (request()->isPost()) {
            $admin_group_model = model('admingroup');
            //判断权限组名是否存在
            $where['group_name'] = input('post.group_name');
            $where['suosgs'] = session('service_pid');
            if ($admin_group_model->getOneAdmingroup($where)) {
                $this->error(lang('admin_group_existence'));
            }
            $limit_str = '';
            if (is_array($_POST['permission'])) {
                $limit_str = implode('|', $_POST['permission']);
            }
            $data['group_limits'] = ds_encrypt($limit_str, MD5_KEY . md5(input('post.group_name')));
            $data['group_name'] = input('post.group_name');
            $data['lang'] = config('default_lang');
            $data['suosgs'] = session('service_pid');
            $data['addtime'] = TIMESTAMP;
            if ($admin_group_model->addAdminGroup($data)) {
                $this->log(lang('ds_add') . lang('ds_admin_group') . '[' . input('post.group_name') . ']', 1);
                dsLayerOpenSuccess(lang('admin_group_add_succ'));
            } else {
                $this->error(lang('admin_group_add_fail'));
            }
        } else {
            $this->assign('admin_array', 'add');
            $this->assign('limit', $this->permission());
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $admin_group_model = model('admingroup');
        $group_id = intval(input('param.group_id'));
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => $group_id));
        if (empty($group_info)) {
            $this->error(lang('admin_set_admin_not_exists'));
        }
        if (request()->isPost()) {
            $limit_str = '';
            if (is_array($_POST['permission'])) {
                $limit_str = implode('|', $_POST['permission']);
            }
            $limit_str = ds_encrypt($limit_str, MD5_KEY . md5(input('post.group_name')));
            $data['group_limits'] = $limit_str;
            $data['group_name'] = input('post.group_name');
            $update = $admin_group_model->editAdminGroup(array('group_id' => $group_id), $data);
            if ($update) {
                $this->log(lang('ds_edit') . lang('group_limits') . '[' . input('post.group_limits') . ']', 1);
                dsLayerOpenSuccess(lang('admin_group_edit_succ'));
            } else {
                $this->error(lang('admin_group_edit_fail'));
            }
        } else {
            //解析已有权限
            $hlimit = ds_decrypt($group_info['group_limits'], MD5_KEY . md5($group_info['group_name']));
            $group_info['group_limits'] = explode('|', $hlimit);
            $this->assign('admin_array', 'edit');
            $this->assign('group_info', $group_info);
            $this->assign('limit', $this->permission());
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }

    public function del()
    {
        $group_id = input('param.group_id');
        if (empty($group_id)) {
            $this->error(lang('param_error'));
        }
        $result = db('admingroup')->delete($group_id);
        if ($result) {
            ds_json_encode(10000, lang('admin_group_del_succ'));
        } else {
            ds_json_encode(10001, lang('admin_group_del_fail'));
        }
    }

    /**
     * 取得所有权限项
     *
     * @return array
     */
    private function permission()
    {
        $limit = $this->limitList();
        if (is_array($limit)) {
            foreach ($limit as $k => $v) {
                if (is_array($v['child'])) {
                    $tmp = array();
                    foreach ($v['child'] as $key => $value) {
                        $controller = (!empty($value['controller'])) ? $value['controller'] : $v['controller'];
                        if (strpos($controller, '|') == false) {//controller参数不带|
                            $limit[$k]['child'][$key]['action'] = rtrim($controller . '.' . str_replace('|', '|' . $controller . '.', $value['action']), '.');
                        } else {//controller参数带|
                            $tmp_str = '';
                            if (empty($value['action'])) {
                                $limit[$k]['child'][$key]['action'] = $controller;
                            } elseif (strpos($value['action'], '|') == false) {//action参数不带|
                                foreach (explode('|', $controller) as $v1) {
                                    $tmp_str .= "$v1.{$value['action']}|";
                                }
                                $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
                            } elseif (strpos($value['action'], '|') != false && strpos($controller, '|') != false) {//action,controller都带|，交差权限
                                foreach (explode('|', $controller) as $v1) {
                                    foreach (explode('|', $value['action']) as $v2) {
                                        $tmp_str .= "$v1.$v2|";
                                    }
                                }
                                $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
                            }
                        }
                    }
                }
            }
            return $limit;
        } else {
            return array();
        }
    }

    /*
    * 权限选择列表
    */
    function limitList()
    {
        $_limit = array(
            array('name' => lang('ds_dashboard_manage'), 'child' => array(
                array('name' => lang('ds_welcome'), 'action' => null, 'controller' => 'Wlcome'),
                array('name' => lang('ds_welcome'), 'action' => "index", 'controller' => 'Wlcome'),
            )),
            array('name' => lang('ds_config'), 'child' => array(
                array('name' => lang('ds_config'), 'action' => null, 'controller' => 'Config'),
                array('name' => lang('ds_wzconfig'), 'action' => "index", 'controller' => 'Config'),
                array('name' => "接入添加", 'action' => "add", 'controller' => 'Config'),
                array('name' => "接入编辑", 'action' => "edit", 'controller' => 'Config'),
                array('name' => "接入删除", 'action' => "del", 'controller' => 'Config'),
            )),
            array('name' => lang('ds_admin'), 'child' => array(
                array('name' => lang('ds_admin'), 'action' => null, 'controller' => 'Admin'),
                array('name' => lang('ds_gladmin'), 'action' => "index", 'controller' => 'Admin'),
                array('name' => lang('ds_addadmin'), 'action' => "add", 'controller' => 'Admin'),
                array('name' => lang('ds_edadmin'), 'action' => "edit", 'controller' => 'Admin'),
                array('name' => lang('ds_dladmin'), 'action' => "del", 'controller' => 'Admin'),
            )),
            array('name' => lang('ds_admin_group'), 'child' => array(
                array('name' => lang('ds_admin_group'), 'action' => null, 'controller' => 'AdminGroup'),
                array('name' => lang('ds_admin_glgroup'), 'action' => "index", 'controller' => 'AdminGroup'),
                array('name' => lang('ds_admin_addgroup'), 'action' => "add", 'controller' => 'AdminGroup'),
                array('name' => lang('ds_admin_edgroup'), 'action' => "edit", 'controller' => 'AdminGroup'),
                array('name' => lang('ds_admin_dlgroup'), 'action' => "del", 'controller' => 'AdminGroup'),
            )),
            array('name' => "常用语", 'child' => array(
                array('name' => "常用语", 'action' => null, 'controller' => 'Product'),
                array('name' => "常用语列表", 'action' => "index", 'controller' => 'Product'),
                array('name' => "常用语添加", 'action' => 'add', 'controller' => 'Product'),
                array('name' => "常用语编辑", 'action' => 'edit', 'controller' => 'Product'),
                array('name' => "常用语删除", 'action' => 'del', 'controller' => 'Product'),
            )),//常用语
            array('name' => lang('ds_product'), 'child' => array(
                array('name' => lang('ds_product'), 'action' => null, 'controller' => 'News'),
                array('name' => lang('ds_product'), 'action' => "index", 'controller' => 'News'),
                array('name' => lang('ds_addproduct'), 'action' => 'add', 'controller' => 'News'),
                array('name' => lang('ds_edproduct'), 'action' => 'edit', 'controller' => 'News'),
                array('name' => lang('ds_dlproduct'), 'action' => 'del', 'controller' => 'News'),
            )),//知识库
            array('name' => lang('ds_message'), 'child' => array(
                array('name' => lang('ds_message'), 'action' => null, 'controller' => 'Message'),
                array('name' => lang('ds_glmessage'), 'action' => "index", 'controller' => 'Message'),//留言表
                array('name' => lang('ds_ckmessage'), 'action' => "reply", 'controller' => 'Message'),
                array('name' => lang('ds_dlmessage'), 'action' => "del", 'controller' => 'Message'),
            )),
            array('name' => lang('ds_kfchat'), 'child' => array(
                array('name' => lang('ds_kfchat'), 'action' => null, 'controller' => 'Service'),
                array('name' => lang('ds_kfchat'), 'action' => "index", 'controller' => 'Service'),
            )),//对话平台
            array('name' => lang('ds_records'), 'child' => array(
                array('name' => lang('ds_records'), 'action' => null, 'controller' => 'Jobcv'),
                array('name' => lang('ds_glrecords'), 'action' => "index", 'controller' => 'Jobcv'),
            )),//历史记录
            array('name' => "访问列表", 'child' => array(
                array('name' => "访问列表", 'action' => null, 'controller' => 'Visitkf'),
                array('name' => "访问列表", 'action' => "index", 'controller' => 'Visitkf'),
            )),//访客列表 
            array('name' => "客服统计", 'child' => array(
                array('name' => "客服统计", 'action' => null, 'controller' => 'Glservice'),
                array('name' => "客服统计", 'action' => "index", 'controller' => 'Glservice'),
            )),//客服统计
        );

        return $_limit;
    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('Admingroup/index')
            ), array(
                'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('".url('Admingroup/add')."','".lang('ds_add')."')"

            ),
        );
        return $menu_array;
    }
}