<?php
namespace Admin\Controller;
use Think\Controller;

class AuthController extends BasicController {
    //用户管理
    public function userList() {
        $Adminuser = M('admin');

        $Role= M('role');
        $count = $Adminuser->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $list = $Adminuser->order('id desc')->page($_GET['p'].',20')->select();
        $Role= M('role');
        $rolelist = $Role->select();
        $this->assign('rolelist',$rolelist);
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('title','用户管理');
        $this->assign('parentInfo',$this->parentInfo);
    	$this->display();
    }

    //添加后台管理员
    public function addUser(){
        $Adminuser = M('admin');
        $Role = M('role');

        if(IS_POST) {
            $data['username'] = I('post.admin_name','','htmlspecialchars');
            $data['password'] = md5(I('post.admin_password','','htmlspecialchars'));
            $data['role'] = '1';
            $Adminuser->add($data);
            echo 1;
        }

        $roleinfo = $Role->select();
        $this->assign('roleinfo', $roleinfo);
        $this->assign('title','添加用户');
        $this->assign('parentInfo',$this->parentInfo);
        $this->display();
    }

    //添加权限
    public function add_auth() {
        $id = I('id', '', 'htmlspecialchars');

        $Role = M('role');
        $Auth = D('auth');

        if(I('post.id')) {
            $authid = I('auth', '', 'htmlspecialchars');
            $id = I('id', '', 'htmlspecialchars');

            $data['auth'] = implode(',', $authid);

            $Role->where("id=$id")->save($data);
            echo 1;

            exit(0);
        }

        $roleinfo = $Role->where("id='$id'")->find();

        $roleinfo['auth'] = explode(',', $roleinfo['auth']);
        $authinfo = $Auth->sel_nav(0);

        $this->assign('roleinfo', $roleinfo);
        $this->assign('authinfo', $authinfo);
        $this->assign('id',$id);

        $this->assign('action_name','roleList');
        $this->assign('parentInfo',$this->parentInfo);
        $this->display();
    }
    //修改用户角色
    public function updrole(){
        $data['role'] = I('roleid','','htmlspecialchars');
        $uid = I('uid','','htmlspecialchars');
        $Adminuser = M('admin');
        $Adminuser->where("id='$uid'")->save($data);
        echo 1;
    }

    //删除用户
    public function Userdelete(){
        if(session('role')!=1){
            echo -1;exit();
        }
        $Adminuser = M('admin');
        $uid = I('uid','','htmlspecialchars');
        $Adminuser->where("id='$uid'")->delete();
    }
}
