<?php
namespace Smi\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class AuthController extends BaseController {
    /**
     * 分组
     */
    public function group_list()
    {
        $where = 1;
        $order = "`id` DESC";
        $l = 10;//确定每页记录数
        $count = M("group") -> where($where) -> count();// 查询满足要求的总记录数
        // 实例化分页类 传入总记录数和每页显示的记录数($l)
        $Page = new \Think\Page($count, $l);
        $show = $Page -> show();// 分页显示输出
        $list = M("group") -> where($where) -> order($order) -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
        $this -> assign('page', $show);// 赋值分页输出
        // 定义变量并显示模板
        $this -> assign("list", $list);
        $this -> display("group_list");die;
    }

    /**
     * 建立新分组
     */
    public function group_add()
    {
        if ($_POST) {
            // 提交后处理表单
            // 接收表单
            $groupname = I("groupname");
            $auth = I("auth");
            $time = time();
            $ip = $_SERVER['REMOTE_ADDR'];
            // 空值处理
            if (!$groupname) {$this -> error("请填写组名");die;}
            // 查重
            $return = D("Group") -> check_same($groupname);
            if ($return) {$this -> error("该组名已被使用");die;}
            // 权限
            $auth = implode(",", $auth);
            // 数据保存
            $data['groupname'] = $groupname;
            $data['auth'] = $auth;
            $data['create_ip'] = $ip;
            $data['create_time'] = $time;
            $insert_id = M("group") -> add($data);
            $this -> redirect("group_list");die;
        }
        
        // 获取所有入录的节点
        $this->all_node();

        $this -> assign("legend", "建立新分组");
        $this -> display("group_add");die;
    }

    /**
     * 编辑分组
     */
    public function group_edit()
    {
        // 接收 ID
        $id = I("id");
        if (!$id) {$this -> error("系统故障，无法接收 ID");die;}
        if ($_POST) {
        // 提交后处理表单
            // 接收表单
            $groupname = I("groupname");
            $auth = I("auth");
            $time = time();
            $ip = $_SERVER['REMOTE_ADDR'];
            // 空值处理
            if (!$groupname) {$this -> error("请填写组名");die;}
            // 查重
            $return = D("Group") -> check_same($groupname, $id);
            if ($return) {$this -> error("该组名已被使用");die;}
            // 权限
            $auth = implode(",", $auth);
            // 数据保存
            $data['groupname'] = $groupname;

            $data['auth'] = $auth;
            $data['edit_ip'] = $ip;
            $data['edit_time'] = $time;
            $insert_id = M("group") -> where("`id` = $id") -> save($data);
            session('auth',explode(',',  M('group')->where(array('group_id' => session('group_id')))->find()['auth']));// 编辑成功，重新分配权限
            $this -> redirect("group_list");die;
        }
        // 获取该记录信息
        $info = M("group") -> where("`id` = $id") -> find();
        $this -> assign("info", $info);
        // 权限拆分
        $auth_list = explode(",", $info['auth']);

        // 获取所有入录的节点
        $this->all_node();

        $this -> assign("auth_list", $auth_list);
        $this -> assign("legend", "编辑分组“{$info['groupname']}”");
        $this -> display("group_add");die;
    }

    /**
     * 获取权限节点
     */
    private function all_node()
    {
        $one_node_list = M('node')->field('id, title, auth_name, remark')->where(array('pid' => 0, 'level' => 1))->select();
        $two_node_list = M('node')->field('id, pid, title, auth_name, url, remark')->where('pid <> 0 AND level = 2')->select();
        $three_node_list = M('node')->field('pid, title, auth_name, url, remark')->where('pid <> 0 AND level = 3')->select();

        foreach ($one_node_list as $k => &$vo) {
            // 获取二（三）级节点
            $vo['menu_list'] = array();
            foreach ($two_node_list as $vv) {
                if ($vv['pid'] == $vo['id']) {
                    $vo['menu_list'][] = $vv;// 
                    foreach ($three_node_list as $v) {
                        if ($v['pid'] == $vv['id']) {
                            $vo['menu_list'][] = $v;// 
                        }
                    }
                }
            }
        }
        $this->assign('node_list', $one_node_list);
    }

    /**
     * 删除分组
     */
    public function group_del()
    {
        //接收 ID
        $id = I("id");
        if (!$id) {$this -> error("系统故障，无法接收 ID");die;}
        $bool = M("group") -> delete($id);
        $this -> redirect("group_list");die;
    }

