<?php
include "../../include/conn.php";
include "../../include/function.php";
include "../admin_loginstate.php";
include "../admin_version.php";
//'**************************************************************************************************
//' 软件名称: 搜一次 Content Management System
//' 版本编号: V4.0.0版
//' 官方网站: http://www.syccms.com
//' 官方论坛：http://bbs.syccms.com
//' 版权所有: 搜一次运营团队
//' 郑重声明:
//'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
//'    2、搜一次网络有限公司保留此软件的法律追究权利
//'**************************************************************************************************
?>
<!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=gb2312" />
<title>冲值卡生成</title>
<link  href="../images/style.css" rel="stylesheet" type="text/css" />
<link href="../images/alerts.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">var tentpath='<?php echo S_SitePath?>';</script>
<script src="../js/jquery.js"></script>
<script src="../js/alerts.js"></script>
<script language="javascript">
$(document).ready(function(){
	$("#ChkAll").click(function(){
			if($(this).attr("checked")==true){
				$("input[name='S_ID[]']").each(function(){
					$(this).attr("checked",true);									 
				})							
			}else{
				$("input[name='S_ID[]']").each(function(){
					$(this).attr("checked",false);									 
				})							
			}
	});
	$("#search1").click(function(){
		var SearchStr=$("#SearchStr").val();	
		if(SearchStr==""){
			$("#SearchStr").css("border","1px solid red");
			return false;
			};
	});
	$("#submit2").click(function(){
		var S_CardNum=$("#S_CardNum").val();
		var S_CardPWD=$("#S_CardPWD").val();
		var S_CardWorth=$("#S_CardWorth").val();
		$("#S_CardNum").css("border-color","#666 #ccc #ccc #666");
		$("#S_CardPWD").css("border-color","#666 #ccc #ccc #666");
		$("#S_CardWorth").css("border-color","#666 #ccc #ccc #666");
		if(S_CardNum.length!=16){
			$("#S_CardNum").css("border","1px solid red");return false;
			};
		else if(S_CardPWD.length!=8){
			$("#S_CardPWD").css("border","1px solid red");return false;
			};
		else if(S_CardWorth==""){
			$("#S_CardWorth").css("border","1px solid red");return false;
			};
	});
	$("#submit3").click(function(){
		var CardNum=$("#CardNum").val();
		var S_CardWorth=$("#S_CardWorth").val();
		$("#CardNum").css("border-color","#666 #ccc #ccc #666");
		$("#S_CardWorth").css("border-color","#666 #ccc #ccc #666");
		if(CardNum==""){
			$("#CardNum").css("border","1px solid red");
			return false;
			};
		else if(S_CardWorth==""){
			$("#S_CardWorth").css("border","1px solid red");
			return false;
			};
	});
});
function del(id){
	jConfirm('您确定要删除该充值卡吗?', '操作提示', function(r) {
		if(r==true){
			window.location='?action=del&S_ID='+id;
			}
				});
		
};
function delall(){
	jConfirm('您确定要执行操作吗?', '操作提示', function(r) {
		if(r==true){form2.submit();}
		});

};
function turnback(){
	window.history.go(-1);
};
</script>
</head>

