<%@language=vbscript codepage=936 %>
<%
option explicit
response.buffer=true	
Const PurviewLevel=2    '操作权限
%>
<!--#include file="Admin_ChkPurview.asp"-->
<!--#include file="inc/conn.asp"-->
<!--#include file="inc/function.asp"-->
<%
dim sql,rs,i
dim Action,FoundErr,ErrMsg
Action=Trim(Request("Action"))
%>
<html>
<head>
<title>调查管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="760" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
  <tr class="topbg"> 
    <td height="22" colspan=2 align=center><b>网 站 调 查 管 理</b></td>
  </tr>
  <tr class="tdbg"> 
    <td width="70" height="30"><strong>管理导航：</strong></td>
    <td height="30"><a href="Admin_Vote.asp">网站调查管理首页</a> | <a href="Admin_Vote.asp?Action=Add">添加新调查</a></td>
  </tr>
</table>
<%
if Action="Add" then
	call AddVote()
elseif Action="SaveAdd" then
	call SaveAdd()
elseif Action="Modify" then
	call Modify()
elseif Action="SaveModify" then
	call SaveModify()
elseif Action="Set" then
	call SetNew()
elseif Action="Del" then
	call DelVote()
else
	call main()
end if
if FoundErr=True then
	call WriteErrMsg()
end if
call CloseConn()


sub main()
	sql="select * from Vote order by id desc"
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,1
%>
<form method="POST" action="Admin_Vote.asp">
  <table width="760" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
    <tr class="title"> 
      <td width="30" height="22" align="center">选择</td>
      <td width="30" height="22" align="center">ID</td>
      <td height="22" align="center">主题</td>
      <td width="60" height="22" align="center">操作</td>
    </tr>
<%
if not (rs.bof and rs.eof) then
	do while not rs.eof
%>
    <tr class="tdbg"> 
      <td width="30" align="center"><input type="radio" value=<%=rs("ID")%><%if rs("IsSelected")=true then%> checked<%end if%> name="ID"></td>
      <td width="30" align="center"><%=rs("ID")%></td>
      <td><%=rs("Title")%></td>
      <td width="60" align="center"><%
	  response.write "<a href='Admin_Vote.asp?Action=Modify&ID=" & rs("ID") & "'>修改</a>&nbsp;&nbsp;"
      response.write "<a href='Admin_Vote.asp?Action=Del&ID=" & rs("ID") & "'>删除</a>"
	  %></td>
    </tr>
    <%
		rs.movenext
	loop
%>
    <tr class="tdbg"> 
      <td colspan=4 align=center><input name="Action" type="hidden" id="Action" value="Set"> 
        <input type="submit" value="将选定的调查设为最新调查" name="submit">
      </td>
    </tr>
<%
end if
%>
  </table>
</form>
<%
	rs.close
	set rs=nothing
end sub

sub AddVote()
%>
<form method="POST" action="Admin_Vote.asp">
  <table width="760" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
    <tr class="title"> 
      <td height="22" class="title" colspan=4 align=center><b>添 加 调 查</b></td>
    </tr>
    <tr class="tdbg"> 
      <td width="15%" align="right">主题：</td>
      <td width="85%" colspan="3"><textarea name="Title" cols="50" rows="4"></textarea></td>
    </tr>
    <%
	for i=1 to 8%>
    <tr class="tdbg"> 
      <td align="right">选项<%=i%>：</td>
      <td><input type="text" name="select<%=i%>" size="36"> 
      </td>
      <td align="right">票数：</td>
      <td width="80"><input type="text" name="answer<%=i%>" size="5"></td>
    </tr>
    <%next%>
    <tr class="tdbg"> 
      <td align="right">调查类型：</td>
      <td colspan="3"><select name="VoteType" id="VoteType">
          <option value="Single" selected>单选</option>
          <option value="Multi">多选</option>
        </select></td>
    </tr>
    <tr class="tdbg">
      <td align="right">&nbsp;</td>
      <td colspan="3"><input name="IsSelected" type="checkbox" id="IsSelected" value="True" checked>
        设为最新调查</td>
    </tr>
    <tr class="tdbg"> 
      <td colspan=4 align=center><input name="Action" type="hidden" id="Action" value="SaveAdd"> 
        <input name="Submit" type="submit" id="Submit" value=" 添 加 "> 
        &nbsp; <input  name="Reset2" type="reset" id="Reset2" value=" 清 除 "> </td>
    </tr>
  </table>
</form>
<%
end sub

