<!--#include file="conn.asp"-->
<!--#include file="user_chkpass.asp"-->
<!--#include file="inc/usercode.asp"-->
<!--#include file="inc/function.asp"-->
<SCRIPT language=javascript>
function del_space(s)
{
	for(i=0;i<s.length;++i)
	{
	 if(s.charAt(i)!=" ")
		break;
	}

	for(j=s.length-1;j>=0;--j)
	{
	 if(s.charAt(j)!=" ")
		break;
	}

	return s.substring(i,++j);
}


function VerifySubmit()
{
	topic = del_space(document.form2.topic.value);
     if (topic.length == 0)
     {
        alert("您忘了填写题目!");
	return false;
     }
		 
	submits(); 
	if (document.form2.edit.value == "")
     {
        alert("请输入内容!");
	return false;
     }	
	return true;
}
</SCRIPT>
<%
if CheckUserLogined()=False then
	response.Redirect "user_login.asp"
end if
const MaxPerPage=20
dim strFileName
dim totalPut,CurrentPage,TotalPages
dim rs, sql
dim ID,UserSearch,Keyword,strField
dim Action,FoundErr,ErrMsg
dim tmpDays,usersql
dim mainuser
keyword=trim(request("keyword"))
if keyword<>"" then 
	keyword=ReplaceBadChar(keyword)
end if
strField=trim(request("Field"))
UserSearch=trim(request("UserSearch"))
Action=trim(request("Action"))
ID=trim(Request("ID"))
mainuser=trim(Request("mainuser"))
ComeUrl=Request.ServerVariables("HTTP_REFERER")

if UserSearch="" then
	UserSearch=0
else
	UserSearch=Clng(UserSearch)
end if
strFileName="user_message.asp?UserSearch=" & UserSearch
if request("page")<>"" then
    currentPage=cint(request("page"))
else
	currentPage=1
end if

%>
<html>
<head>
<title>用户留言管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="Admin_STYLE.CSS" rel="stylesheet" type="text/css">
<SCRIPT language=javascript>
function unselectall()
{
    if(document.myform.chkAll.checked){
	document.myform.chkAll.checked = document.myform.chkAll.checked&0;
    } 	
}

function CheckAll(form)
{
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.Name != "chkAll")
       e.checked = form.chkAll.checked;
    }
}
</SCRIPT>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body leftmargin="2" topmargin="0" marginwidth="0" marginheight="0" class="bgcolor">
<br>
<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
  <tr class="topbg"> 
    <td height="22" colspan=2 align=center><strong>留 言 板 管 理</strong></td>
  </tr>
  <form name="form1" action="user_message.asp" method="get">
    <tr class="tdbg"> 
      <td width="100" height="30"><strong>快速查找留言：</strong></td>
      <td width="687" height="30"><select size=1 name="UserSearch" onChange="javascript:submit()">
          <option value="0">列出所有留言</option>
       	  <%if userlevel=9 then%><option value="1">我的留言</option><%end if%>
          <option value="10" selected> </option>
         
        </select>
        &nbsp;&nbsp;&nbsp;&nbsp;<a href="user_message.asp">留言管理首页</a>&nbsp;</td>
    </tr>
  </form>
</table>
<br>
<%
if Action="Modify" then
	call Modify()
elseif Action="SaveModify" then
	call SaveModify()
elseif Action="Del" then
	call Delmessage()
else
	call main()
end if
if FoundErr=True then

	call WriteErrMsg()
end if
call CloseConn()  

