<?php

class userAction extends Action{
		private $user;
		function __construct(){
			parent::__construct();
			$this->user=new userModel();
		}

		function login(){

			$log	=$this->user->login($_POST['TxtUserName'], $_POST['TxtPassword']); //检验管理员登录
			if ($log==1){ //登录成功
				$this->showMsg("登录成功","admin.php?m=index&a=index");

			}else { //登录失败
				$this->showMsg("用户名或密码错误, 请重试","admin.php");

			}

		}


		function  add(){
			$this->display("admin/user/useradd.html");
		}

		function insert(){
			if($this->user->existsUser($_POST["userName"])){
				$this->showMsg("该用户已经存在, 请重试","admin.php?m=user&a=add");
			}else{
				$_POST["regTime"]=time();
				if($this->user->validateForm()){
					$_POST["userPwd"]=md5($_POST["userPwd"]);
					if($this->user->add($_POST)){
						$_POST=array();
						$this->showMsg("用户添加成功","admin.php?m=user&a=add");
					}else{
						$this->showMsg("用户添加失败","admin.php?m=user&a=add");
					}
				}else{
					$this->showMsg($this->user->getMessList(),"admin.php?m=user&a=add");
				}
			}

		}

		function mod(){
			$total = count( $this->user->total());
			$this->page = new page($total,10,'');
			$val = $this->user->findAll("",$this->page->getOffset(),10,"","id");
			$fpage =$this->page->fpage();
			$this->assign("users",$val);
			$this->assign("fpage",$fpage);
			$this->display("admin/user/usermod.html");

		}

		function edit(){
			$user=$this->user->find($_REQUEST['id']);
			if($user["sex"]=="1"){
				$this->assign("male", "checked");
			}elseif($user["sex"]=="-1"){
				$this->assign("female", "checked");
			}else{
				$this->assign("secret", "checked");
			}

			if($user["disable"]=="1"){
				$this->assign("disable", "checked");
			}
			if($user["allow_dlht"]=="1"){
				$this->assign("dlht", "checked");
			}

			if($user["allow_fbpl"]=="1"){
				$this->assign("fbpl", "checked");
			}
			$this->assign('post',$user);
			$this->display("admin/user/useredit.html");
		}

		function update(){
			if($this->user->validateForm(0)){
				if(empty($_POST["userPwd"])){
					unset($_POST["userPwd"]);
				}else{
					$_POST["userPwd"]=md5($_POST["userPwd"]);
				}

				if(!isset($_POST["disable"])){
					$_POST["disable"]=0;
				}
				if(!isset($_POST["allow_dlht"])){
					$_POST["allow_dlht"]=0;
				}
				if(!isset($_POST["allow_fbpl"])){
					$_POST["allow_fbpl"]=0;
				}


				if($this->user->mod($_POST)){
					$this->showMsg("修改成功","admin.php?m=user&a=mod");
				}else{
					$this->showMsg("修改失败","admin.php?m=user&a=mod");
				}
			}else{
				$this->showMsg($this->user->getMessList(),"admin.php?m=user&a=mod");
			}

		}

		function del(){
			if($_REQUEST["id"]=="data"){
				$_REQUEST["id"]=$_REQUEST["xm"];
			}
			if($this->user->del($_REQUEST["id"])){
				$this->showMsg("删除用户成功","admin.php?m=user&a=mod");
			}else{
				$this->showMsg("删除用户失败","admin.php?m=user&a=mod");
			}

		}


}
?>