    /**
     * 管理员
     */
    public function admin_list()
    {
        $where = "a.`is_business` = 2";
        $order = "a.`login_num` DESC,a.`id` DESC";
        $l = 10;//确定每页记录数
        $count = M("admin") -> join(" a LEFT JOIN `am_group` g ON a.`group_id` = g.`id`") -> where($where) -> count();// 查询满足要求的总记录数
        // 实例化分页类 传入总记录数和每页显示的记录数($l)
        $Page = new \Think\Page($count, $l);
        $show = $Page -> show();// 分页显示输出
        $list = M("admin") -> join(" a LEFT JOIN `am_group` g ON a.`group_id` = g.`id`") -> where($where) -> field("a.*,g.`groupname`") -> order($order) -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
        $this -> assign('page', $show);// 赋值分页输出
        // 定义变量并显示模板
        $this -> assign("list", $list);
        $this -> display("admin_list");die;
    }

    /**
     * 录入新管理员
     */
    public function admin_add()
    {
        if ($_POST) {
            // 提交后处理表单
            // 接收表单
            $username = I("username");
            $password = I("password");
            $group_id = I("group_id");
            $status = I("status");
            $time = time();
            $ip = $_SERVER['REMOTE_ADDR'];
            // 空值处理
            if (!$username) {$this -> error("请填写用户名");die;}
            if (!$password) {$this -> error("请填写密码");die;}
            if ($group_id == "no") {$this -> error("请选择分组");die;}
            // 上帝账号保护
            if ($username == "admin") {$this -> error("禁止使用“admin”");die;}
            // 查重
            $return = D("Admin") -> check_same($username);
            if ($return) {$this -> error("该用户名已被使用");die;}
            // 密码
            $password = md5($password);
            // 数据保存
            $data['username'] = $username;
            $data['password'] = $password;
            $data['group_id'] = $group_id;
            $data['status'] = $status;
            $data['create_ip'] = $ip;
            $data['create_time'] = $time;
            $data['is_business'] = 2;
            $insert_id = M("admin") -> add($data);
            $this -> redirect("admin_list");die;
        }
        // 获取分组列表
        $group_list = D("Group") -> get_list();
        $this -> assign("group_list", $group_list);
        $this -> assign("legend", "录入新管理员");
        $this -> display("admin_add");die;
    }

    /**
     * 编辑管理员
     */
    public function admin_edit()
    {
        // 接收 ID
        $id = I("id");
        if (!$id) {$this -> error("系统故障，无法接收 ID");die;}
        if ($_POST) {
        // 提交后处理表单
            // 接收表单
            $username = I("username");
            $group_id = I("group_id");
            $status = I("status");
            $time = time();
            $ip = $_SERVER['REMOTE_ADDR'];
            // 空值处理
            if (!$username) {$this -> error("请填写用户名");die;}
            if ($group_id == "no") {$this -> error("请选择分组");die;}
            // 上帝账号保护
            if ($username == "admin") {$this -> error("禁止使用“admin”");die;}
            // 查重
            $return = D("Admin") -> check_same($username, $id);
            if ($return) {$this -> error("该用户名已被使用");die;}
            // 数据保存
            $data['username'] = $username;
            $data['group_id'] = $group_id;
            $data['status'] = $status;
            $data['edit_ip'] = $ip;
            $data['edit_time'] = $time;
            $bool = M("admin") -> where("`id` = $id") -> save($data);
            $this -> redirect("admin_list");die;
        }
        // 获取分组列表
        $group_list = D("Group") -> get_list();
        $this -> assign("group_list", $group_list);
        // 获取该记录信息
        $info = M("admin") -> where("`id` = $id") -> find();
        $this -> assign("info", $info);
        $this -> assign("legend", "编辑管理员“{$info['username']}”");
        $this -> display("admin_add");die;
    }

    /**
     * 删除管理员
     */
    public function admin_del()
    {
        //接收 ID
        $id = I("id");
        if (!$id) {$this -> error("系统故障，无法接收 ID");die;}
        $bool = M("admin") -> delete($id);
        $this -> redirect("admin_list");die;
    }

    /**
     * 重置密码
     */
    public function password_reset()
    {
        // 接收 ID
        $id = I("id");
        $password = I("password");
        $time = time();
        $ip = $_SERVER['REMOTE_ADDR'];
        if (!$id) {$this -> error("系统故障，无法接收 ID");die;}
        if (!$password) {$this -> error("请填写密码");die;}
        // 密码
        $password = md5($password);
        // 数据保存
        $data['password'] = $password;
        $bool = M("admin") -> where("`id` = $id") -> save($data);
        $this -> success("重置成功", U("admin_list"));die;
    }
}