<?
include("connect.php");
$PRODUCTSPERPAGE=20;
include("security.php");
include('../sendmail.php');
$type1 = "1";
$type2 = "2";
include("pagepermission.php");

if($_GET['sendemail']!="")
{
		$sqlsendemail = "select * from registration where user_delete_flag!='d' and id='".$_GET['sendemail']."'";
		$ressendemail = mysql_query($sqlsendemail) or die(mysql_error());
		$rowsendemail = mysql_fetch_array($ressendemail);
		$email = $rowsendemail['email'];
		$content='';
		$content.= "<font style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>亲爱的会员,"."</font><br>"."<br>"."<p align='center' style='font-size: 14px;font-family: Arial, Helvetica, sans-serif;font-weight:bold;'>个人账户信息</p>"."<br>".	
	
	"<table border='0' cellpadding='3' cellspacing='0' width='100%' align='center' class='style13'>";

		$content.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>您好 ".$rowsendemail['firstname'].", </td>
		</tr>";

		$content.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>以下是您的会员注册信息:</td>
		</tr>";
		
		$content.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>用户名: ".$rowsendemail['username']."</td>
		</tr>";

		$content.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>会员ID: ".$rowsendemail['id']."</td>
		</tr>";

		$content.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>激活您的会员帐号 <a href='".$SITE_URL."password.php?auc_key=".$rowsendemail['verify_code']."'>点这里</a></td>
		</tr>
		</table>";

		$subject="激活您的会员帐号";
		$from=$adminemailadd;
		//$ADMIN_EMAIL = "scah@comcast.net";
		//$ADMIN_EMAIL = "testmailfunction@gmail.com";
		SendHTMLMail($email,$subject,$content,$from);
		header("location:message.php?msg=59");
		exit;
}

if($_GET["verifyit"]!="")
{
	$userverify = $_GET["verifyit"];
	$qryupd = "update registration set account_status='1' where id='".$userverify."'";
	mysql_query($qryupd) or die(mysql_error());
	header("location: message.php?msg=80");
	exit;
}

if($_POST['ChangeStatus'])
{
	if(is_array($_POST['enable_disable']))
	{
		for($j=0;$j<count($_POST['enable_disable']);$j++)
		{
			$cvariable = explode("|",$_POST['enable_disable'][$j]);
			$m_id = $cvariable[0];
			$c_status = $cvariable[1];
			if(trim($c_status)=="0" or trim($c_status)=="")
			{
				$chag_status = "1";
			}
			elseif(trim($c_status)=="1")
			{
				$chag_status = "0";
			}
			$udpatestatus = "update registration set member_status='".$chag_status."' where id='".$m_id."'";
			mysql_query($udpatestatus) or die(mysql_error());
		}
	}
}

//EnableDelivery
if($_POST['EnableDelivery'])
{
	if(is_array($_POST['enable_disable']))
	{
		for($j=0;$j<count($_POST['enable_disable']);$j++)
		{
			$cvariable3 = explode("|",$_POST['enable_disable'][$j]);
			$m_id3 = $cvariable3[0];
			$c_status3 = $cvariable3[3];
			if(trim($c_status3)=="0" or trim($c_status3)=="")
			{
				$chag_status3 = "1";
			}
			elseif(trim($c_status3)=="1")
			{
				$chag_status3 = "0";
			}
			$udpatestatus3 = "update registration set sent_gift='".$chag_status3."' where id='".$m_id3."'";
			mysql_query($udpatestatus3) or die(mysql_error());
		}
	}
}

//EnableGift
if($_POST['EnableGift'])
{
	if(is_array($_POST['enable_disable']))
	{
		for($j=0;$j<count($_POST['enable_disable']);$j++)
		{
			$cvariable2 = explode("|",$_POST['enable_disable'][$j]);
			$m_id2 = $cvariable2[0];
			$c_status2 = $cvariable2[2];
			if(trim($c_status2)=="0" or trim($c_status2)=="")
			{
				$chag_status2 = "1";
			}
			elseif(trim($c_status2)=="1")
			{
				$chag_status2 = "0";
			}
			$udpatestatus2 = "update registration set can_send_gift='".$chag_status2."' where id='".$m_id2."'";
			mysql_query($udpatestatus2) or die(mysql_error());
		}
	}
}

// calculation for order
		if($_REQUEST['order'])
		{
		$order=$_REQUEST['order'];
		}
//calculation for page no
		if(!$_GET['pgno'])
		{
			$PageNo = 1;
		}
		else
		{
			$PageNo = $_GET['pgno'];
		}