<body>
<?php
class ui_card{
	//DBU类构造函数
	public function editboard($ActionUrl,$emValues){
		global $db;
		if(is_array($emValues)){
			$S_ID = $emValues[0];
			$S_CardNum = $emValues[1];
			$S_CardPWD = $emValues[2];
			$S_CardGroup = $emValues[3];
			$S_CardState = $emValues[4];
			$S_CardUsed = $emValues[5];
			$S_CardVip = $emValues[6];
			$S_CardWorth = $emValues[7];
		}
		?>
        <form id="form1" action="<?php echo $ActionUrl?>" method="post">
		<table width="100%" border="0" cellpadding="0" cellspacing="0"  class="tb_style">
        	<tr>
            	<td colspan="2" class="td_title">&nbsp;充值卡编辑</td>
          </tr>
            
			<tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;充值卡号：</td>
                <td width="86%" height="30" class="td_border"><input type="text" name="S_CardNum" id="S_CardNum" value="<?php echo $S_CardNum?>" size="30" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>
                  <font color="#FF0000">＊</font></td>
            </tr>
			<tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;充值卡密：</td>
                <td height="30" class="td_border"><input type="text" name="S_CardPWD" id="S_CardPWD" value="<?php echo $S_CardPWD?>" size="30" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>
                  <font color="#FF0000">＊</font></td></tr>
            <tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;卡片类型：</td>
                <td height="30" class="td_border"><input type="radio" class="radio" value="0" name="S_CardGroup"<?php if($S_CardGroup==0){echo("checked='checked'");}?>/>点卡&nbsp;&nbsp;<input type="radio" class="radio" value="1" name="S_CardGroup"<?php if($S_CardGroup==1){echo("checked='checked'");}?> />月卡</td>
        </tr>
            <tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;点数/月数：</td>
                <td height="30" class="td_border"><input name="S_CardWorth" id="S_CardWorth" type="text" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" value="<?php echo $S_CardWorth?>" size="30" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>
                  <font color="#FF0000">＊</font></td>
            </tr>
      <tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;是否发送：</td>
                <td height="30" class="td_border"><input type="radio" class="radio" value="1" name="S_CardState"<?php if($S_CardState==1){echo("checked='checked'");}?> />已发<input type="radio" class="radio" value="0" name="S_CardState"<?php if($S_CardState==0){echo("checked='checked'");}?> />未发</td>
          </tr>
            <tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;是否使用：</td>
                <td height="30" class="td_border"><input type="radio" class="radio" value="1" name="S_CardUsed"<?php if($S_CardUsed==1){echo("checked='checked'");}?> />已用<input type="radio" class="radio" value="0" name="S_CardUsed"<?php if($S_CardUsed==0){echo("checked='checked'");}?> />未使用</td>
            </tr>
            <tr>
                <td width="14%" height="30" class="td_border">&nbsp;&nbsp;使用会员：</td>
                <td height="30" class="td_border"><input name="S_CardVip" type="text" value="<?php echo $S_CardVip?>" size="30" />
                  <font color="#FF0000">＊</font></td>
            </tr>
            <tr>
              <td height="30" class="td_border" align="right">&nbsp;&nbsp;</td>
                <td height="30" class="td_border" align="left"><input name="submit2" id="submit2" type="submit" class="rb1" value="确认编辑" />                  
                  &nbsp;&nbsp;
              <input type="button" onclick="turnback()" value="返回列表" class="rb1"/></td>
            </tr>
		</table>
		</form>
	<?php
	}
	
	
	function lists($sql,$size){
		global $db;
		$Arr=getpagerow($sql,$size);//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$action=SafeRequest("action","get");
		?>
    	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tb_style">
        	<tr>
            	<td class="td_title">&nbsp;会员充值卡管理<a href="?action=makecard"></a></td>
            </tr>
            <tr><form action="?action=search" method="post">
              <td height="40" class="td_border">
                &nbsp;&nbsp;充值卡查询：                &nbsp;&nbsp;
                <input type="text" size="20" name="SearchStr" id="SearchStr" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/>                &nbsp;&nbsp;
               <input type="submit" value="查 找" class="rb1" id="search1" name="search1" />
&nbsp;&nbsp;&nbsp;
<input type="button" name="input3" value="添加充值卡片" class="rb1" onclick=location.href='?action=makecard'  />
                &nbsp;
                &nbsp; 
                <input type="button" name="input3" value="已充值卡片" class="rb1" onclick=location.href='?action=yishiyong'  />
                &nbsp;
                &nbsp; 
                <input type="button" name="input3" value="未充值卡片" class="rb1" onclick=location.href='?action=weishiyong'  />
			  </td>
            </form>
            </tr>
        </table>

    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="tb_style">
	<form id="form2" method="post" action="?action=delall">
		<tr>
        	<td height="30" bgcolor="#FFFFFF" class="td_btop3" width="9%">  &nbsp;编号</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" width="23%">&nbsp;充值卡号</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" width="13%">&nbsp;充值卡密</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" align="center" width="9%">类型</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" align="center" width="11%">点数/月数</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" align="center" width="11%">使用状态</td>
            <td width="14%" height="30" align="center" bgcolor="#FFFFFF" class="td_btop3">充值会员</td>
            <td height="30" bgcolor="#FFFFFF" class="td_btop3" align="center" width="10%">操作</td>
        </tr>
	<?php
	$numdate="a";
    if($result){
		while ($row = $db ->fetch_array($result)){
	?>
    	<tr>
       	  <td width="9%"  height="30" align="left" class="td_border"><input type="checkbox" class="checkbox" value="<?php echo $row["S_ID"]?>" name="S_ID[]" /><?php echo $row["S_ID"]?>.</td>
          <td width="23%" height="30" class="td_border">&nbsp;<a href="?action=edit&S_ID=<?php echo $row["S_ID"]?>"><?php echo $row["S_CardNum"]?></a></td>
            <td width="13%" height="30" class="td_border">&nbsp;<?php echo $row["S_CardPWD"]?></td>
            <td width="9%" height="30" align="center" class="td_border"><?php if($row["S_CardGroup"]==0){?>点卡<?php }else{?>月卡<?php }?></td>
            <td width="11%" height="30" align="center" class="td_border"><?php echo $row["S_CardWorth"]?></td>
            <td width="11%" height="30" align="center" class="td_border"><?php if($row["S_CardUsed"]==0){?>未被冲值<?php }else{?><font color="#FF0000">已被冲值</font><?php }?></td>
            <td width="14%" height="30" align="center" class="td_border"><?php if($row["S_CardVip"]==""){?>无人使用
          <?php }else{?><?php echo $row["S_CardVip"]?><?php }?></td>
            <td width="10%" height="30" align="center" class="td_border"><a href="?action=edit&S_ID=<?php echo $row["S_ID"]?>">编辑</a>&nbsp;&nbsp;<a style="cursor:pointer;" onclick="del(<?php echo $row["S_ID"]?>)">删除</a></td>
        </tr>
	<?php
	$numdate="b";
		}
	}
	if($numdate=="a"){
		echo("<tr><td height='30' align='center' class='td_border' colspan='8'><b>暂无充值卡！</b></td></tr>");
	}
	?>
		<tr>
       	  <td colspan="9" class="td_border"><input type="checkbox" name="ChkAll" id="ChkAll" class="checkbox"/>
        	  全选&nbsp; &nbsp;
        	  <input type="button" value="删除所选卡片" class="rb1" onclick="delall()"/>
        	  </td>
       	</tr>
		<tr>
		  <td height="40" colspan="9" align="right">
          <?php echo $Arr[0];?>
		  </td>
	  </tr>
    </table> 
    </form>
	<?php
	}
	
}
?>

