<?
session_start();
require_once "../init.php";
require_once "../authority.inc.php";
$backUrl = GetUrl();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送续费邮件</title>
<LINK href="../share/style.css" rel=stylesheet>
<script src='../share/global.js'></script>
</head>
<body>
<? include "../share/top.php"?>
<div id="topLink">
<div id="topInfo">
	<span class="tTitle">发送续费邮件</span>
	<span class="sarr"> >> </span><a href="<?=$GLOBALS["parentpath"]?>">返回到管理首页</a>
</div>
</div>

<table width="100%"  border="0" align="center" id="table" class="table" cellpadding="0" cellspacing="0">
  <form action='<?=$_SERVER['PHP_SELF']?>' method='post'>
	<tr id="tr">
	<td>
		发送续费邮件
	</td>
	</tr>
    <tr>
      <td style="padding-left:150px;line-height:180%;">
	  <br>指定要发送续费通知的产品：发过期
          <input name="days" type="text" size="10" maxlength="10" value="<?=$days?>">
        天以上的产品(<span class="red1">如果要查找在X天内到期的，请输入负数</span>) <br>
		<br>
		产品类型：
        <select name='pro'>
          <option value='host'>虚拟主机</option>
          <option value='email'>企业邮局</option>
          <option value='domain'>域名注册</option>
		  <option value='nicecall'>NiceCall网络秘书</option>
          <option value='database'>数据库空间</option>
        </select><br><br>
        <input name="submit" type="submit" value="发送" class="bt2"></td>
    </tr>
  </form>
</table>
<br>
<?
$table = "`". $pro ."`";
switch($pro){
    case "host" :
	     $table_field = "`host`.`Domain`";
		 break;
    case "email" :
	     $table_field = "`email`.`Domain`";
		 break;
	case "domain" :
	     $table_field = "`domain`.`Domain`";
		 break;
	case "nicecall" :
	     $table_field = "`nicecall`.`Domain`";
		 break;
    case "database" :
	     $table_field = "`database`.`DbUser`";
		 break;
}
if($days){
	if(intval($days)){
		if($days<0)$wheres = " AND TO_DAYS($table.dtExpiry)-TO_DAYS(now())<=".abs($days)." AND TO_DAYS($table.dtExpiry)-TO_DAYS(now())>0";
		elseif($days>0)$wheres = " AND TO_DAYS(now())-TO_DAYS($table.dtExpiry)>=".abs($days);
	}else{
		$error = 1;
		echo "请指定天数，它必须是大于零或者小于零";
	}
}
if(!$error && intval($days)){
	$wheres .= " AND $table.iStatus=".ProductStatus_Actived;
	$sql="select count(*) from $table where 1 $wheres";
	$rs=$DB->query($sql);
	$pageSize=20;
	$totalRecord=$DB->result($rs,0,0);
	$totalPage=ceil($totalRecord/$pageSize);
	if(!$page)$page=1;
	$offset=($page-1)*$pageSize;
	$sql="select $table_field as About,$table.dtCreate,$table.dtExpiry,users.Contact_GB,users.Email,products.ProductName
		  from $table left join users on $table.UserName = users.UserName left join products on $table.fIDProd = products.IDProduct 
		  where 1 $wheres limit $offset,$pageSize";
	//echo $sql;
	$result=$DB->query($sql);
	echo "<center><span style='color:red'>当前正在发送第 $page 页,共 $totalPage 页</span></center><br>";
	while($pInfo=$DB->fetch_array($result)){
	     $About = $pInfo["About"] ." ". $pInfo["ProductName"];
		 $vals['Contact_GB'] = $pInfo[Contact_GB];
		 $vals['Product'] = $About;
		 $vals['ExpiryDate'] = @date("Y-m-d",@strtotime($pInfo[dtExpiry]));
		 $vals['Company'] = $AgentInfo[Company];
		 $vals['SendDate'] = date("Y-m-d",time());
		 $sendok = SendTemplateMail("refee_notice.htm",$vals,$pInfo['Email'],$About."续费通知");
		 echo "<table align='center' border='0' width='98%'><tr><td>";
		 if($sendok)echo "<font color='#009900'><font color='blue'>".$About."</font> 的续费通知已成功发送到 ".$pInfo['Email']."</font>";
		 else echo "<font color='red'><font color='blue'>".$About."</font> 的续费通知发送失败</font>";
		 echo "</td></tr></table><br>";
		 flush();
	}
	if($page<$totalPage){
		echo "<form id='postbackform' action='$_SERVER[PHP_SELF]' method='post'> \r\n";
		foreach($_REQUEST as $key => $val){
		   if($key != "page"){
			  echo "<textarea name='".$key."' style='display:none'>".$val."</textarea> \r\n";
		   }
		}
		echo "<input type='hidden' name='page' value='".($page+1)."'> \r\n";
		echo "</form>";
		echo "<center><b><span id='second' style='color:red'></span>秒后自动转到下一页</b></center>";
		echo "<script>
		       var ss=10 ;
			   function jishi(){ 
			       document.all.second.innerText = ss;
			       if(ss==0){document.all.postbackform.submit();}
				   ss --;
			   }
			   setInterval('jishi()',1000);
			   </script>";
	}
}
$DB->close();
include "../share/bottom.php";
?>
</body>
</html>