if($order)
{
if($_REQUEST['memstatus']!="")
{
	if($_REQUEST['memstatus']=="0" || $_REQUEST['memstatus']=="1" || $_REQUEST['memstatus']=="2")
	{
		$addquery = " and account_status='".$_REQUEST['memstatus']."' ";
	}
	elseif($_REQUEST['memstatus']=="d")
	{
		$addquery = " and member_status='1' ";
	}
$memstatus = $_REQUEST['memstatus'];	
}
if($_REQUEST["stext"]!="")
{
	$searchdata = $_REQUEST["stext"];
	$addquery2 = "and (username like '%".$searchdata."%' or firstname like '%".$searchdata."%' or lastname like '%".$searchdata."%')";
}
$sql="select * from registration where admin_user_flag='0' and username like '$order%' and user_delete_flag!='d'  ".$addquery.$addquery2." order by id";
}
else
{
if($_REQUEST['memstatus']!="")
{
	if($_REQUEST['memstatus']=="0" || $_REQUEST['memstatus']=="1" || $_REQUEST['memstatus']=="2")
	{
		$addquery = " and account_status='".$_REQUEST['memstatus']."' ";
	}
	elseif($_REQUEST['memstatus']=="d")
	{
		$addquery = " and member_status='1' ";
	}
$memstatus = $_REQUEST['memstatus'];	
}
if($_REQUEST["stext"]!="")
{
	$searchdata = $_REQUEST["stext"];
	$addquery2 = "and (username like '%".$searchdata."%' or firstname like '%".$searchdata."%' or lastname like '%".$searchdata."%')";
}
$sql="select * from registration where admin_user_flag='0' and user_delete_flag!='d' ".$addquery.$addquery2." order by id";
}
$PRODUCTSPERPAGE=20;
$result=mysql_query($sql);
$total=mysql_num_rows($result);
$totalpage=ceil($total/$PRODUCTSPERPAGE);
//echo $totalpage;
if($totalpage>=1)
{
$startrow=$PRODUCTSPERPAGE*($PageNo-1);
$sql.=" LIMIT $startrow,$PRODUCTSPERPAGE";
//echo $sql;
$result=mysql_query($sql);
$total=mysql_num_rows($result);
}

//find all users without deleted users
$qrycount = "select * from registration where admin_user_flag='0' and user_delete_flag!='d'";
$rescount = mysql_query($qrycount);
$totalcount = mysql_num_rows($rescount);

//find not varified users
$qrycount1 = "select * from registration where admin_user_flag='0' and user_delete_flag!='d' and account_status='0' and member_status='0'";
$rescount1 = mysql_query($qrycount1);
$totalcount1 = mysql_num_rows($rescount1);

//find active users
$qrycount2 = "select * from registration where admin_user_flag='0' and user_delete_flag!='d' and account_status='1' and member_status='0'";
$rescount2 = mysql_query($qrycount2);
$totalcount2 = mysql_num_rows($rescount2);

//find unsubscribed users

$qrycount3 = "select * from registration where admin_user_flag='0' and user_delete_flag!='d' and account_status='2'";
$rescount3 = mysql_query($qrycount3);
$totalcount3 = mysql_num_rows($rescount3);

//find suspended users
$qrycount4 = "select * from registration where admin_user_flag='0' and user_delete_flag!='d' and member_status='1'";
$rescount4 = mysql_query($qrycount4);
$totalcount4 = mysql_num_rows($rescount4);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$lng_characset;?>" />
<title><? echo $ADMIN_MAIN_SITE_NAME." - Manage Members"; ?></title>
<script language="JavaScript">
	
	function delconfirm(loc)
	{
		if(confirm("确定要删除该会员?"))
		{
			window.location.href=loc;
		}
		return false;
	}
function Submitform()
{
	document.form1.submit();
}	
function EnterData()
{
	if(document.searchuser.stext.value=="")
	{
		alert("Please enter search text");
		document.searchuser.stext.focus();
		return false;
	}
}	
	
</script>
<link href="main.css" type="text/css" rel="stylesheet">
</head>

