<!--#include file="../../conn.asp"-->
<!--#include file="../inc/setup.asp"-->
<!--#include file="../inc/const.asp"-->
<!--#include file="../inc/check.asp"-->
<%
Admin_header
'=====================================================================
' 软件名称：新云网站管理系统
' 当前版本：NewAsp Content Management System Version 4.0
' 文件名称：admin_card.asp
' 更新日期：2008-09-16
' 官方网站：新云网络(www.NewAsp.net www.NewAsp.cn) QQ：94022511
'=====================================================================
' Copyright 2003-2008 NewAsp.net - All Rights Reserved.
' newasp is a trademark of NewAsp.net
'=====================================================================
%>
<table class="table1" cellspacing="1" cellpadding="3" align="center" border="0">
	<tr>
		<td class="tableline linetitle" width="100" align="left">充值卡管理</td>
		<td class="tableline" width="*" align="right"><a href="admin_index.asp">管理首页</a>
			 - <a href="admin_card.asp">充值卡列表</a>
			 - <a href="admin_card.asp?action=add" style="color:red">添加充值卡</a>
			 - <a href="admin_userorder.asp">会员充值定单</a>
			 - <a href="admin_account.asp">交易明细查询</a>
			 - <a href="admin_confirm.asp">交费确认</a>
			 - <a href="admin_paymode.asp">付款方式</a>
			 - <a href="admin_list.asp">用户列表</a>
		</td>
	</tr>
</table>
<%
Dim maxperpage,totalrec,Pcount,pagelinks,showmode,pagenow,count
Dim Action,listArry,SQLQuery

If Not ChkAdmin("Card") Then
	Call Transfer_error()
End If
Action = LCase(Request("action"))
Select Case Trim(Action)
Case "del"
	Call delCard()
Case "save"
	Call saveCard()
Case "add"
	Call addCard()
Case "pay"
	Call payCard()
Case "upcard"
	Call UserCard()
Case "delfinish"
	Call delFinishCard()
Case Else
	Call showmain()
End Select
If FoundErr=True Then
	ReturnError(ErrMsg)
End If
Admin_footer
SaveLogInfo(AdminName)
NewAsp.PageEnd

Sub showmain()
	Dim i,iCount,lCount
	iCount=1:lCount=2
	maxperpage = 30 '###每页显示数
	count=NewAsp.ChkNumeric(Request("count"))
	pagenow=NewAsp.ChkNumeric(Request("page"))
	If pagenow=0 Then pagenow=1
	Call showQueryform()
%>
<form name="selform" method="post" action="?action=del">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
	<th width="5%" nowrap>选择</th>
	<th width="15%" nowrap>充 值 卡 号</th>
	<th width="15%" nowrap>充 值 卡 密 码</th>
	<th width="15%" nowrap>充 值 卡 面 值</th>
	<th width="15%" nowrap>充 值 卡 点 数</th>
	<th width="10%" nowrap>充 值 卡 时 长</th>
	<th width="10%" nowrap>充 值 卡 类 型</th>
	<th width="10%" nowrap>会 员 名 称</th>
</tr>
<tr>
	<td class="tablerow1" colspan="8" align="left" id="showNextPage">&nbsp;</td>
</tr>
<%
	Call showCardlist()
	If IsArray(listArry) Then
		For i=0 To Ubound(listArry,2)
			If Not Response.IsClientConnected Then Response.End
			If (i mod 2) = 0 Then iCount=2:lCount=1 Else iCount=1:lCount=2
%>
<tr align="center">
	<td class="tablerow<%=iCount%> hovered"><input type="checkbox" name="carid" value="<%=listArry(0,i)%>"></td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap" title="点击此处对会员进行充值">
	<%If CLng(listArry(9,i)) <> 1 Then%>
	<a href="?action=pay&carid=<%=listArry(0,i)%>"><%=listArry(1,i)%></a>
	<%Else%>
	<font color="red"><%=listArry(1,i)%></font>
	<%End If%>
	</td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap"><%=listArry(2,i)%></td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap"><%=FormatCurrency(listArry(5,i),2,-1)%> 元</td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap"><%=listArry(6,i)%></td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap"><%=listArry(4,i)%></td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap">
