<?php
class UserAction extends BackAction{
	public function _initialize() {
        parent::_initialize();  //RBAC 验证接口初始化
    }
	public function index(){
        $role = M('Role')->getField('id,name');
		//dump($role);
//exit;
        $uquantity=M('User')->where(array('username'=>$this->_session('username')))->find();
		$quantity=M('User')->where(array('superior'=>$this->_session('username')))->find();
		
		
		if ($uquantity['username'] == admin)
			{
				//exit("admin"); 
        $map = array();
			}else{
		//exit("ww"); 
		$map = array('superior'=>$this->_session('username'));
		}
		
		
		
        $UserDB = D('User');
        $count = $UserDB->where($map)->count();
        $Page       = new Page($count);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $show       = $Page->show();// 分页显示输出
        $list = $UserDB->where($map)->order('id ASC')->page($nowPage.','.C('PAGE_NUM'))->select();
		//显示本代理
		
		 $ulist = $UserDB->where(array('username'=>$this->_session('username')))->find();
				//dump($ulist);
                //exit;
		
        $this->assign('role',$role);
		$this->assign('ulist',$ulist);
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // 添加用户
    public function add(){
		$tj=M('User')->where(array('superior'=>$this->_session('username')))->count();
		$quantity=M('User')->where(array('username'=>$this->_session('username')))->find();
		if(($tj<$quantity['quantity']) || ($quantity['username'] == 'admin'))
		
			{
					
        $UserDB = D("User");
        if(isset($_POST['dosubmit'])) {
            $password = $_POST['password'];
            $repassword = $_POST['repassword'];
            if(empty($password) || empty($repassword)){
                $this->error('密码必须填写！');
            }
            if($password != $repassword){
                $this->error('两次输入密码不一致！');
            }
            //根据表单提交的POST数据创建数据对象
            if($UserDB->create()){
                $user_id = $UserDB->add();
                if($user_id){
                    $data['user_id'] = $user_id;
                    $data['role_id'] = $_POST['role'];
                    if (M("RoleUser")->data($data)->add()){
                        $this->success('添加成功！','index.php?g=Admin&m=User&a=index');
                    }else{
                        $this->error('用户添加成功,但角色对应关系添加失败!','index.php?g=Admin&m=User&a=index');
                    }
                }else{
                     $this->error('添加失败!');
                }
            }else{
                $this->error($UserDB->getError());
            }
        }else{
            $role = D('Role')->getAllRole(array('status'=>1),'sort DESC');
			
		
			
            $this->assign('role',$role);
            $this->assign('tpltitle','添加');
            $this->display();
        }
			}else{
				$this->error($quantity['quantity'].'个代理数量已用完!请联系站长');
			}
    }

    // 编辑用户
    public function edit(){

		$yh=M('User')->where(array('id'=>$this->_GET('id')))->find();
		$uquantity=M('User')->where(array('username'=>$this->_session('username')))->find();
		//echo M()->getLastSql();
		//exit($Think.session['username']);
		if (($yh['superior'] == $uquantity['username']) || ( 'admin' == $uquantity['username']) || ( $uquantity['username'] == $yh['username']))
			{
				
         $UserDB = D("User");
        if(isset($_POST['dosubmit'])) {
            $password = $_POST['password'];
            $repassword = $_POST['repassword'];
            if(!empty($password) || !empty($repassword)){
                if($password != $repassword){
                    $this->error('两次输入密码不一致！');
                }
                $_POST['password'] = md5($password);
            }
            if(empty($password) && empty($repassword)) unset($_POST['password']);   //不填写密码不修改
            //根据表单提交的POST数据创建数据对象
            if($UserDB->create()){
                if($UserDB->save()){
                    $where['user_id'] = $_POST['id'];
                    $data['role_id'] = $_POST['role'];
                    M("RoleUser")->where($where)->save($data);
                   
                    $this->success('编辑成功！','index.php?g=Admin&m=User&a=index');
                }else{
                     $this->error('编辑失败!','index.php?g=Admin&m=User&a=index');
                }
            }else{
                $this->error($UserDB->getError());
            }
        }else{
            $id = $this->_get('id','intval',0);
            if(!$id)$this->error('参数错误!');
            $role = D('Role')->getAllRole(array('status'=>1),'sort DESC');
            $info = $UserDB->getUser(array('id'=>$id));

            $this->assign('tpltitle','编辑');
            $this->assign('role',$role);
            $this->assign('info',$info);
            $this->display('add');
        }
		}else{
				$this->error('此代理不是您的下级！不能进行编辑');
			}
    }

    //ajax 验证用户名
    public function check_username(){
        $userid = $this->_get('userid');
        $username = $this->_get('username');
        if(D("User")->check_name($username,$userid)){
            echo 1;
        }else{
            echo 0;
        }
    }

    //删除用户
    public function del(){
		$dyh=M('User')->where(array('id'=>$this->_get('id')))->find();
		//echo M()->getLastSql();
		$duquantity=M('User')->where(array('username'=>$this->_session('username')))->find();
		//echo M()->getLastSql();
		
		
		//exit($dyh['superior']);
		//exit($Think.session['username']);
		if (($dyh['superior'] == $duquantity['username']) || ( 'admin' == $duquantity['username']))
			{
		
        $id = $this->_get('id','intval',0);
        if(!$id)$this->error('参数错误!');
        $UserDB = D('User');
        $info = $UserDB->getUser(array('id'=>$id));
        if($info['username']==C('SPECIAL_USER')){     //无视系统权限的那个用户不能删除
           $this->error('禁止删除此用户!');
        }
        if($UserDB->delUser('id='.$id)){
            if(M("RoleUser")->where('user_id='.$id)->delete()){
                $this->assign("jumpUrl",U('/Admin/User/index'));
                $this->success('删除成功！','index.php?g=Admin&m=User&a=index');
            }else{
                $this->error('用户成功,但角色对应关系删除失败!');
            }
        }else{
            $this->error('删除失败!');
        }
		}else{
				$this->error('此代理不是您的下级！不能进行删除');
			}
    }
}