<?php

$action=SafeRequest("action","get");
$ui=new ui_card;
switch($action){
	case "del" :
		Del();
		break;
	case "delall" :
		DelAll();
		break;
	case "edit" :
		Edit();
		break;
	case "saveedit" :
		SaveEdit();
		break;
	case "search" :
		Search();
		break;
	case "makecard" :
		MakeCard();
		break;
	case "saveadd" :
		SaveAdd();
		break;
	case "yifasong" :
		YiFaSong();
		break;
	case "weifasong" :
		WeiFaSong();
		break;
	case "yishiyong" :
		YiShiYong();
		break;
	case "weishiyong" :
		WeiShiYong();
		break;
	default:
		Main();
		break;
}

function Main(){
	global $ui;
	$ui->lists("select * from s_card Order by S_ID asc",30);
}

function Search(){
	global $ui;
	$S_CardNum = SafeRequest("SearchStr","post");
	$ui->lists("select * from s_card where S_CardNum=".$S_CardNum." Order by S_ID asc",30);
}

function YiShiYong(){
	global $ui;
	$ui->lists("select * from s_card where S_CardUsed=1 Order by S_ID asc",30);
}

function WeiShiYong(){
	global $ui;
	$ui->lists("select * from s_card where S_CardUsed=0 Order by S_ID asc",30);
}

function Del(){
	global $db;
	$S_ID = SafeRequest("S_ID","get");
	$sql="delete from s_card where S_ID=".$S_ID;
	if($db->query($sql)){
		echo "<script>window.location='Admin_Card.php'</script>";
		}
	else{
		echo "<script>jAlert('删除失败!','操作提示',function(R){history.back();})</script>";
		}
}

function DelAll(){
	global $db;
	$S_ID=RequestBox("S_ID");
	$S_ID=explode(",",$S_ID);
	for($i=0;$i<count($S_ID);$i++){
		$sql="delete from s_card where S_ID=".$S_ID[$i];
		$db->query($sql);
		}
	echo "<script>window.location='admin_card.php'</script>";

}

function Edit(){
	global $db;
	global $ui;
	$S_ID=SafeRequest("S_ID","get");
	$sql="select * from s_card where S_ID=".$S_ID;
	$result=$db->query($sql);
	$row=$db->fetch_array($result);
	$S_CardNum = $row["S_CardNum"];
	$S_CardPWD = $row["S_CardPWD"];
	$S_CardGroup = $row["S_CardGroup"];
	$S_CardState = $row["S_CardState"];
	$S_CardUsed = $row["S_CardUsed"];
	$S_CardVip = $row["S_CardVip"];
	$S_CardWorth = $row["S_CardWorth"];
	$Arr=array($S_ID,$S_CardNum,$S_CardPWD,$S_CardGroup,$S_CardState,$S_CardUsed,$S_CardVip,$S_CardWorth);
	$ui->editboard("?action=saveedit&S_ID=".$S_ID,$Arr);
}