<%
	If CLng(listArry(3,i)) = 0 Then
		Response.Write "<span class=""style1"">计点充值卡"
	ElseIf listArry(3,i) = 1 Then
		Response.Write "<span class=""style2"">计时充值卡"
	Else
		Response.Write "<span class=""style3"">会员升级卡"
	End If
	Response.Write "</span>"
%>
	</td>
	<td class="tablerow<%=iCount%> hovered" nowrap="nowrap" title="点击查看会员信息">
<%
	If Len(listArry(7,i)&"") > 0 And listArry(7,i) <> "无" Then
		Response.Write "<a href=""admin_user.asp?action=edit&username="& listArry(7,i) &""">"& listArry(7,i) &" </a>"
	Else
		Response.Write "暂无会员使用"
	End If
%>
	
	</td>
</tr>
<%
		Next
		listArry=Null
	End If
%>
<tr>
	<td class="tablerow<%=lCount%>" colspan="8">
	<input class="button" type="button" name="chkall" value="全选" onClick="CheckAll(this.form)"><input class="button" type="button" name="chksel" value="反选" onClick="ContraSel(this.form)">
	<input class="button" type="submit" name="submit_button1" value="删除充值卡" onclick="return confirm('您确定要删除选择的充值卡吗?');">
	<input class="button" type="button" name="submit_button2" value="清空已充卡" onclick="{if(confirm('您确定要清空所有已充值的卡吗?')){location.href='admin_card.asp?action=delfinish';return true;}return false;}">
	</td>
</tr>
<tr>
	<td class="tablerow<%=iCount%>" colspan="8" id="NextPageText"><var class="morePage"><%=showlistpage(pagenow,Pcount,maxperpage,totalrec,pagelinks)%></var></td>
</tr>
</table>
</form>
<script type="text/javascript">
document.getElementById("showNextPage").innerHTML = document.getElementById("NextPageText").innerHTML;
</script>
<%
End Sub

Sub showCardlist()
	Dim Rs,SQL,keyword,UserGroup,strByOrder
	UserGroup=NewAsp.ChkNumeric(Request("groupid"))
	showmode=NewAsp.ChkNumeric(Request("showmode"))
	If Not IsNull(Request("word")) And Request("word") <> "" Then
		keyword = Trim(Replace(Request("word"), "'", ""))
		If IsSqlDataBase=1 Then
			SQLQuery="WHERE CardMark like '"& keyword &"%' Or UserName like '"& keyword &"%'"
		Else
			SQLQuery="WHERE InStr(1,LCase(CardMark),LCase('"&keyword&"'),0)>0 Or InStr(1,LCase(UserName),LCase('"&keyword&"'),0)>0"
		End If
	Else
		If UserGroup = 0 Then
			If showmode=1 Then
				SQLQuery="WHERE IsConsume=1"
			ElseIf showmode=2 Then
				SQLQuery="WHERE IsConsume<>1"
			Else
				SQLQuery=""
				strByOrder="IsConsume DESC,"
			End If
		Else
			SQLQuery="WHERE UserGroup="& UserGroup
		End If
	End If
	If count=0 Then
		totalrec=NewAsp.Execute("SELECT COUNT(*) FROM [NC_Card] "&SQLQuery&"")(0)
	Else
		totalrec=count
	End If
	Pcount = CLng(totalrec / maxperpage)
	If Pcount < totalrec / maxperpage Then Pcount = Pcount + 1
	If pagenow>Pcount Then pagenow=1
	pagelinks="?showmode="&Request("showmode")&"&UserGroup="&UserGroup&"&word="&Request("word")&"&count="&totalrec&"&"
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT CardID,CardMark,CardPass,CardType,UseDayNum,CardMoney,addPoint,UserName,UserGroup,IsConsume FROM [NC_Card] "&SQLQuery&" ORDER BY "&strByOrder&" CardID DESC"
	Rs.Open SQL,Conn,1,1
	If pagenow >1 Then
		Rs.Move (pagenow-1) * maxperpage
	End If
	If Not (Rs.BOF And Rs.EOF) Then
		listArry=Rs.GetRows(maxperpage)
	Else
		listArry=Null
	End If
	Rs.close()
	Set Rs=Nothing
End Sub

Sub showQueryform()
%>
<form name="queryform" method="post" action="">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
	<th>会员充值卡管理</th>
</tr>
<tr>
	<td class="tablerow2">
	<input type="text" name="word" size="30" value="<%=Server.HTMLEncode(Request("word")&"")%>"> <input type="submit" name="query_button" value=" 查找 " class="button">
	你可以在此批量增加或者删除充值卡；点击充值卡号可以对会员进行手动充值。</td>
</tr>
<tr>
	<td class="tablerow1"><a href="admin_card.asp">充值卡管理</a> - <a href="admin_card.asp?showmode=2">未充值</a> - <a href="admin_card.asp?showmode=1">已充值</a> - <a href="admin_card.asp?action=add" class="showlink">添加充值卡</a>  
<%
	Dim oRs
	Set oRs = Newasp.Execute("SELECT GroupName,Grades FROM NC_UserGroup WHERE Grades<>0 And Grades<>999 ORDER BY Groupid")
	Do While Not oRs.EOF
		Response.Write " - <a href=""admin_card.asp?groupid="
		Response.Write oRs("Grades")
		Response.Write """>"
		Response.Write oRs("GroupName")
		Response.Write "</a>" & vbCrLf
		oRs.movenext
	Loop
	Set oRs = Nothing
