<?php
define('IN_PHPQUAN', true);
require_once dirname(__FILE__) . '/include/common.php';
if($CFG['uc'])require PHPQUAN_ROOT . 'include/uc.inc.php';
chkadmin('member');
$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list' ;
switch ($_REQUEST['act'])
{
	case 'list':
		$gid = !empty($_REQUEST['gid']) ? intval($_REQUEST['gid']) : '0';
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
		if($gid>0){
			$sqls = " and gid=$gid";
		}else{
			$sqls = " and gid=''";
		}
		$sql = "SELECT COUNT(*) FROM {$table}member where 1 $sqls";
		$count = $db->getOne($sql);
		$pager = get_pager('member.php',array('act'=>'list','gid'=>$gid),$count,$page,'20');
		
		$sql = "SELECT * FROM {$table}member where 1 $sqls ORDER BY lastlogintime DESC LIMIT $pager[start],$pager[size]"; 
		$res = $db->query($sql);
		$userinfo = array();
		while($row=$db->fetchRow($res)) {
			$row['username'] = cut_str($row['username'],20);
			//$row['fids'] = $row['fid']==1 ? '<span class=f94>批量注册</span>' : '正常注册';
			$row['registertime']  = date('Y-m-d H:i:s',$row['registertime']);
			$row['lastlogintime'] = date('Y-m-d H:i:s',$row['lastlogintime']);
			$userinfo[] = $row;
		}
	    include tpl('list_member');
	break;
	case 'edit':
	    $userid = intval($_REQUEST['id']);
		$sql = "SELECT * FROM {$table}member WHERE userid = '$userid'";
		$userinfo = $db->getRow($sql);
		$sex = $userinfo['sex']=='1' ? '男' : '女';
		$face = $userinfo['face'] ? '<img src="'.$userinfo['face'].'" width="50px">' : '无';
		$shengris = $userinfo['shengri']>0 ? date('Y-m-d',$userinfo['shengri']) : '未填写';
		include tpl('edit_member');
	break;
	case 'update':
		$userid = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		$password = $_REQUEST['password'] ? trim($_REQUEST['password']) : '';
		$repassword = $_REQUEST['repassword'] ? trim($_REQUEST['repassword']) : '';
		$email = $_REQUEST['email'] ? trim($_REQUEST['email']) : '';
		$status = $_REQUEST['status'] ? trim($_REQUEST['status']) : '0';
		$gid = $_REQUEST['gid'] ? intval($_REQUEST['gid']) : '';
		if($password != $repassword)show('两次输入密码不相同');
		if($password && (strlen($password) < 5 || strlen($password) > 30))show("密码必须在5个至30个字符之间");
		
		/* 验证邮件是否存在 */
		if($email){
			$sql = "select count(*) from {$table}member where email='$email' and userid<>'$userid' ";
			if($db->getOne($sql)>0)show('您所填写的邮件已经存在');
		}
		
		if($password)$set[] = " password = '".MD5($password)."' ";
		$set[] = " email = '$email' ";
		$set[] = " gid = '$gid' ";
		$set[] = " status = '$status' ";
		if(!empty($set)) $set = join(',', $set);
		$res = $db->query("UPDATE {$table}member SET $set WHERE userid = '$userid'");
		
		if($CFG['uc']) {
			uc_call("uc_user_edit", array($username, '', $password, $email, '1'));
		}
		admin_log("修改会员 $userid 信息成功");
		$link = "member.php?act=list";
		show("修改会员信息成功", $link);
	break;
	case 'batch':
		$userid = is_array($_REQUEST['id']) ? join(',',$_REQUEST['id']) : intval($_REQUEST['id']);
		if(empty($userid))show('没有选择记录');
			$sql = "SELECT id FROM {$table}juan WHERE userid in ($userid) ";
			$juans = $db->getAll($sql);
			foreach($juans as $juan)
			{
				//删除券属性
				$sql = "DELETE FROM {$table}cus_value WHERE juanid = '$juan[id]'";
				$db->query($sql);
			}
		//删除所有发布、领取券
		$db->query("DELETE from {$table}juan WHERE userid in ($userid)");
		$db->query("DELETE from {$table}juan_user WHERE userid in ($userid)");
		/* 删除会员的消息、登录绑定、数据库信息 */
	    $db->query("DELETE FROM {$table}member_msg WHERE senduser in ($userid) ");
		$db->query("DELETE FROM {$table}member WHERE userid in ($userid) ");
		admin_log("删除会员 $userid 成功");
		$link = 'member.php?act=list';
		show("删除会员 $userid 成功", $link);
	break;
}
?>