<?php
if(!defined('CORE'))exit("error!"); 


//角色页面权限
function checkbox_area_action($areaid,$row_action){
	$action=explode(',',$row_action);
	$i=1;
	foreach($areaid as $key=>$val){
		if($key!="0"){
		if(in_array($key,$action)){
		$cbox .="<span style=\"float:left;width:150px\"><input name=\"areaid[]\" type=\"checkbox\" value=\"$key\" checked=\"checked\" />$val</span>\n";
		}else{
		$cbox .= "<span style=\"float:left;width:150px\"><input name=\"areaid[]\" type=\"checkbox\" value=\"$key\" />$val</span>\n";
		}
		}
		if($i==8){$cbox .="";}
		$i++;
	}
	return $cbox;
}


//验证登录
if($do=="loginok"){
	$name=$_POST[username];
	$pwd=md5($_POST[password]);
	
	$validate_arr=array($name,$pwd);
	Ifvalidate($validate_arr);
	
	$sql = "SELECT id,username,roleid from users WHERE username = '$name' AND password = '$pwd' limit 1 ";
	//echo $sql;
	$db->query($sql);
	if ($record = $db->fetchRow()){	//登录成功
		$_SESSION['isLogin'] 	= true;
		$_SESSION['userid']		= $record['id'];
		$_SESSION['username']	= $record['username'];
		$_SESSION['roleid']	    = $record['roleid'];
		if($_SESSION['roleid']==1){
		     echo success($msg,"?action=admin");
		}else{
		     echo no_qx($msg,"?action=index");
			  
		     }
	}
	else
		exit($lang_cn['rabc_login_error']);
	exit;
}


//登录	
if($do=="login"){
	$smt = new smarty();smarty_cfg($smt);
	$smt->assign('web_name',$web_name);
	$smt->assign('title',"登录");
	$smt->display('admin_login.htm');
	exit;
}


//退出	
if($do=="logout"){
	$_SESSION = array();
	session_destroy();
	exit($lang_cn['rabc_logout']);
}

//列表	
if($do==""){
	If_rabc(); //检测权限
	if($_POST['kewords']){$search .= " and username like '%".strip_tags($_POST[kewords])."%'";}			
	if($_POST['time_start']!="" && $_POST['time_over']!=""){
		$search .= " and `created_at` >  '".strtotime($_POST[time_start]." 00:00:00")."' AND  `created_at` <  '".strtotime($_POST[time_over] ." 23:59:59")."'";
	}
	
	//设置分页
	if($_POST[numPerPage]==""){$numPerPage="10";}else{$numPerPage=$_POST[numPerPage];}
	if($_GET[pageNum]==""||$_GET[pageNum]=="0" ){$pageNum="0";}else{$pageNum=($_GET[pageNum]-1)*$numPerPage;}
	$num=mysql_query("SELECT * FROM users where 1=1 $search");//当前频道条数
	$total=mysql_num_rows($num);//总条数	
	$page=new page(array('total'=>$total,'perpage'=>$numPerPage));

	//查询
	$sql="SELECT * FROM users  where 1=1 $search order by id desc LIMIT $pageNum,$numPerPage";
	
	//echo $sql;
	$db->query($sql);
	$list=$db->fetchAll();	
	
 	
	//模版
	$smt = new smarty();smarty_cfg($smt);
	$smt->assign('list',$list);
	$smt->assign('numPerPage',$_POST[numPerPage]); //显示条数
	$smt->assign('pageNum',$_GET[pageNum]); //当前页数
	$smt->assign('total',$total);  //总条数
	$smt->assign ('page',$page->show());
	$smt->assign('title',"用户管理");
	$smt->display('user_list.htm');
	exit;
	
}


//新建	
if($do=="new"){	
	If_rabc(); //检测权限
	
	//角色数组
	$sql="SELECT id FROM `users`";
	$db->query($sql);
	$list=$db->fetchAll();
	
	//格式化角色数据
	foreach($list as $key=>$val){
		$role_cn[$list[$key][id]]=$list[$key][title];		
	}
	$smt = new smarty();smarty_cfg($smt);
	$smt->assign('select_role_cn',select($role_cn,"roleid","","选择角色","required"));
	$smt->assign('checkbox_area_action',checkbox_area_action($areaid));
	$smt->assign('title',"新建用户");
	$smt->display('user_new.htm');
	exit;
}


//写入
if($do=="add"){
	If_rabc(); //检测权限
	$created_at=date("Y-m-d H:i:s", time());
	$password=md5($_POST[password]);
	$areaid=implode(',',$_POST[areaid]);
	
	//查询
	$sql="SELECT * FROM `users` where username ='$_POST[username]' LIMIT 1";
	$db->query($sql);
	if($db->fetchRow()){echo  "{\"statusCode\":\"300\",\"message\":\"错误!用户已存在!\",\"navTabId\":\"\",\"callbackType\":\"forward\"}";exit();}
	$created_at = time();
	$sql="INSERT INTO `users` (`username` ,`password` ,`roleid`,`created_at` )
	VALUES ( '$_POST[username]', '$password', '$_POST[roleid]','$created_at');";
	if($db->query($sql)){echo success($msg,"?action=user");}else{echo error($msg);}
	exit;
}


//编辑	
if($do=="edit"){	
	If_rabc(); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//查询
	$sql=" SELECT * FROM users where id='{$id}' LIMIT 1";
	$db->query($sql);
	$row=$db->fetchRow();
	
	//模版
	$smt->assign('row',$row);
	$smt->assign('title',"编辑用户");
	$smt->display('user_edit.htm');
	exit;
}


//更新
if($do=="updata"){
	If_rabc(); //检测权限
    $updated_at= time();
	$id=$_POST['id'];
	//$areaid=implode(',',$_POST[areaid]);
	$password=md5($_POST[password]);
	$sql="UPDATE users SET 
	`username` = '$_POST[username]',
	`roleid` = '$_POST[roleid]',
	`password` = '$password',
	`updated_at` = '$updated_at' WHERE `id` ='$_POST[id]' LIMIT 1 ;";
	
	if($db->query($sql)){echo success($msg,"?action=user");}else{echo error($msg);}	
	exit;
}


//删除
if($do=="del"){
	If_rabc(); //检测权限
	$sql="delete from `users` where `id`=$id limit 1";
	if($db->query($sql)){echo success($msg,"?action=user");}else{echo error($msg);}	
	exit;
}


//修改密码	
if($do=="editpass"){	
	If_rabc(); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//查询
	$sql="SELECT * FROM `users` where id='$id' LIMIT 1";
	$db->query($sql);
	$row=$db->fetchRow();
	//模版
	$smt->assign('row',$row);
	$smt->assign('title',"修改密码");
	$smt->display('user_edit_pass.htm');
	exit;
}


//更新密码
if($do=="updatapass"){
	If_rabc(); //检测权限
	$updated_at=time();
	$id=$_SESSION['userid'];
	if($_POST[password]){
		$password=md5($_POST[password]);
	}
	$sql="UPDATE `users` SET `password`='{$password}',`updated_at` = '$updated_at' WHERE `users`.`id` ={$id} LIMIT 1 ;";	
	if($db->query($sql)){echo success($msg,"?action=user&do=login");}else{echo error($msg);}
	exit;
}
?>