<?php
define('IN_PHPMPS', true);
require_once dirname(__FILE__) . '/include/common.php';
if($CFG['uc'])require PHPMPS_ROOT . 'include/uc.inc.php';
chkadmin('member');
$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list' ;

switch ($_REQUEST['act'])
{
	case 'list':
		$here = "会员列表";
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}member";
		$count = $db->getOne($sql);
		$pager = get_pager('member.php',array('act'=>'list'),$count,$page,'20');
		
		$sql = "SELECT * FROM {$table}member 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['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);
		$userinfo['allmoney'] = $db->getOne("SELECT sum(yjia) FROM {$table}pay_exchange where userid='$userid'");
		$userinfo['allmoney'] = number_format($userinfo['allmoney'],2);
		$here = "编辑会员信息";
		include tpl('edit_member');
	break;

	case 'update':
		$userid = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		$username = $_REQUEST['username'] ? trim($_REQUEST['username']) : '';
		$phone = $_REQUEST['phone'] ? trim($_REQUEST['phone']) : '';
		$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';

		if($password != $repassword)show('两次输入密码不相同');
		if($password && (strlen($password) < 5 || strlen($password) > 30))show("密码必须在5个至30个字符之间");
		//if(empty($email))show('邮箱不能为空');
		if(!preg_match("/^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+$/",$email))show('邮箱格式错误');
		
		/* 验证邮件是否存在 */
		$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[] = " phone = '$phone' ";
		$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("修改会员 $username 信息成功");
		$link = "member.php?act=list";
		show("修改会员 $username 信息成功", $link);
	break;

	case 'batch':
		$userid = is_array($_REQUEST['id']) ? join(',',$_REQUEST['id']) : intval($_REQUEST['id']);
		if(empty($userid))show('没有选择记录');
		/*
		 * 删除用户的其他评论。
		 * 系统设置删除会员时删除此会员发表的评论的话，则删除评论。
		 */
		if($CFG['del_m_comment']) $db->query("delete from {$table}com_comment where userid in ($userid) ");
		if($CFG['del_m_credit']) $db->query("delete from {$table}pay_credit where userid in ($userid) ");
		if($CFG['del_m_tx']) $db->query("delete from {$table}pay_tx where userid in ($userid) ");
		if($CFG['del_m_juan']) $db->query("delete from {$table}juan_pay where userid in ($userid) ");
		
		/* 删除会员的数据库信息 */
	    $db->query("DELETE FROM {$table}member WHERE userid in ($userid) ");

		admin_log("删除会员 $userid 成功");
		$link = 'member.php?act=list';
		show("删除会员 $userid 成功", $link);
	break;
	
	case 'member_tx':
	    $uid = $_REQUEST['uid'] ? intval($_REQUEST['uid']) : '';
		if(empty($uid)){
			$sqls = "";
			$sqls1 = "";
		}else{
			$sqls = " and userid=$uid";
			$sqls1 = " and a.userid=$uid";
		}
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}pay_tx where tid=2 $sqls";
		$count = $db->getOne($sql);
		$pager = get_pager('member.php',array('act'=>'member_tx'),$count,$page,'20');
		
		$sql = "SELECT a.*,c.username FROM {$table}pay_tx as a left join {$table}member as c on a.userid=c.userid where tid=2 $sqls1 ORDER BY txtime DESC LIMIT $pager[start],$pager[size]"; 
		$res = $db->query($sql);
		$usertx = array();
		while($row=$db->fetchRow($res)) {
			$row['siti'] = $row['money']-($row['money']*$row['soxu']/100);
			$row['txtime'] = date('Y-m-d H:i',$row['txtime']);
			$usertx[] = $row;
		}
	    $here = "会员提现记录";
	    include tpl('member_tx');
	break;
	
	case 'member_tx_no';
		$id = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		if(empty($id))show("参数错误");
        $r = $db->getRow("SELECT money,userid FROM {$table}pay_tx WHERE id='$id'");
        $db->query("update {$table}member set money=money+".$r['money']." where userid=".$r['userid']);
        $db->query("update {$table}pay_tx set zuangtai=2 where id='$id'");
        show("提现退回成功，金额已返回到用户帐户！");
    break;
	
	case 'member_tx_yes';
	    $id = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		if(empty($id))show("参数错误");
		$db->query("update {$table}pay_tx set zuangtai=1 where id='$id'");
		show("提现成功！");
	break;
}
?>