<body bgcolor="#ffffff" style="padding-left:10px">
<TABLE cellSpacing=10 cellPadding=0  border=0 width="100%">
		<TR>
			<TD>
				<table border="0" width="770">
				<tr>
					<td class='H1' width="600">用户管理</td>
					<td>
						<table border="0">
							<tr>
								<Td>已激活会员 :</Td>
								<td align="right"><?=$totalcount2;?></td>
							</tr>
							<tr>
								<Td>已注销会员 :</Td>
								<td align="right"><?=$totalcount3;?></td>
							</tr>
							<tr>
								<td>未验证会员 :</td>
								<td align="right"><?=$totalcount1;?></td>
							</tr>
							<tr>
								<td>被冻结会员 :</td>
								<td align="right"><?=$totalcount4;?></td>
							</tr>
							<tr>
								<Td>总计 :</Td>
								<td align="right"><?=$totalcount;?></td>
							</tr>
						</table>
					</td>
				</tr>
				</table>
			</TD>
		</TR>
		<TR>
			<TD background="images/vdots.gif"><IMG height=1 
			  src="images/spacer.gif" width=1 border=0></TD>
		</TR>
		<tr>
			<td>
				<table border="0" width=" 770">
				<tr>
					<form name="searchuser" action="" method="post" onsubmit="return EnterData();">
					<Td width="515">
					&nbsp;&nbsp; <IMG height=11 src="images/001.gif" width=8 border=0> 
                <A class=la href="addmembers.php">新增一个会员</A> </td>
					<td>查找 : <input type="text" name="stext" value="<?=$searchdata;?>" />&nbsp;&nbsp;<input type="submit" name="searchsubmit" value="查找" class="bttn" /><input type="hidden" name="memstatus" value="<?=$memstatus;?>" /></td>
					</form>
			    </tr>
				</table>
			</td>
		</tr>