%>
	</td>
</tr>
</table>
</form>
<%
End Sub

Sub addCard()
%>
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<form name="myform" method="post" action="?action=save">
<tr>
	<th colspan="2">批量增加充值卡</th>
</tr>
<tr>
	<td width='20%' class="tablerow1" align="right"><b>充值卡类型：</b></td>
	<td width='80%' class="tablerow1"><select name="cardType">
		<option value="0">计点充值卡</option>
		<option value="1">计时充值卡</option>
	</select></td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>所属用户组：</b></td>
	<td class="tablerow2"><select name='UserGrade'>
<%
	Dim oRs
	Set oRs = NewAsp.Execute("SELECT GroupName,Grades From NC_UserGroup WHERE Grades<>0 And Grades<>999 ORDER BY Groupid")
	Do While Not oRs.EOF
		Response.Write Chr(9) & Chr(9) & "<option value=""" & oRs("Grades") & """>"
		Response.Write oRs("GroupName")
		Response.Write "</option>" & vbCrLf
		oRs.movenext
	Loop
	Set oRs = Nothing
%></select> <span class="style2">! 为不同的会员组设置不同金额的充值卡</span></td>
</tr>
<tr>
	<td class="tablerow1" align="right"><b>充值卡金额：</b></td>
	<td class="tablerow1"><input type="text" name="CardMoney" size="10" value="0"> 元 <span class="style1">! 如果输入0，用户将免费充值</span></td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>充值点数：</b></td>
	<td class="tablerow2"><input type="text" name="addPoint" size="10" value="0"> <span class="style2">! 无论是计时卡或者计点卡，此充值点数都是有效的</span></td>
</tr>
<tr>
	<td class="tablerow1" align="right"><b>有效天数：</b></td>
	<td class="tablerow1"><input type="text" name="UseDayNum" size="10" value="0"> 天 <span class="style1">! 如果是计时卡，请选择有效期</span></td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>增加卡的数量：</b></td>
	<td class="tablerow2"><input type="text" name="num" size="10" value="10"></td>
</tr>
<tr>
	<td class="tablerow1" align="right">&nbsp;</td>
	<td class="tablerow1"><input type="submit" name="submit_button" value="批量增加充值卡" class="button"></td>
</tr>
</table>
</form>
<%
End Sub

Sub saveCard()
	Dim Rs,SQL
	Dim CardNum,CardMark,CardPass
	Dim CardMoney,addPoint,UseDayNum
	Dim cardType,UserGrade,i
	If Trim(Request.Form("CardMoney")) = "" Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>充值卡金额不能为空！</li>"
	Else
		CardMoney = NewAsp.CheckNumeric(Request.Form("CardMoney"))
	End If
	If Trim(Request.Form("addPoint")) = "" Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>充值点数不能为空！</li>"
	Else
		addPoint = NewAsp.ChkNumeric(Request.Form("addPoint"))
	End If
	If Trim(Request.Form("UseDayNum")) = "" Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>有效天数不能为空！</li>"
	Else
		UseDayNum = NewAsp.ChkNumeric(Request.Form("UseDayNum"))
	End If
	CardNum = NewAsp.ChkNumeric(Request.Form("num"))
	cardType = NewAsp.ChkNumeric(Request.Form("cardType"))
	UserGrade = NewAsp.ChkNumeric(Request.Form("UserGrade"))
	If CardNum < 1 Then CardNum = 1
	If Founderr = True Then Exit Sub
	SQL="SELECT * FROM NC_Card WHERE (cardid is null)"
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	Rs.Open SQL,Conn,1,3
	For i = 1 To CardNum
		Randomize
		CardMark = Int((9999999999 - 1000000000 + 1) * Rnd + 1000000000)
		CardPass = Int((9898989898 - 1000000000 + 1) * Rnd + 1000000000)
		Rs.Addnew
		Rs("CardMark") = CardMark
		Rs("CardPass") = CardPass
		Rs("CardMoney") = CardMoney
		Rs("addPoint") = addPoint
		Rs("UseDayNum") = UseDayNum
		Rs("cardType") = cardType
		Rs("UserGroup") = UserGrade
		Rs("UserName") = "无"
		Rs("IsConsume") = 0
		Rs("addExperience") = 0
		Rs("addCharm") = 0
		Rs("addTime") = Now()
		Rs.Update
	Next
	Rs.Close:Set Rs = Nothing
	Response.Redirect "admin_card.asp"
End Sub

Sub delCard()
	If Request("carid") <> "" Then
		NewAsp.Execute ("DELETE FROM NC_Card WHERE CardID in (" & Request("carid") & ")")
	End If
	Response.Redirect Request.ServerVariables("HTTP_REFERER")
End Sub

Sub delFinishCard()
	NewAsp.Execute ("DELETE FROM NC_Card WHERE IsConsume=1")
	Response.Redirect "admin_card.asp"
End Sub

Sub payCard()
	Dim Rs,oRs,SQL
	Dim carid
	carid = NewAsp.ChkNumeric(Request("carid"))
	If carid = 0 Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>系统参数错误！</li>"
		Exit Sub
	End If
	Set Rs = NewAsp.Execute("SELECT * FROM NC_Card WHERE IsConsume<>1 And CardID=" & carid)
	If Rs.BOF And Rs.EOF Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>此卡已经充值，请不要重复使用此卡！</li>"
		Set Rs = Nothing
		'Response.Redirect "admin_card.asp"
		Exit Sub
	End If
%>
<form name="form2" method="post" action="?action=upcard">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<input type="hidden" name="carid" value="<%=Rs("CardID")%>">
<tr>
	<th colspan="2">会员充值</th>
</tr>
<tr>
	<td width='20%' class="tablerow1" align="right"><b>充值卡类型：</b></td>
	<td width='80%' class="tablerow1"><select name="cardType" disabled>
		<option value="0"<%If Rs("cardType") = 0 Then Response.Write " selected"%>>计点充值卡</option>
		<option value="1"<%If Rs("cardType") = 1 Then Response.Write " selected"%>>计时充值卡</option>
	</select></td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>所属用户组：</b></td>
	<td class="tablerow2"><select name='UserGrade' disabled>
<%
	Set oRs = NewAsp.Execute("SELECT GroupName,Grades From NC_UserGroup WHERE Grades<>0 And Grades<>999 ORDER BY Groupid")
	Do While Not oRs.EOF
		Response.Write Chr(9) & Chr(9) & "<option value=""" & oRs("Grades") & """"
		If oRs("Grades") = Rs("UserGroup") Then Response.Write " selected"
		Response.Write ">"
		Response.Write oRs("GroupName")
		Response.Write "</option>" & vbCrLf
		oRs.movenext
	Loop
	Set oRs = Nothing
%></select></td>
</tr>
<tr>
	<td class="tablerow1" align="right"><b>充值卡金额：</b></td>
	<td class="tablerow1"><input type="text" name="CardMoney" size="10" value="<%=Rs("CardMoney")%>" disabled> 元</td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>充值点数：</b></td>
	<td class="tablerow2"><input type="text" name="addPoint" size="10" value="<%=Rs("addPoint")%>" disabled></td>
</tr>
<tr>
	<td class="tablerow1" align="right"><b>有效天数：</b></td>
	<td class="tablerow1"><input type="text" name="UseDayNum" size="10" value="<%=Rs("UseDayNum")%>" disabled> 天</td>
</tr>
<tr>
	<td class="tablerow2" align="right"><b>会员名称：</b></td>
	<td class="tablerow2">
<%
	If Rs("IsConsume") > 1 Then
%>
	<input type="text" name="name" size="15" value="<%=Rs("username")%>" disabled> 
	<input type="hidden" name="username" value="<%=Rs("username")%>">
<%
	Else
%>
	<input type="text" name="username" size="15" value=""> <span class="style1">! 请输入会员名称进行充值操作</span>
<%
	End If
%>
	</td>
</tr>
<tr>
	<td class="tablerow1" align="right">&nbsp;</td>
	<td class="tablerow1"><input type="button" name="go_button" onclick="javascript:history.go(-1)" value="返回上一页" class="button">
	<input type="submit" name="submit_button" value="立即进行充值" class="button"></td>
</tr>
</table>
</form>
<%
	Rs.Close:Set Rs = Nothing
End Sub

Sub UserCard()
	Dim Rs,SQL
	Dim carid,UserName,CardType
	Dim UseDayNum,addPoint,UserGroup
	carid = NewAsp.ChkNumeric(Request("carid"))
	If Trim(Request.Form("UserName")) = "" Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>请输入会员名称！</li>"
		Exit Sub
	Else
		UserName = Replace(Trim(Request.Form("UserName")), "'", "")
	End If
	If carid = 0 Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>系统参数错误！</li>"
		Exit Sub
	End If
	Set Rs = NewAsp.Execute("SELECT CardID,CardMark,CardPass,CardType,UseDayNum,CardMoney,addPoint,UserName,UserGroup,IsConsume FROM NC_Card WHERE IsConsume<>1 And CardID=" & carid)
	If Rs.BOF And Rs.EOF Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>此卡已经充值，请不要重复使用此卡！</li>"
		Set Rs = Nothing
		Exit Sub
	Else
		If Len(Rs("UserName")) > 0 And Rs("UserName") <> "无" Then
			If Rs("UserName") <> UserName Then
				UserName = Replace(Rs("UserName"), "'", "")
			End If
		End If
		CardType = Rs("CardType")
		UseDayNum = Rs("UseDayNum")
		addPoint = Rs("addPoint")
		UserGroup = Rs("UserGroup")
	End If
	Set Rs = Nothing
	If Founderr = True Then Exit Sub
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM NC_User WHERE username='" & UserName & "'"
	Rs.Open SQL,Conn,1,3
	If Rs.BOF And Rs.EOF Then
		FoundErr = True
		ErrMsg = ErrMsg + "<li>会员充值失败！</li><li>此会员可能不存在。</li>"
		Set Rs = Nothing
		Exit Sub
	Else
		Rs("userpoint").value = Rs("userpoint").value + addPoint
		If CardType = 1 Then
			Rs("ExpireTime").value = DateAdd("d",UseDayNum,Now())
			Rs("UserClass").value = 1
		End If
		Rs.update
	End If
	Rs.Close:Set Rs = Nothing
	NewAsp.Execute ("UPDATE NC_Card SET IsConsume=1,UserName='"&UserName&"' WHERE CardID=" & carid)
	'Succeed("<li>会员充值成功！</li>")
	Call OutputScript("会员充值成功！","admin_card.asp")
End Sub
%>