<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2030>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>


<?php
    $n=0;                    //响应信息号
    $givelength=0;           //缺省赠送数量
    $discount=100;           //缺省折扣
    $loginname=trim($_REQUEST["loginname"]);
    $viptype=$_REQUEST["viptype"];
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
    if ($loginname!="") {
       $rs = mysql_query("select users.userid,users.type,types.name,types.typemode from users,types where types.typeid=users.type and loginname='".$loginname."' and checked='n'",$db);
       if ($row = mysql_fetch_array($rs)) {
          $userid=$row[0];
          $typeid=$row[1];
          $usertype=$row[2];
          $typemode=$row[3];
          mysql_free_result($rs);
          if ($typemode=="0") {     //免费用户不用添加优惠等级
             $n=3;
		  }
          else {
             $rs = mysql_query("select viptype,givelength,discount from vipuser where userid='".$userid."'",$db);
		     if ($row = mysql_fetch_array($rs)) {
                mysql_free_result($rs);
                $n=1;               //该用户优惠等级信息已经存在
			 }
             else {
                mysql_free_result($rs);
                if ($viptype!="") {
                   $rs = mysql_query("select * from vips where viptype='".$viptype."'",$db);
			       if ($row = mysql_fetch_array($rs)) {
                      $discount=strval($row["discount"]);
                      $givelength=strval($row["givelength"]);
                      mysql_free_result($rs);
                      $allmoney=0;
                      $rs = mysql_query("select sum(money2) as allmoney from addtimelist where userid='".$userid."'",$db);
				      if ($row = mysql_fetch_array($rs)) {
                         $allmoney=$row["allmoney"];
						 if (is_null($allmoney)) $allmoney=0;
                      }
                      mysql_free_result($rs);
				   }
                   else {
                      mysql_free_result($rs);
                      $n=4;            //要添加的优惠等级不存在
                   }
				}
                else {
                   $n=6;               //优惠等级名为空
                }
             }
          }
	   }
       else {
          mysql_free_result($rs);
          $n=2;                     //无效的ID
       }
	}
    else {
       $n=5;                        //用户ID为空
    }
    mysql_close($db);
?>

<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<SCRIPT language="JavaScript">

//检查输入的信息
function checkinfo()        
{
    var givelength=document.FrontPage_Form1.givelength.value;
    var discount=document.FrontPage_Form1.discount.value;
    var cansubmit1=false;
    var cansubmit2=false;
        
    if (givelength!="")
       {
        var tmpstr=parseInt(givelength,"10");
        if (tmpstr==NaN||tmpstr!=givelength)
            alert("赠送数量必须为整数");
        else
           {
            if (tmpstr>=0)
               {
                document.FrontPage_Form1.givelength.value=tmpstr;
                cansubmit1=true;
               }
            else
                alert("赠送数量应为大于等于0的整数");
            }
        }  
    else
        alert("赠送数量不能为空");
    
    if (discount!="")
       {     
        tmpstr=parseInt(discount,"10")
        if (tmpstr==NaN||tmpstr!=discount)
            alert("折扣必须为整数");
        else
           {
            if (tmpstr>0&&tmpstr<=100)
               {
                document.FrontPage_Form1.discount.value=tmpstr;
                cansubmit2=true;
               }
            else
                alert("折扣应为大于0小于等于100的整数");
           }
       }
    else
        alert("折扣不能为空");

    if (cansubmit1&&cansubmit2)
       document.FrontPage_Form1.submit();
}

</SCRIPT>
<title>添加用户优惠等级信息</title>
</head>

<body background="bkg.bmp" style="background-attachment: fixed; background-position-y:100;">

<p align="center"><font size="3"><b>添加用户优惠等级信息</b></font></p>
<div align="center">
<center>

<?php if ($n==0) {?>
<form name="FrontPage_Form1" method="POST" action="addvipuser3.php">
<table border="1" width="550" bordercolor="#008080" cellspacing="0" cellpadding="0">
  <tr>
    <td width="18%" align="center" colspan="2">用户ID　
    </td>
    <td width="82%" align="left">&nbsp&nbsp<?php echo $loginname; ?>
    <input type="hidden" name="userid" value="<?php echo $userid; ?>">　 
    </td>
  </tr>
    <tr>
    <td width="18%" align="center" colspan="2">用户类型　
    </td>
    <td width="82%" align="left">&nbsp&nbsp<?php echo $usertype; ?>
    <input type="hidden" name="usertype" value="<?php echo $usertype; ?>"> 
    </td>
  </tr>
  <tr>
    <td width="18%" align="center" colspan="2">总计消费　
    </td>
    <td width="82%" align="left">&nbsp&nbsp<?php echo $allmoney; ?>元
    <a href="queryadd2_result.php?userid=<?php echo $userid; ?>" target="_blank">详细消费清单</a>
    </td>
  </tr>
  <tr>
    <td width="9%" align="center" rowspan="3">
    优<br>惠<br>等<br>级<br>信<br>息
    </td>
    <td width="9%" align="center">名称</td>
    <td width="82%" align="left">&nbsp;&nbsp;<?php echo $viptype; ?>
    <input type="hidden" name="viptype" value="<?php echo $viptype; ?>">　
    </td>
  </tr>
  <tr>
    <td width="9%" align="center">赠送</td>
    <td width="82%">&nbsp
    <?php if ($typemode=="2") {?>
    <input type="text" name="givelength" value="0" disabled size="10">
    <?php } else {?>
    <input type="text" name="givelength" value="<?php echo $givelength; ?>" size="10">
    <?php } ?>
           <font size="2"><br>     
          ·赠送只对预付费用户有效；<br>
          ·“包年”类型赠送数量是以月为单位；<br>
          ·“计流量(G)”类型赠送数量是以M为单位；<br>
          ·其它类型赠送数量的单位和该类型续费数量单位相同； 
          </font>
    </td>
  </tr>
  <tr>
    <td width="9%" align="center">折扣</td>
    <td width="82%">&nbsp
    <input type="text" name="discount" value="<?php echo $discount; ?>" size="3" maxlength="3">%　
    </td>
  </tr>
   <tr>
    <td width="100%" colspan="3" align="center">
    <input type="button" name="b1" value="确认" onclick="checkinfo();">　
    </td>
  </tr>
</table>
</form>
<?php } elseif ($n==1) {?>
<p align="center">该用户已经拥有优惠等级，不用再次添加</p>
<?php } elseif ($n==3) {?>
<p align="center">该用户为免费用户，不用添加</p>
<?php } elseif ($n==4) {?>
<p align="center">该优惠等级不存在</p>
<?php } elseif ($n==2) {?>
<p align="center">输入的用户ID为无效</p>
<?php } elseif ($n==5) {?>
<p align="center">请输入用户ID</p>
<?php } else {?>
<p align="center">请输入用户优惠等级</p>
<?php } ?>
</center>
</div>
</body>

</html>