<Tr>
<td>
<FORM id="form1" name="form1" action="manage_members.php" method="post">
      <TABLE cellSpacing=0 cellPadding=1 border=0  width="780">
        <TBODY>
        <TR>
		  <td width="56%">
		  </td>
		  <td width="15%">&nbsp;</td>
		<td width="40%" align="right">
			<table border="0" cellpadding="0"  cellspacing="0" width="100%">
				<tr>
					<td align="left"><b>会员状态 :</b>
					<select name="memstatus" onchange="Submitform();">
					<option value="" <? if($memstatus==""){?> selected="selected"<? } ?>>全部</option>
					<option value="2" <? if($memstatus=="2"){?> selected="selected"<? } ?>>已注销</option>
					<option value="0" <? if($memstatus=="0"){?>selected="selected"<? } ?>>未验证</option>
					<option value="1" <? if($memstatus=="1"){?>selected="selected"<? } ?>>激活</option>
					<option value="d" <? if($memstatus=="d"){?>selected="selected"<? } ?>>冻结</option>
					</select>
					</td>
				</tr>
			</table>
		  </td>		
		  </TR></TBODY></TABLE>
		<input type="hidden" name="stext" value="<?=$searchdata;?>" />		  
			</form>
			 <? if(!$total){?>
		<table width="95%" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#000000">
        <tr> 
          <td > 
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td class=th-a > 
                  <div align="center">没有会员信息</div>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
      <?php }else{?>
<form id="form2" name="form2" action="" method="post">	  
		<TABLE width="95%" border="1" cellSpacing="0" class="t-a" align="center">
        <TR class="th-a"> 
			  <TD nowrap="nowrap"></TD>
			  <TD nowrap="nowrap">登录名称</TD>
				<TD width="10%" nowrap="nowrap">姓名</TD>
        <TD width="30%" nowrap="nowrap">收货地址</TD>
				<td nowrap="nowrap">允许赠送</td>
				<td nowrap="nowrap">是否已寄</td>
				<td nowrap="nowrap">邮件地址</td>
				<TD nowrap="nowrap">状态</TD>
				<!--TD nowrap="nowrap"></TD-->
				<TD nowrap="nowrap">选项</TD>
              </TR>
              <?php
			  $colorflg=0;
			  for($i=0;$i<$total;$i++)
			  {
				$row = mysql_fetch_object($result);
				$id=$row->id;
				$fname = stripslashes($row->firstname);
				$lname = stripslashes($row->lastname);
				$city=$row->city;
				$member_status = $row->member_status;
				$account_status = $row->account_status;				
				$country=$row->country;
					$qrycou = "select * from countries";
					$rescou = mysql_query($qrycou);
					while($cou=mysql_fetch_array($rescou))
					{
						if($country==$cou["countryId"])
						{
							$country = $cou["printable_name"];
						}					
					}
					if($row->sponser!='0')
					{
						$qryreg = "select * from registration where id='".$row->sponser."'";
						$resreg = mysql_query($qryreg);
						$objreg = mysql_fetch_object($resreg);
						$sponsername = $objreg->username;
					}
				$email=stripslashes($row->email);
				$username=stripslashes($row->username);
				$addressline1 = stripslashes($row->addressline1);
				$can_send_gift = stripslashes($row->can_send_gift);
				$sent_gift = stripslashes($row->sent_gift);
				if($can_send_gift=="1")$can_send_gift="<font color='green'>允许</font>";
				if($can_send_gift=="0")$can_send_gift="<font color='red'>不允许</font>";
				if($sent_gift=="1")$sent_gift="<font color='green'>已寄</font>";
				if($sent_gift=="0")$sent_gift="<font color='red'>未寄</font>";
				
				if ($colorflg==1){
					$colorflg=0;
					$colorid = "#F2F6FD";
					echo "<TR bgcolor=\"#F2F6FD\"> ";
				}else{
					$colorflg=1;
					$colorid = "#FFFFFF";
				  	echo "<TR> ";
				}?>
				<TD align="left" valign="middle"><input type="checkbox" name="enable_disable[]" value="<?=$id."|".$member_status."|".$row->can_send_gift."|".$row->sent_gift;?>" style="border:none; background-color:<?=$colorid?>" /></TD>
        <TD align="left" valign="middle"><?=$username."&nbsp;"; ?></TD>
				<TD align="left" valign="middle"><?=$fname."&nbsp;".$lname; ?></TD>
				<TD align="left" valign="middle"><?=$addressline1."&nbsp;"; ?></TD>
				<TD align="left" valign="middle"><?=$can_send_gift."&nbsp;"; ?></TD>
				<TD align="left" valign="middle"><?=$sent_gift."&nbsp;"; ?></TD>
				<TD align="left" valign="middle"><?=$email."&nbsp;"; ?></TD>
				<TD align="left" valign="middle"><? if($member_status==0 && $account_status==1){ echo "<font color='green'>可用</font>";} elseif($member_status==0 && $account_status==2){ echo "<font color='red'>Closed</font>"; }elseif($member_status==1){ echo "<font color='red'>不可用</font>";} elseif($member_status==0 && $account_status==0){ echo "<font color='green'>可用</font>"; } ?></TD>
				<!--TD align="center" valign="middle"><?=$sponsername!=""?$sponsername:"--"; ?></TD-->
                <TD noWrap width="32%" >
			<INPUT class="bttn" onClick="window.location.href='edit_member.php?editid=<?=$id;?>'" type="button" value="编辑"> 
            <input name="button" type="button" class="bttn" value="删除" onClick="return delconfirm('edit_member.php?delid=<?=$id;?>')">
			<input name="button" type="button" class="bttn" value="详情" onClick="window.location.href='view_member_statistics.php?uid=<?=$id;?>'">			
			<? if($account_status=='0'){?>
			<br /><br />
			<input type="button" name="re_sent" class="bttn" value="发送验证邮件" onclick="window.location.href='manage_members.php?sendemail=<?=$id;?>'" />&nbsp;<input type="button" name="verify_it" class="bttn" value="验证" onclick="window.location.href='manage_members.php?verifyit=<?=$id;?>'" />
			<? } ?>
                </TD>
              </TR>
              <?
			  	$sponsername = "";
			  }
			  ?>
              
          </TABLE><br />
		  <input type="submit" class="bttn" name="ChangeStatus" value="可用/不可用">
		  <input type="submit" class="bttn" name="EnableGift" value="允许赠送/不允许赠送">
		  <input type="submit" class="bttn" name="EnableDelivery" value="已寄/未寄">
		  <br />
		  </form>
		  <? } ?><br />
		  <!-- paging starts -->
	   <?php
		if($PageNo>1)
		{
                  $PrevPageNo = $PageNo-1;

	    ?>
	  <A class=paging href="manage_members.php?pgno=<?=$PrevPageNo; ?>&order=<?=$order?>&memstatus=<?=$memstatus?>&stext=<?=$searchdata;?>">&lt; 上一页</A>
	  <?
	   }
	  ?> &nbsp;&nbsp;&nbsp;
	  <?php
        if($PageNo<$totalpage)
        {
         $NextPageNo = 	$PageNo + 1;
      ?>
	  <A class=paging id=next href="manage_members.php?pgno=<?=$NextPageNo;?>&order=<?=$order?>&memstatus=<?=$memstatus?>&stext=<?=$searchdata;?>">下一页 &gt;</A>
	  <?
       }
      ?>
		  <!-- paging ends -->
</TD></TR></TBODY></TABLE></body>
</html>
