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

<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="jscript">
function setresult()
{
    if (document.form1.isdel.value=="y")
       {
        parent.vip2.location.href="vipquery2.php";
        parent.vipuser.location.href="vipuser.php";
       }
}

function setresult2(obj)
{
   parent.vipuser.location.href="vipuser.php?viptype="+obj.innerText;
}
</script>
<title>优惠等级列表</title>
</head>

<body background="bkg.bmp" style="background-attachment: fixed; background-position-y:189;" onload="setresult();">
<p align="center"><font size="3"><b>优惠等级列表</b></font></p>
<center>
<?php
	$timestr=strftime("%Y-%m-%d %H:%M:%S");
    $db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
    mysql_select_db(get_cfg_var("SIPDB"),$db);
    $ispostback=$_REQUEST["ispostback"];
    $isdel="n";
    if ($ispostback=="y") {
          if ($_REQUEST["count"]!="") {
             $count=intval($_REQUEST["count"]);
             for($i=1;$i<=$count;$i++) {
                 $del=$_REQUEST["del".strval($i)];
                 if ($del=="del") {
                    $isdel="y";
                    $viptype=$_REQUEST["viptype".strval($i)];
                    $rs = mysql_query("select viptype from vips where viptype='".$viptype."'",$db);      //该优惠等级是否存在
		            if ( ($row = mysql_fetch_array($rs))==FALSE ) {
                       mysql_free_result($rs);                                             
                       echo("<font size=2>·用户优惠等级：".$viptype."不存在，不用删除<br></font>");
					}
                    else {
                       mysql_free_result($rs);   
                       mysql_query( "delete from vips where viptype='".$viptype."'",$db );
                       mysql_query( "delete from vipuser where viptype='".$viptype."'",$db );
                       mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','删除优惠等级，等级名称：".$viptype."')",$db );
                       echo("<font size=2>·用户优惠等级：".$viptype."删除成功<br></font>");
                    }
                 }
             }
          }
    }    
    $rs = mysql_query("select viptype from vips",$db);        //提取所有存在的优惠等级名称
    if ( $row = mysql_fetch_array($rs) ) {
       echo("<form name='form1' method='POST' action='vipquery1.php'>");
       echo("<table border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
       echo("<tr>");
       echo("<td align='center'>序号</td>");
       echo("<td align='center'>优惠等级名称</td>");
       echo("<td align='center'>当前用户数</td>");
       echo("<td align='center'>删除</td>");
       echo("</tr>");
       $n=0;
       while ($row) {
          $n=$n+1;
          echo("<tr><td align='center'>".strval($n)."</td>");
          echo("<td align='center'><a href='vipquery2.php?viptype=".$row["viptype"]."' target='vip2' onclick='setresult2(this);'>".$row["viptype"]."</a></td>");
          echo("<input type=hidden name=viptype".strval($n)." value=".$row["viptype"].">");
          $allcount=0;
          $rs1 = mysql_query("select count(vipuser.userid) as allcount from vipuser,users where users.userid=vipuser.userid and users.checked='n' and vipuser.viptype='".$row["viptype"]."'",$db);   //提取该优惠等级的用户数量
		  if ( $row1 = mysql_fetch_array($rs1) ) {
             $allcount=$row1["allcount"];
          }
          mysql_free_result($rs1);
          echo("<td align='center'>".strval($allcount)."</td>");
          echo("<td align=center><input type=checkbox value='del' name=del".strval($n)."></td></tr>");
          $row = mysql_fetch_array($rs);
       }
       echo("</table>");
       echo("<input type=hidden value=".$isdel." name=isdel>");
       echo("<input type=hidden value='y' name=ispostback>");
       echo("<input type=hidden name=count value=".strval($n).">");
       echo("<input type=submit value='删除' name=b2>");
       echo("</form>");
       mysql_free_result($rs);
	}
    else {
       echo("没有优惠等级信息");
       echo("<form name='form1' method='POST' action='vipquery1.php'>");
       echo("<input type=hidden value=".$isdel." name=isdel>");
       echo("</form>");
    }
    mysql_close($db);
?>
</center>
</body>
</html>