sub main()
	dim strGuide
	strGuide="<table width='98%' align='center'><tr><td align='left'>您现在的位置：<a href='User_message.asp'>用户留言管理</a>&nbsp;&gt;&gt;&nbsp;"
	select case UserSearch
		case 0
			if userlevel=9 then
				sql="select * from [message] order by ID desc"
			else
				sql="select * from [message] where mainuser='"&username&"' order by ID desc"
			end if
			strGuide=strGuide & "所有留言"
		case 1
			sql="select * from [message] where mainuser='"&username&"' order by id desc"
			strGuide=strGuide & "我文章里的留言"
		
		case 10
			if Keyword="" then
				if userlevel=9 then 
					sql="select * from [message] order by id desc"
				else
					sql="select * from [message] where mainuser='"&username&"' order by id desc"
				end if
				strGuide=strGuide & "所有留言"
			else
				select case strField
				case "ID"
					if IsNumeric(Keyword)=False then
						FoundErr=True
						ErrMsg=ErrMsg & "<br><li>ID必须是整数！</li>"
					else
						if userlevel=9 then 
							sql="select * from [message] where ID =" & Clng(Keyword)&" and mainuser='"&username&"'"
						else
							sql="select * from [message] where ID =" & Clng(Keyword)
						end if
						strGuide=strGuide & "留言ID等于<font color=red> " & Clng(Keyword) & " </font>的用户"
					end if
				case "topic"
					if userlevel=9 then
						sql="select * from [message] where messagetopic like '%" & Keyword & "%' order by ID  desc"
					else
						sql="select * from [message] where messagetopic like '%" & Keyword & "%' and mainuser='"&username&"' order by ID  desc"
					end if
					strGuide=strGuide & "标题中含有“ <font color=red>" & Keyword & "</font> ”的留言"
				end select
			end if
		case else
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>错误的参数！</li>"&sql
	end select
	strGuide=strGuide & "</td><td align='right'>"
	if FoundErr=True then exit sub
	
	Set rs=Server.CreateObject("Adodb.RecordSet")
	'response.Write userlevel
	'response.Write(sql)
	rs.Open sql,Conn,1,1
  	if rs.eof and rs.bof then
		strGuide=strGuide & "共找到 <font color=red>0</font> 个留言</td></tr></table>"
		response.write strGuide
	else
    	totalPut=rs.recordcount
		strGuide=strGuide & "共找到 <font color=red>" & totalPut & "</font> 个留言</td></tr></table>"
		response.write strGuide
		if currentpage<1 then
       		currentpage=1
    	end if
    	if (currentpage-1)*MaxPerPage>totalput then
	   		if (totalPut mod MaxPerPage)=0 then
	     		currentpage= totalPut \ MaxPerPage
		  	else
		      	currentpage= totalPut \ MaxPerPage + 1
	   		end if

    	end if
	    if currentPage=1 then
        	showContent
        	showpage strFileName,totalput,MaxPerPage,true,true,"个留言"
   	 	else
   	     	if (currentPage-1)*MaxPerPage<totalPut then
         	   	rs.move  (currentPage-1)*MaxPerPage
         		dim bookmark
           		bookmark=rs.bookmark
            	showContent
            	showpage strFileName,totalput,MaxPerPage,true,true,"个留言"
        	else
	        	currentPage=1
           		showContent
           		showpage strFileName,totalput,MaxPerPage,true,true,"个留言"
	    	end if
		end if
	end if
	rs.Close
	set rs=Nothing
	call ShowSearch()
end sub

sub showContent()
   	dim i
    i=0
%>
<table width='100%' border="0" cellpadding="0" cellspacing="0">
  <tr>
  <form name="myform" method="Post" action="user_message.asp" onsubmit="return confirm('确定要执行选定的操作吗？');">
     <td>
