<?php
namespace app\admin\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'] = 1;
        $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'] = 1;
            if ($admin_group_model->getOneAdmingroup($where)) {
                $this->error(lang('admin_group_existence'));
            }
            var_dump($_POST['permission']);die;
            $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['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' => lang('ds_fzsconfig'), 'action' => "dump", 'controller' => 'Config'),
                //array('name' => lang('ds_seoconfig'), 'action' => "seo", 'controller' => 'Config'),
            )),
            array('name' => "数据库设置", 'child' => array(
                array('name' => lang('ds_db'), 'action' => null, 'controller' => 'Db'),
                array('name' => lang('ds_bfdb'), 'action' => "index", 'controller' => 'Db'),
                array('name' => lang('ds_hydb'), 'action' => "restore", 'controller' => 'Db'),
            )),
            array('name' => lang('ds_adminlog'), 'child' => array(
                array('name' => lang('ds_adminlog'), 'action' => null, 'controller' => 'AdminLog'),
                array('name' => lang('ds_adminczlog'), 'action' => 'index', 'controller' => 'AdminLog'),
            )),
            array('name' => lang('ds_member'), 'child' => array(
                array('name' => lang('ds_member'), 'action' => null, 'controller' => 'Member'),
                array('name' => lang('ds_glmember'), 'action' => "index", 'controller' => 'Member'),
                array('name' => lang('ds_addmember'), 'action' => "add", 'controller' => 'Member'),
                array('name' => lang('ds_edmember'), 'action' => "edit", 'controller' => 'Member'),
                array('name' => lang('ds_dlmember'), 'action' => "del", 'controller' => 'Member'),
            )),
            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' => lang('ds_product'), 'child' => array(
                array('name' => lang('ds_product'), 'action' => null, 'controller' => 'Product'),
                array('name' => lang('ds_product'), 'action' => "index", 'controller' => 'Product'),
                array('name' => lang('ds_addproduct'), 'action' => 'add', 'controller' => 'Product'),
                array('name' => lang('ds_edproduct'), 'action' => 'edit', 'controller' => 'Product'),
                array('name' => lang('ds_dlproduct'), 'action' => 'del', 'controller' => 'Product'),
            )),
            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'),
            )),
        );

        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;
    }
}