sub Modify()
	dim ID
	ID=Trim(Request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定调查IP</li>"
		exit sub
	else
		ID=Clng(ID)
	end if
	sql="select * from Vote where ID="& ID
	Set rs= Server.CreateObject("ADODB.Recordset")
	rs.open sql,conn,1,1
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>找不到指定的调查！</li>"
		rs.close
		set rs=nothing
		exit sub
	end if
%>
<form method="POST" action="Admin_Vote.asp">
  <table width="760" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
    <tr class="title"> 
      <td height="22" class="title" colspan=4 align=center><b>修 改 调 查</b></td>
    </tr>
    <tr class="tdbg"> 
      <td width="15%" align="right">主题：</td>
      <td width="85%" colspan="3"><textarea name="Title" cols="50" rows="4"><%=rs("Title")%></textarea></td>
    </tr>
    <%
for i=1 to 8%>
    <tr class="tdbg"> 
      <td align="right">选项<%=i%>：</td>
      <td><input name="select<%=i%>" type="text" value="<%=rs("select"& i)%>" size="36"> 
      </td>
      <td align="right">票数：</td>
      <td width="80"><input name="answer<%=i%>" type="text" value="<%=rs("answer"&i)%>" size="5"></td>
    </tr>
    <%next%>
    <tr class="tdbg"> 
      <td align="right">调查类型：</td>
      <td colspan="3"><select name="VoteType" id="VoteType">
          <option value="Single" <% if rs("VoteType")="Single" then %> selected <% end if%>>单选</option>
          <option value="Multi" <% if rs("VoteType")="Multi" then %> selected <% end if%>>多选</option>
        </select></td>
    </tr>
    <tr class="tdbg">
      <td align="right">&nbsp;</td>
      <td colspan="3"><input name="IsSelected" type="checkbox" id="IsSelected" value="True" <% if rs("IsSelected")=true then response.write "checked"%>>
        设为最新调查</td>
    </tr>
    <tr class="tdbg"> 
      <td colspan=4 align=center><input name="Action" type="hidden" id="Action" value="SaveModify">
        <input name="ID" type="hidden" id="ID" value="<%=rs("ID")%>"> 
        <input name="Submit" type="submit" id="Submit" value="保存修改结果"> </td>
    </tr>
  </table>
</form>
<%
	rs.close
	set rs=nothing
end sub
%>
</body>
</html>
<%

sub SaveAdd()
	dim Title,VoteTime,Action,IsSelected,i
	Title=trim(request.form("Title"))
	
	VoteTime=trim(request.form("VoteTime"))
	if VoteTime="" then VoteTime=now()
	IsSelected=trim(request("IsSelected"))

	sql="select top 1 * from Vote"
	Set rs= Server.CreateObject("ADODB.Recordset")
	rs.open sql,conn,1,3
	rs.addnew
	rs("Title")=Title
	for i=1 to 8
		if trim(request("select"&i))<>"" then
			rs("select"&i)=trim(request("select"&i))
			if request("answer"&i)="" then
				rs("answer"&i)=0
			else
				rs("answer"&i)=clng(request("answer"&i))
			end if
		end if
	next
	rs("VoteTime")=VoteTime
	rs("VoteType")=request("VoteType")
	if IsSelected="" then IsSelected=false
	if IsSelected="True" then conn.execute "Update Vote set IsSelected=False where IsSelected=True"
	rs("IsSelected")=IsSelected
	rs.update
	rs.close
	set rs=nothing
	call CloseConn()
	Response.Redirect "Admin_Vote.asp"
end sub

sub SaveModify()
	dim ID,Title,VoteTime,IsSelected
	ID=Trim(Request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定调查IP</li>"
		exit sub
	else
		ID=Clng(ID)
	end if
	Title=trim(request.form("Title"))
	VoteTime=trim(request.form("VoteTime"))
	if VoteTime="" then VoteTime=now()
	IsSelected=trim(request("IsSelected"))
	if IsSelected="True" then
		conn.execute "Update Vote set IsSelected=False where IsSelected=True"
	end if
	sql="select * from Vote where ID="& ID
	Set rs= Server.CreateObject("ADODB.Recordset")
	rs.open sql,conn,1,3
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>找不到指定的调查！</li>"
		rs.close
		set rs=nothing
		exit sub
	end if
		rs("Title")=Title
		for i=1 to 8
			if trim(request("select"&i))<>"" then
				rs("select"&i)=trim(request("select"&i))
				if request("answer"&i)="" then
					rs("answer"&i)=0
				else
					rs("answer"&i)=clng(request("answer"&i))
				end if
			else
				rs("select"&i)=""
				rs("answer"&i)=0
			end if
		next
		rs("VoteTime")=VoteTime
		rs("VoteType")=request("VoteType")
		if IsSelected="" then IsSelected=false
		rs("IsSelected")=IsSelected
		rs.update
		rs.close
		set rs=nothing
		call CloseConn()
		Response.Redirect "Admin_Vote.asp"

end sub
sub SetNew()
	dim ID
	ID=Trim(Request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定调查IP</li>"
		exit sub
	else
		ID=Clng(ID)
	end if
	conn.execute "Update Vote set IsSelected=False where IsSelected=True"
	conn.execute "Update Vote set IsSelected=True Where ID=" & ID
	response.Write "<script language='JavaScript' type='text/JavaScript'>alert('设置成功！');</script>"
	call main()
end sub

sub DelVote()
	dim ID
	ID=Trim(Request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定调查IP</li>"
		exit sub
	else
		ID=Clng(ID)
	end if
	conn.Execute "delete from Vote where ID=" & ID
	Response.Redirect "Admin_Vote.asp"

end sub
%>