function SaveEdit(){
	global $db;
	$S_ID = SafeRequest("S_ID","get");
	$S_CardNum = SafeRequest("S_CardNum","post");
	$S_CardPWD = SafeRequest("S_CardPWD","post");
	$S_CardGroup = SafeRequest("S_CardGroup","post");
	$S_CardState = SafeRequest("S_CardState","post");
	$S_CardUsed = SafeRequest("S_CardUsed","post");
	$S_CardVip = SafeRequest("S_CardVip","post");
	$S_CardWorth = SafeRequest("S_CardWorth","post");
	$sqlc="select * from s_card where S_ID=".$S_ID;
	$resultc=$db->query($sqlc);
	$rowc=$db->fetch_array($resultc);
	if($rowc["S_CardNum"]!=$S_CardNum){
		$sqlc1="select * from s_card where S_CardNum=".$S_CardNum;
		if($db->query($sqlc1)){
			$resultc1=$db->query($sqlc1);
			$rowc1=$db->fetch_array($resultc1);
			if($rowc1["S_CardNum"]==$S_CardNum){
				die("<script>jAlert('卡号已经存在!','操作提示',function(R){history.back();})</script>");
			}
		}
	}
	if($S_CardVip!=""){
		$sqlu="select * from s_users where S_Name='".$S_CardVip."'";
		if($db->query($sqlu)){
			$resultu=$db->query($sqlu);
			$rowu=$db->fetch_array($resultu);
			if($rowu["S_Name"]==$S_CardVip){
			}
			else{
				die("<script>jAlert('用户名不存在!','操作提示',function(R){history.back();})</script>");	
			}
		}
	}
	$sql="update s_card set S_CardNum='".$S_CardNum."',S_CardPWD='".$S_CardPWD."',S_CardGroup='".$S_CardGroup."',S_CardState='".$S_CardState."',S_CardUsed='".$S_CardUsed."',S_CardVip='".$S_CardVip."',S_CardWorth='".$S_CardWorth."' where S_ID=".$S_ID;
	if($db->query($sql)){
		echo "<script>window.location='admin_card.php'</script>";
	}
	else{
		echo "<script>jAlert('卡片修改失败!','操作提示',function(R){history.back();})</script>";
	}
}

function SaveAdd(){
	global $db;
	$CardNum = SafeRequest("CardNum","post");
	$S_CardGroup = SafeRequest("S_CardGroup","post");
	$S_CardState = 0;
	$S_CardUsed = 0;
	$S_CardWorth = SafeRequest("S_CardWorth","post");
	if($S_CardGroup==0){ 
		$aa="点卡";
	}
	else{
		$aa="月卡";
	}
	for($i=1;$i<=$CardNum;$i++){
		$S_CardNum = gen_key(16);
		$S_CardPWD = gen_key(8);
		$sql="insert into s_card (S_CardNum,S_CardPWD,S_CardGroup,S_CardState,S_CardUsed,S_CardWorth) values ('".$S_CardNum."','".$S_CardPWD."','".$S_CardGroup."','".$S_CardState."','".$S_CardUsed."','".$S_CardWorth."')";
		
		$db->query($sql);
		echo("<br/><font color='red'>&nbsp;&nbsp;序号：".$i." 卡号：".$S_CardNum." 卡密：".$S_CardPWD." 类型：".$aa." 点数/月数：".$S_CardWorth."</font><br/><br/>");
	}
	echo("<br/>&nbsp;&nbsp;<a href='admin_card.php'>返回充值卡列表</a>");
}

function MakeCard(){
	?>
<form action="?action=saveadd" method="post">
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tb_style">
    	<tr>
        	<td colspan="2" class="td_title">&nbsp;生成充值卡</td>
        </tr>
    	<tr>
        	<td width="14%" height="30" align="right" class="td_border">生成数目：</td>
            <td width="86%" height="30" class="td_border"><input type="text" name="CardNum" id="CardNum" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/><label id="tisih1"></label></td>
        </tr>
        <tr>
            <td height="30" class="td_border" align="right">&nbsp;&nbsp;充值类型：</td>
            <td height="30" class="td_border">
<input type="radio" class="radio" value="0" name="S_CardGroup" checked="checked"/>点卡
           &nbsp; <input type="radio" class="radio" value="1" name="S_CardGroup"/>月卡
            </td>
        </tr>
        <tr>
            <td height="30" class="td_border" align="right">&nbsp;&nbsp;点数/月数：</td>
            <td height="30" class="td_border"><input type="text" name="S_CardWorth" id="S_CardWorth" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"/><label id="tisih2"></label></td>
        </tr>
		<tr>
            <td height="30" class="td_border" align="right">&nbsp;</td>
            <td height="30" class="td_border"><input name="submit" type="submit" class="rb1" id="submit3" value="生成卡片"/></td>
        </tr>
    </table>
</form>
	<?php
}

function gen_key($digits){
	$output='';
	for ($a = 0; $a < $digits; $a++) {
	$output =$output.(rand(0,9));    //生成php随机数
	}
	return $output;
}

include "../admin_bottom.php";
?>


</body>
</html>