<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="border">
          <tr class="title"> 
            <td width="34" align="center"><font color="#FFFFFF">选中</font></td>
            <td width="34" align="center"><font color="#FFFFFF">ID</font></td>
            <td width="138" height="22" align="center"><span class="style1">发表人</span> </td>
            <td width="320" height="22" align="center"><font color="#FFFFFF">留言标题</font></td>
            <td width="154" align="center"><font color="#FFFFFF">发表时间</font></td>
            <td width="87" height="22" align="center"><font color="#FFFFFF"> 
              操作</font></td>
          </tr>
          <%do while not rs.EOF %>
          <tr class="tdbg" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor='#BFDFFF'"> 
            <td width="34" align="center"><input name='ID' type='checkbox' onclick="unselectall()" id="ID" value='<%=cstr(rs("ID"))%>'>
            <input name='mainuser' type="hidden" id="mainuser" value='<%=cstr(rs("mainuser"))%>'></td>
            <td width="34" align="center"><%=rs("ID")%></td>
            <td width="138" align="center" <%if userlevel=9 then%>title="IP:<%=rs("addip")%>"<%end if%>><%=rs("message_user")%>
		    </td>
            <td align="center"> <a href=message.asp?name=<%=rs("mainuser")%> target=_blank><%=htmlencode(rs("messagetopic"))%>  </a> </td>
            <td align="center"> <%
	if rs("addtime")<>"" then
		response.write rs("addtime")
	else
		response.write "&nbsp;"
	end if
	%> </td>
            <td width="100" align="center"><%
		response.write "<a href='User_message.asp?Action=Modify&ID=" & rs("ID") & "&re=true'>回复</a>&nbsp;"
		response.write "<a href='User_message.asp?Action=Modify&ID=" & rs("ID") & "'>修改</a>&nbsp;"
		response.write "<a href='User_message.asp?Action=Del&ID=" & rs("ID") & "&mainuser="&rs("mainuser")&"' onClick='return confirm(""确定要删除此留言吗？"");'>删除</a>"
		%> </td>
          </tr>
          <%
	i=i+1
	if i>=MaxPerPage then exit do
	rs.movenext
loop
%>
        </table>  
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="200" height="30"><input name="chkAll" type="checkbox" id="chkAll" onclick=CheckAll(this.form) value="checkbox">
              选中本页显示的所有留言</td>
            <td> <strong>操作：</strong> 
              <input name="Action" type="radio" value="Del" checked onClick="document.myform.UserLevel.disabled=true">删除&nbsp;&nbsp;&nbsp;&nbsp;
                   <input type="submit" name="Submit" value=" 执 行 "></td>
  </tr>
</table>
</td>
</form></tr></table>
<%
end sub

sub ShowSearch()
%>
<form name="form2" method="post" action="user_message.asp">
<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
  <tr class="tdbg">
    <td width="120"><strong>留言高级查询：</strong></td>
    <td width="300">
      <select name="Field" id="Field">
        <option value="ID">留言ID</option>
        <option value="topic" selected>留言标题</option>
      </select>
      <input name="Keyword" type="text" id="Keyword" size="20" maxlength="30">
      <input type="submit" name="Submit2" value=" 查 询 ">
      <input name="UserSearch" type="hidden" id="UserSearch" value="10">
	</td>
    <td>若为空，则查询所有留言</td>
  </tr>
</table>
</form>
<%
end sub

