<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!--#include file="../Conn.asp"-->
<!--#include file="../KS_Cls/Kesion.MemberCls.asp"-->
<!--#include file="../KS_Cls/Kesion.Label.CommonCls.asp"-->
<!--#include file="../KS_Cls/Template.asp"-->
<!--#include file="../KS_Cls/Kesion.IFCls.asp"-->
<%

Dim KSCls
Set KSCls = New GroupBuyIndex
KSCls.Kesion()
Set KSCls = Nothing

Class GroupBuyIndex
        Private KS, KSR,KSUser,Product,LoginTf
		Private GroupBuy,K,CurrentPage,totalPut,MaxPerPage,ID
		Private Sub Class_Initialize()
		 If (Not Response.IsClientConnected)Then
			Response.Clear
			Response.End
		 End If
		  MaxPerPage=10         rem 定义每页显示条数
		  Set KS=New PublicCls
		  Set KSR = New Refresh
		  Set KSUser=New UserCls
		End Sub
        Private Sub Class_Terminate()
		 Call CloseConn()
		 Set KS=Nothing
		 Set KSUser=Nothing
		End Sub
		Public Sub Kesion()
		  LoginTF=Cbool(KSUser.UserLoginChecked)
		  If KS.S("Action")="dosave" Then
		    Call DoSave()
		  Else
		    Call showmain()
		  End If
		End Sub
		%>
		<!--#include file="../KS_Cls/Kesion.IFCls.asp"-->
		<%
		Sub ShowMain()
			 Dim FileContent
			 FileContent = KSR.LoadTemplate(KS.Setting(138))    
			 FCls.RefreshType = "groupbyIndex" '设置刷新类型，以便取得当前位置导航等
			 FCls.RefreshFolderID = "0" '设置当前刷新目录ID 为"0" 以取得通用标签
			 GetQueryParam
			 LoadGroupBuyList
			 LoadProductList
			 Immediate=false
			 Scan FileContent
			 Templates=KSR.KSLabelReplaceAll(Templates)
			 Response.write RexHtml_IF(Templates)
		End Sub
		
		
		Sub ParseArea(sTokenName, sTemplate)
			Select Case lcase(sTokenName)
			 case "purview"
			   If (GroupBuy(14,0)<>"0" And LoginTF=false) or (GroupBuy(14,0)="2" and KS.FoundInArr(GroupBuy(15,0),KSUser.GroupID,",")=false) Then
				  Scan sTemplate
			   End If
			 case "qsbm"
			    If (GroupBuy(14,0)<>"0" And LoginTF=false) or (GroupBuy(14,0)="2" and KS.FoundInArr(GroupBuy(15,0),KSUser.GroupID,",")=false) Then
			    Else
				 Scan sTemplate
				End iF
			 case "productlist"
			  If IsArray(Product) Then
			     For K=0 To Ubound(Product,2)
				  Scan sTemplate
				 Next 
			  End If
			End Select 
        End Sub 
		
		Sub ParseNode(sTokenType, sTokenName)
			Select Case lcase(sTokenType)
			   case "groupbuy"
					 Select  case lcase(sTokenName)
					  case "id" echo GroupBuy(0,0)
					  case "subject" echo GroupBuy(1,0)
					  case "date"  echo Year(GroupBuy(2,0)) & "年" & Month(GroupBuy(2,0)) & "月" & Day(GroupBuy(2,0)) & "日"
					  case "time" echo GroupBuy(3,0)
					  case "address" echo GroupBuy(4,0)
					  case "adddate" echo GroupBuy(5,0)
					  case "intro" echo KS.HtmlCode(GroupBuy(6,0))
					  case "photourl" if GroupBuy(7,0)="" or isnull(GroupBuy(7,0)) Then Echo "../images/nopic.gif" Else Echo GroupBuy(7,0)
					  case "interest" EchoInterest
					  case "highlights" echo GroupBuy(8,0)
					  case "protection" echo GroupBuy(9,0)
					  case "buyflow" echo GroupBuy(10,0)
					  case "notes" echo GroupBuy(11,0)
					  case "bus" echo GroupBuy(12,0)
					  case "telphone" echo groupbuy(13,0)
					  case "realname" echo KSUser.GetUserInfo("RealName")
					  case "mobile" echo KSUser.GetUserInfo("Mobile")
					 end select
		      case "product" 
			        select case lcase(sTokenName)
					  case "id" echo Product(0,k)
					  case "name" echo Product(1,k)
					  case "photourl" if Product(3,k)="" or isnull(Product(3,k)) Then Echo "../images/nopic.gif" Else Echo Product(3,k)
					  case "pricemarket" echo Product(5,k)
					  case "price" echo Product(6,k)
					  case "remark" echo KS.GotTopic(KS.LoseHtml(Product(7,k)),150)
					  case "title" echo Product(4,k)
					  case "brandname" echo Product(2,k)
					end select
			  case "foot"
			         if sTokenName="showpage" then
				      Echo ShowPagePara(totalput, MaxPerPage, "", true, "条", CurrentPage,KS.QueryParam("page"))
					 end if
		    End Select 
        End Sub 
		
		
		Sub GetQueryParam()
		  ID=KS.ChkClng(KS.S("ID"))
		  If ID=0 Then
		   Call KS.AlertHintScript("参数出错!")
		   Response.End
		  End If
		  If KS.S("page") <> "" Then
			  CurrentPage = CInt(Request("page"))
		  Else
			  CurrentPage = 1
		  End If
		End Sub
		
		Sub EchoInterest()
		  Dim RS,Interest,n
		  Set RS=Conn.Execute("Select Item FROM KS_GroupBuyInt Where SubjectID=" & ID & " Or SubjectID=0 Order By OrderID")
		  IF Not RS.Eof Then
		    Interest=RS.GetRows(-1)
			For n=0 To Ubound(interest,2)
			 echo "<input type='checkbox' name='interest' value='" & interest(0,n) & "'>" & interest(0,n) & "&nbsp;"
			 if (n+1) mod 4=0 then echo "<br />"
			Next
		  End If
		End Sub

		
		Sub LoadGroupBuyList()
		 Dim RS:Set RS=Server.CreateObject("ADODB.RECORDSET")
		 RS.Open "Select TOP 1 ID,Subject,ActiveDate,ActiveTime,address,adddate,intro,photourl,Highlights,Protection,BuyFlow,Notes,BusRoute,Telphone,AllowBMFlag,AllowArrGroupID From KS_GroupBuy Where ID=" & ID & " And Endtf=0 and Locked=0 Order By Id Desc",Conn,1,1
		 If RS.Eof And RS.Bof Then
		   RS.Close:Set RS=Nothing
		   Call KS.AlertHintScript("参数出错或本活动已关闭!")
		   Response.End()
		 Else
		   GroupBuy=RS.GetRows(-1)
		 End If
		 RS.Close:Set RS=Nothing
		End Sub

		Sub LoadProductList()
		 Dim RS:Set RS=Server.CreateObject("ADODB.RECORDSET")
		 RS.Open "Select ID,ProductName,BrandName,PhotoUrl,Title,PriceMarket,Price,Remark,AddDate From KS_GroupBuyPro Where SubjectID=" & ID & " And status=1 Order By Id Desc",Conn,1,1
		 If Not RS.Eof Then
		   Product=RS.GetRows(-1)
		 End If
		 RS.Close:Set RS=Nothing
		End Sub

		
		Sub DoSave()
		   Dim Subjectid:Subjectid=KS.ChkClng(KS.G("Subjectid"))
		   If Subjectid=0 Then
				 Response.Write "<script>alert('对不起,您没有选择团购主题！');history.back();</script>"
				 Exit Sub
		   End If
		   Dim RealName:RealName=KS.LoseHtml(KS.G("RealName"))
		   Dim Mobile:Mobile=KS.LoseHtml(KS.G("Mobile"))
		   Dim Interest:Interest=Replace(KS.LoseHtml(KS.G("Interest"))," ","")
		   Dim AcceptSms:AcceptSms=KS.ChkClng(KS.G("AcceptSms"))
		   If RealName="" Then
		    Call KS.AlertHintScript("请输入真实姓名!")
			Exit Sub
		   End If
		   If Mobile="" Then 
		    Call KS.AlertHintScript("请输入电话!")
			Exit Sub
		   End If
		 
		    If Not Conn.Execute("Select * From KS_GroupBuyBM Where SubjectID=" & SubjectID & " And RealName='" & RealName & "' And Mobile='" & Mobile & "'").Eof Then
			 Call KS.Alert("对不起,您已报名过 ,请等待管理员的审核!",Request.ServerVariables("HTTP_REFERER"))
			 Exit SUb
			End If
			
	    	  Dim RS: Set RS=Server.CreateObject("Adodb.RecordSet")
			  RS.Open "Select TOP 1 ID,Subject,ActiveDate,ActiveTime,address,adddate,intro,photourl,Highlights,Protection,BuyFlow,Notes,BusRoute,Telphone,AllowBMFlag,AllowArrGroupID From KS_GroupBuy Where ID=" & Subjectid & " And Endtf=0 and Locked=0 Order By Id Desc",Conn,1,1
			  If RS.Eof And RS.Bof Then
			     RS.Close : Set RS=Nothing
				 Call KS.Alert("请不要非法提交!",Request.ServerVariables("HTTP_REFERER"))
				 Exit SUb
			  Else
               	 If (RS("AllowBMFlag")<>"0" And LoginTF=false) or (RS("AllowBMFlag")="2" and KS.FoundInArr(RS("AllowArrGroupID"),KSUser.GroupID,",")=false) Then
				   RS.Close :Set RS=Nothing
                  Call KS.Alert("对不起，您没有参加报名的权限!",Request.ServerVariables("HTTP_REFERER"))
				   Exit SUb
				End iF
			  End If			
			  RS.Close
			  RS.Open "Select top 1 * From KS_GroupBuyBM Where 1=0",Conn,1,3
			     RS.AddNEW
			     RS("SubjectID")=SubjectID
			     RS("RealName")=RealName
				 RS("Mobile")=Mobile
				 RS("AcceptSms")=AcceptSms
				 RS("Interest")=Interest
				 RS("UserName")=KSUser.UserName
				 RS("Status")=0
		 		 RS.Update
			     RS.Close
				 Set RS=Nothing
			 Call KS.Alert("恭喜,您已成功报名,请等待管理员的审核!",Request.ServerVariables("HTTP_REFERER"))
		End Sub
		

         Public Function ShowPagePara(totalnumber, MaxPerPage, FileName, ShowAllPages, strUnit, CurrentPage, ParamterStr)
		  Dim N, I, PageStr
				Const Btn_First = "<span style='font-family:webdings;font-size:14px' title='第一页'>9</span>" '定义第一页按钮显示样式
				Const Btn_Prev = "<span style='font-family:webdings;font-size:14px' title='上一页'>3</span>" '定义前一页按钮显示样式
				Const Btn_Next = "<span style='font-family:webdings;font-size:14px' title='下一页'>4</span>" '定义下一页按钮显示样式
				Const Btn_Last = "<span style='font-family:webdings;font-size:14px' title='最后一页'>:</span>" '定义最后一页按钮显示样式
				  PageStr = ""
					If totalnumber Mod MaxPerPage = 0 Then
						N = totalnumber \ MaxPerPage
					Else
						N = totalnumber \ MaxPerPage + 1
					End If
					PageStr = PageStr & ("<div class='showpage' style='height:20px'><form action=""" & FileName & "?" & ParamterStr & """ name=""myform"" method=""post"">共 <font color=red>" & totalnumber & "</font> " & strUnit & "  分 <font color=red>" & N & "</font> 页 每页 <font color=red>" & MaxPerPage &"</font> " & strUnit &" | 当前第 <font color=red>" & CurrentPage & "</font> 页 &nbsp;&nbsp;&nbsp;")
					If CurrentPage < 2 Then
						PageStr = PageStr & Btn_First & " " & Btn_Prev & " "
					Else
						PageStr = PageStr & ("<a href=" & FileName & "?page=1" & "&" & ParamterStr & ">" & Btn_First & "</a> <a href=" & FileName & "?page=" & CurrentPage - 1 & "&" & ParamterStr & ">" & Btn_Prev & "</a> ")
					End If
					
					If N - CurrentPage < 1 Then
						PageStr = PageStr & " " & Btn_Next & " " & Btn_Last & " "
					Else
						PageStr = PageStr & (" <a href=" & FileName & "?page=" & (CurrentPage + 1) & "&" & ParamterStr & ">" & Btn_Next & "</a> <a href=" & FileName & "?page=" & N & "&" & ParamterStr & ">" & Btn_Last & "</a> ")
					End If
					If ShowAllPages = True Then
						PageStr = PageStr & ("转到:<input type='text' value='" & (CurrentPage + 1) &"' name='page' style='width:30px;height:18px;text-align:center;'>&nbsp;<input style='height:18px;border:1px #a7a7a7 solid;background:#fff;' type='submit' value='GO' name='sb'>")
				  End If
				  PageStr = PageStr & "</form></div>"
			 ShowPagePara = PageStr
	End Function

End Class
%>
