<html>

<head>
<title>List Bill</title>
</head>

<body background="../bkg.bmp">

<p align="center"><font size="5"><b>List Bill</b></font></p>
<hr color="#999966">


<p align="center">
<?php
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$userid = trim($_REQUEST["userid"]);
	$loginname = trim($_REQUEST["loginname"]);
	$password = trim($_REQUEST["password"]);
	$bPass=FALSE;
	if ($userid!="") {
		$bPass=TRUE;
	}
	else {
		if ($loginname!="" && $password!="") {
			$bFound=0;
			$rs = mysql_query("select userid,password from users where loginname='".$loginname."' and checked='n'",$db);	//取该用户开号数据
			if ($row = mysql_fetch_array($rs)) {
				if ($password!=$row["password"]) {
					$bFound=1;				//密码错误
				}
				else {
					$userid=$row["userid"];
					$bFound=2;				//密码符合
				}
			}
			mysql_free_result($rs);
			if ($bFound==0) {
				echo("There don't exist this UserID!");
			}
			elseif ($bFound==1) {
				echo("Password is incorrect!");
			}
			else {
				$bPass=TRUE;
			}
		}
		else {
			echo("Please write UserID and password at first!");
		}
	}
	if ($bPass) {			//用户身份验证通过
			$year1 = $_REQUEST["year1"];
			$month1 = $_REQUEST["month1"];
			$month2 = $_REQUEST["month2"];
			$time1=strftime("%Y-%m-01 00:00:01");		//统计开始时间，本月开始
			$time2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",time()));		//统计结束时间，下一个月开始
    		if ($year1!="" && $month1!="" && $month2!="") {
				$time1=$year1;
				if (intval($month1)<10) {
	    			$time1=$time1."-0".$month1."-01 00:00:01";		//统计开始时间
				}
				else {
	    			$time1=$time1."-".$month1."-01 00:00:01";		//统计开始时间
				}
				$time2=$year1;
				if (intval($month2)<10) {
	    			$time2=$time2."-0".$month2."-01 00:00:01";
				}
				else {
	    			$time2=$time2."-".$month2."-01 00:00:01";
				}
				$time2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",strtotime($time2)));		//统计结束时间，下一个月开始
    		}
			$pagecount=20;		//每页显示多少条记录
			$str=$_REQUEST["pageno"];
			if ($str=="") {
				$str="1";
			}
			$page_no=intval($str);			//当前显示页号，因为可能需要多页显示
			$count=0;
			$alltime=0;
			$alllength=0;
			$alllengthstr="0";
			$allpage=0;
			$rs = mysql_query( "select count(ipaddr) as allcount,sum(usetime) as alltime,sum(datalength) as alllength from history where userid='".$userid."' and starttime>='".$time1."' and starttime<='".$time2."'",$db );	//读取该用户上网记录
			//计算总记录数，计算共有多少页。总共时间，总共流量。
			if ($row = mysql_fetch_array($rs)) {			//有记录
				$count=$row["allcount"];
				if (is_null($count)) $count=0;
				$alltime=$row["alltime"];
				if (is_null($alltime)) $alltime=0;
				$alllength=$row["alllength"];
				if (is_null($alllength)) $alllength=0;
				$alllength=round($alllength/1024,1);		//换算成M为单位
			}
			mysql_free_result($rs);
			if ($count<1) {
				echo("You have no bill briefly!");
			}
			else {
				$allpage=(int)($count/$pagecount);
				if ($allpage*$pagecount<$count) {		//不是整除
					$allpage=$allpage+1;
				}
				if ($page_no>$allpage) {
					echo("Page number is overflow!");
				}
				else {
					$rs = mysql_query( "select ipaddr,starttime,endtime,usetime,datalength,discount,facttime,factdata from history where userid='".$userid."' and starttime>='".$time1."' and starttime<='".$time2."' order by starttime",$db );
					if ($row = mysql_fetch_array($rs)) {			//有记录
						$n=1;
						while ($row && $n<=($page_no-1)*$pagecount) {		//移动行号直到当前页对应起始行号位置
							$row = mysql_fetch_array($rs);
							$n=$n+1;
						}
						if ($row) {
							echo("<table border='1' bordercolor='#808080' cellspacing='0' cellpadding='0'>");
							echo("<tr><td>No.</td><td>ip address</td><td>start time</td><td>end time</td><td>usage time(s)</td><td>usage data(k)</td><td>discount</td><td>checked usage time(s)</td><td>checked usage data(s)</td></tr>")
							while ($row && $n<=$page_no*$pagecount) {		//显示当前页记录
								echo("<tr>");
								echo("<td>".strval($n)."</td>");
								echo("<td>".$row["ipaddr"]."</td>");
								echo("<td>".$row["starttime"]."</td>");
								echo("<td>".$row["endtime"]."</td>");
								echo("<td>".strval($row["usetime"])."</td>");
								echo("<td>".strval($row["datalength"])."</td>");
								echo("<td>".strval($row["discount"])."</td>");
								echo("<td>".strval($row["facttime"])."</td>");
								echo("<td>".strval($row["factdata"])."</td>");
								echo("</tr>");
								$row = mysql_fetch_array($rs);
								$n=$n+1;
							}
							mysql_free_result($rs);
							if ($page_no==$allpage) {		//最后一页
								$tmpstr2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",time()));		//统计结束时间，下一个月开始
					    		if ($time2==$tmpstr2) {		//而且是显示当前月，显示当前正在上网的记录
									$rs = mysql_query("select logintime from login where userid='".$userid."'",$db);
									if ($row = mysql_fetch_array($rs)) {			//有记录
										$loginedtime=DateDiff("s",strtotime($row["logintime"]),time());
										$alltime=$alltime+$loginedtime;
										echo("<tr>");
										echo("<td>".strval($n)."</td>");
										echo("<td>online...</td>");
										echo("<td>".$row["logintime"]."</td>");
										echo("<td>...</td>");
										echo("<td>".strval($loginedtime)."</td>");
										echo("<td>...</td>");
										echo("</tr>");
									}
									mysql_free_result($rs);
								}
							}
							echo("</table><br>");
							echo("All count:".strval($count).",All consume:".strval($alltime)."seconds,All data:".strval($alllength)."M.");
							echo("<form method=GET action=query_result.php><input type=hidden name=userid value=".$userid."><input type=hidden name=year1 value=".$year1."><input type=hidden name=month1 value=".$month1."><input type=hidden name=month2 value=".$month2.">");
							echo("Page No. ".strval($page_no).". All ".strval($allpage)." Pages.");
							if ($page_no>1) {			//有前一页
								echo("<a href=query_result.php?userid=".$userid."&year1=".$year1."&month1=".$month1."&month2=".$month2."&pageno=".strval($page_no-1).">Before</a>");
							}
							if ($page_no<$allpage) {	//有后一页
								echo(" <a href=query_result.php?userid=".$userid."&year1=".$year1."&month1=".$month1."&month2=".$month2."&pageno=".strval($page_no+1).">Next</a>");
							}
							if ($allpage>2) {	//超过2页
								echo(" Go<input type=text size=3 name=pageno maxlength=3>");
							}
							echo("</form>");
						}
						else {
							mysql_free_result($rs);
							echo("Page number is overflow!");
						}
					}
					else {
						mysql_free_result($rs);
						echo("You have no bill briefly!");
					}
				}
			}
	}
	mysql_close($db);
?>

</p>

<p align="center">List time: from <?php echo $time1; ?> to <?php echo $time2; ?>.</p>

<hr>
<p align="center">SIPMonitor System</p>

</body>

</html>