sub Modify()
	dim ID
	dim rsblog,sqlblog
	dim restr
	ID=trim(request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
		exit sub
	else
		ID=Clng(ID)
	end if
	Set rsblog=Server.CreateObject("Adodb.RecordSet")
	if userlevel=9 then
		sqlblog="select * from [message] where ID=" & ID
	else
		sqlblog="select * from [message] where ID=" & ID&" and mainuser='"&username&"'"
	end if
	rsblog.Open sqlblog,Conn,1,1
	if rsblog.bof and rsblog.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>找不到指定的留言！</li>"
		rsUser.close
		set rsUser=nothing
		exit sub
	end if
	if request("re")="true" then
		restr="<table align=center bgcolor=#f3f3f3 border=1 bordercolor=#cccccc cellPadding=2 cellSpacing=0 style='BORDER-COLLAPSE: collapse' width='90%'>"
		restr=restr&"<tbody><tr><td><p><strong>以下为blog主人的回复：</strong></p><p>&nbsp;</p></td></tr></tbody></table>"
	end if
%>
<FORM action="user_message.asp?action=SaveModify" method="post" name="form2" onSubmit="return VerifySubmit()">
  <table width="98%" border="0" align=center cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="border">
    <tr align="center"> 
    <td height=25 colspan=5 class="topbg"><strong>修　改  留 言</strong> 
  <tr> 
    <td width="11%"  class="tdbg" height=23>留言标题：</td>
    <td width="89%" colspan="4" class="tdbg"><input name="topic" type=text class="cont" id="topic" value="<%=rsblog("messagetopic")%>" size="50" maxlength="30"> 
      <font color="#FF0000"> *</font></td>
  </tr>
  <tr> 
    <td height=23 colspan="5" class="tdbg"><INPUT type="hidden" name="edit" id="edit" value="<%
				if rsblog("message")<>"" then
					response.Write Server.HtmlEncode(rsblog("message"))+restr
				else
					response.Write "   "
				end if
				%>""> 
<!--#include file="edit.asp"-->      &nbsp;</td>
  </tr>
  <tr> 
    <td height=25 colspan="5" class="tdbg"> <INPUT type="hidden" name="id" value="<%=rsblog("id")%>">
        <input type="submit" name="Submit2" value="提交修改">
        　　　　 
        <input type="reset" name="Submit" value="清除重写">     </td>
  </tr>
</table> 
</form>
<%
	rsblog.close
	set rsblog=nothing
end sub




sub SaveModify()
	dim id,rsblogchk
	id=clng(trim(request("id")))
	if CheckUserLogined()=False then
		response.Write "user_login.asp"
	end if
	'call chkenaddlog()
	if userlevel=9 then
		sql="select * from message where id="&id
	else
		sql="select * from message where id="&id&" and mainuser='"&username&"'"
	end if	
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,3
	rs("messagetopic")=trim(request("topic"))
	rs("message")=request("edit")
	rs.update
	rs.close
	set rs=nothing
	Response.Write"<script language=JavaScript>"
	Response.Write"alert(""修改留言成功！"");"
	Response.Write"window.location='user_message.asp'"
	Response.Write"</script>"
	'response.redirect "user_message.asp"
end sub


sub Delmessage()
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定要删除的留言</li>"
		exit sub
	end if
	if instr(ID,",")>0 then
		ID=replace(ID," ","")
		if userlevel=9 then
			sql="delete from [message] where ID  in (" & ID & ")"
		else
			sql="delete from [message] where ID  in (" & ID & ")"&" and mainuser='"&username&"'"
		end if
		dim n,i,u '留言数-１
		n=split(id,",")
		u=split(mainuser,",")
		for i=0 to ubound(n)
			conn.execute("update [user] set messagecount=messagecount-1 where username='"&trim(u(i))&"'")
		next		
	else
		if userlevel=9 then
			sql="delete from [message] where ID=" & Clng(ID)
		else
			sql="delete from [message] where ID=" & Clng(ID)&" and mainuser='"&username&"'"
		end if
		conn.execute("update [user] set messagecount=messagecount-1 where username='"&mainuser&"'")
	end if
	Conn.Execute sql
	call CloseConn()      
	response.redirect ComeUrl
end sub


sub WriteErrMsg()
	dim strErr
	strErr=strErr & "<html><head><title>错误信息</title><meta http-equiv='Content-Type' content='text/html; charset=gb2312'>" & vbcrlf
	strErr=strErr & "<link href='style.css' rel='stylesheet' type='text/css'></head><body><br><br>" & vbcrlf
	strErr=strErr & "<table cellpadding=2 cellspacing=1 border=0 width=400 class='border' align=center>" & vbcrlf
	strErr=strErr & "  <tr align='center' class='title'><td height='22'><strong>错误信息</strong></td></tr>" & vbcrlf
	strErr=strErr & "  <tr class='tdbg'><td height='100' valign='top'><b>产生错误的可能原因：</b>" & errmsg &"</td></tr>" & vbcrlf
	strErr=strErr & "  <tr align='center' class='tdbg'><td><a href='javascript:history.go(-1)'>&lt;&lt; 返回上一页</a></td></tr>" & vbcrlf
	strErr=strErr & "</table>" & vbcrlf
	strErr=strErr & "</body></html>" & vbcrlf
	response.write strErr
end sub

%>