<!--#include file="../conn.asp"-->
<!--#include file="admin_check.asp"-->
<!--#include file="../inc/function.asp"-->
<!--#include file="../inc/editor/class_fckeditor.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>管理页面</title>
<link id="maincss" rel="stylesheet" href="images/css/admin_style_<%= AdminSkin %>.css" type="text/css" media="all" />
<script language="javascript">
function ChkPut(put,text,num)
{
	if(put.value.length >num)
	{
		put.value = put.value.substring(0,num);
	}
	else
	{
		text.innerText = num - put.value.length;
	}
}
function CheckAll()
{
var length = document.formed.checkbox.length;
var tocheck = document.formed.SelectAll.checked;

  if (length>=2)
  {
    for (var i=0; i<length; i++){document.formed.checkbox[i].checked = tocheck;}
   }
  else
  {
    document.formed.checkbox.checked = tocheck;    
  }
   
}
</script>
<script language="javascript" src="../inc/AjaxAans.js"></script>
</head>
<body leftmargin="0" bottommargin="0" rightmargin="0" topmargin="5" marginheight="0" marginwidth="0" bgcolor="#DDEEFF">
<%
Call chkFlag(16)
Dim act
act=Trim(Request.QueryString("act"))
opendb
Dim AskD
Set AskD = Server.CreateObject("KwAsk.FunAsk")
Select Case act
	Case "add","modi"
	  Call Add()
	Case "save"
	  Call saves()
	Case "del"
	  Call del()
	Case else
	   Call Main()
End Select
closedb
Set AskD = Nothing

Sub DEL()
   dim id
   id=Trim(Request.QueryString("id"))
   IF ID="" Then ID=Trim(Request.Form("checkbox"))
   if ID<>"" then
      conn.execute("delete from "&predb&"message where id in("&id&")")
      
	  Call SendMsg("信息删除成功")
	  Response.Write("<script>setTimeout(""location.href='?'"",2000)</script>")
   else
	  Response.Write("<script>location.href='?'</script>")
   end if
	  
End Sub

Sub Saves()
   dim title,touser,content,AddUser,rs,recU,i,id
   id=Clng(Request.QueryString("id"))
   title=Trim(Request.Form("title"))
   touser=Trim(Request.Form("touser"))
   content=Trim(Request.Form("content"))
   AddUser=Request.Cookies("adminID")
   IF not isnumeric(AddUser) Then AddUser=0
   IF title="" or content="" or touser="" then
      Call SendMsg("信息内容不能为空")
	  Response.Write("<script>setTimeout(""history.go(-1)"",2000)</script>")
	  Response.End()
   End IF
   
   IF touser<>"0" Then
      IF instr(touser,"|")=0 Then
	     Set rs=conn.execute("select id from "&predb&"user where userid='"&touser&"'")
		 IF  rs.bof and rs.eof Then
			  Call SendMsg("接收对象不存在！")
			  Response.Write("<script>setTimeout(""history.go(-1)"",2000)</script>")
			  Response.End()
		 Else
		      touser=rs("id")
		 End IF
		 Set rs=nothing
	  Else
	     recU=split(touser,"|")
		 touser=""
		 For i=0 to ubound(recU)
		    IF recU(i)<>"" Then
				 Set rs=conn.execute("select id from "&predb&"user where userid='"&recU(i)&"'")
				 IF  rs.bof and rs.eof Then
					  Call SendMsg("接收对象'"&recU(i)&"'不存在！")
					  Response.Write("<script>setTimeout(""history.go(-1)"",2000)</script>")
					  Response.End()
				 Else
					  touser=rs("id")&","&touser
				 End IF
				 Set rs=nothing
			End if
		 Next
	  End IF
   End IF
   
   IF id>0 Then
	   Conn.execute("update "&predb&"message set title='"&title&"',Mcont='"&content&"',ToUser='"&touser&"' where id="&id)
	   
	   Call SendMsg("站内信修改成功！")
	   Response.Write("<script>setTimeout(""location.href='?act=modi&id="&id&"'"",2000)</script>")
   Else
	   Conn.execute("insert into "&predb&"message(title,Mcont,AddUser,isAdmin,AddTime,ToUser,state) values('"&title&"','"&content&"',"&AddUser&",1,'"&now()&"','"&touser&"','0')")
	   
	   Call SendMsg("站内信发送成功！")
	   Response.Write("<script>setTimeout(""history.go(-1)"",2000)</script>")
   End IF
End Sub

Sub Main()
dim rs,i,c,rs1,sql,j,recU,str

Dim idcount,maxperpage,n,page,minid
maxperpage=25
page=clng(Request.QueryString("page"))
        set rs=conn.execute("select count(id) from "&predb&"message")
		idcount=rs(0) 
		set rs=nothing
		
		If idcount Mod maxperpage = 0 Then
			n = idcount \ maxperpage
		else
			n = idcount \ maxperpage + 1
		end If
		
IF page>n then page=n
IF page<1 then page=1

		if page=1 then
           set rs=conn.execute("select top "&maxperpage&" * from "&predb&"message order by id desc")
		else
			set rs1=conn.execute("select min(id) from (select top "&(page-1)*maxperpage&" id from "&predb&"message order by id desc) d")
			    minid=rs1(0)
			set rs1=nothing
			set rs=conn.execute("select top "&maxperpage&" * from "&predb&"message where id<"&minid&" order by id desc")
		end if
%>
<table class="tableBorder" cellspacing="1" cellpadding="2" width="96%" align="center" border="0">
  <tr>
	<th height="25" colspan="7" align="left">站内短信管理</th>
  </tr>
  <form method="post" action="?act=del" name="formed" onSubmit="return confirm('请确认您的操作？');">
  
  <tr>
    <td width="10%" align="center" class="TableRow1"><strong>选择</strong></td>
    <td width="28%" align="center" class="TableRow1"><strong>标题</strong></td>
    <td width="9%" align="center" class="TableRow1"><strong>发送者</strong></td>
    <td align="center" class="TableRow1"><strong>接收者</strong></td>
    <td width="15%" align="center" class="TableRow1"><strong>发布时间</strong></td>
    <td width="6%" align="center" class="TableRow1"><strong>状态</strong></td>
    <td width="12%" align="center" class="TableRow1"><strong>管理</strong></td>
  </tr>
  <%
  do while not rs.eof
i=i+1
if i mod 2=0 then
  c=1
else
  c=2
end if
  %>
  <tr>
    <td align="center" class="TableRow<%= c %>"><input name="checkbox" type="checkbox" value="<%= rs("id") %>"></td>
    <td class="TableRow<%= c %>"><%= rs("title") %></td>
    <td class="TableRow<%= c %>">
    <%
	If rs("isAdmin")=1 Then
	sql="select AdminName from "&predb&"admin where id="&rs("adduser")
    Else
	sql="select Userid from "&predb&"user where id="&rs("adduser")
	End IF
    
	set rs1=conn.execute(sql)
	Response.Write(rs1(0))
	set rs1=nothing
	%>    </td>
    <td class="TableRow<%= c %>">
    <%
	If rs("touser")=0 Then
	   Response.Write("全体")
	Else
	   IF instr(rs("touser"),",")=0 Then
			set rs1=conn.execute("select Userid from "&predb&"user where id="&rs("touser"))
			IF not(rs1.bof and rs1.eof) then
			str=rs1(0)
			end if
			set rs1=nothing
	   Else
	        recU=split(rs("touser"),",")
			For j=0 to Ubound(recU)-1
			set rs1=conn.execute("select Userid from "&predb&"user where id="&recU(j))
			if not(rs1.bof and rs1.eof) then
			str=rs1(0)&"|"&str
			end if
			set rs1=nothing
			Next
	   End IF
	End IF
	Response.Write(askD.LeftStr(str,24))
	%></td>
    <td align="center" class="TableRow<%= c %>"><%= rs("addtime") %></td>
    <td align="center" class="TableRow<%= c %>">
	<%
	If rs("state")="0" Then
	   Response.Write("<font color=blue>未读</font>")
	elseif rs("state")="1" then
	   Response.Write("已读")
	else
	   Response.Write("公共")
	end if
	%></td>
    <td align="center" class="TableRow<%= c %>"><a href="?act=modi&id=<%= rs("id") %>">修改</a>&nbsp;&nbsp; <a href="?act=del&id=<%= rs("id") %>" onClick="return confirm('请确认您的操作？');">删除</a></td>
  </tr>
  <% 
  rs.movenext
  loop
  %>
  <tr>
    <td colspan="9" align="center" class="TableRow1">共<b><%= idcount %></b>则 <font color="#FF0000"><%= page %></font>/<%= n %>页&nbsp;&nbsp; <a href="?page=1">首页</a>&nbsp;&nbsp; <a href="?page=<%= page-1 %>">上一页</a>&nbsp;&nbsp; <a href="?page=<%= page+1 %>">下一页</a>&nbsp;&nbsp; <a href="?page=<%= n %>">尾页</a></td>
  </tr>
  <tr>
    <td colspan="7" class="TableRow1"><label><input type="checkbox" name="SelectAll" id="SelectAll" onClick="CheckAll()">全选\全不选</label>
    &nbsp; <input type="submit" name="button" id="button" value="删除选中的信息">
    &nbsp;
      <input name="b" type="button" id="b" onClick="location.href='?act=add'" value="发送站内信">
      </td>
  </tr>
  </form>
</table>
<%
set rs=nothing
End Sub

Sub Add()
dim id,rs,sql,i,title,Mcont,AddUser,IsAdmin,ToUser,states,recU
id=clng(Request.QueryString("id"))
IF id>0 then
   set rs=conn.execute("select * from "&predb&"message where id="&id)
       title=rs("title")
	   Mcont=rs("Mcont")
	   AddUser=rs("AddUser")
	   IsAdmin=rs("IsAdmin")
	   ToUser=rs("ToUser")
	   states=rs("state")
   set rs=nothing
   
	If IsAdmin=1 Then
	   sql="select AdminName from "&predb&"admin where id="&adduser
    Else
	   sql="select Userid from "&predb&"user where id="&adduser
	End IF
    
	set rs=conn.execute(sql)
	    AddUser=rs(0)
	set rs=nothing
	
	If touser<>0 Then
	   IF instr(touser,",")=0 Then
			set rs=conn.execute("select Userid from "&predb&"user where id="&touser)
			if not(rs.bof and rs.eof) then
			touser=rs(0)
			end if
			set rs=nothing
	   Else
	        recU=split(touser,",")
			touser=""
			For i=0 to Ubound(recU)-1
			set rs=conn.execute("select Userid from "&predb&"user where id="&recU(i))
			if not(rs.bof and rs.eof) then
			touser=rs(0)&"|"&touser
			end if
			set rs=nothing
			Next
	   End IF
	End IF
End if
%>
<table class="tableBorder" cellspacing="1" cellpadding="2" width="96%" align="center" border="0">
  <tr>
	<th height="25" colspan="2" align="left">站内短信管理</th>
  </tr>
  <form method="post" action="?act=save&id=<%= id %>" name="formed">
  
  <tr>
    <td width="21%" align="right" class="TableRow1">信息标题：</td>
    <td width="79%" class="TableRow1"><input name="title" type="text" id="title" size="50" maxlength="50" value="<%= title %>">
    <span class="xing">*</span>50字符以内</td>
  </tr>
  <tr>
    <td align="right" class="TableRow2">接收对象：</td>
    <td class="TableRow2"><input name="touser" type="text" id="touser" <% If touser="" Then Response.Write(" value="""&Trim(Request.QueryString("user"))&"""") Else Response.Write(" value="""&touser&"""")%> size="50">
      <br>
    <span class="xing">*</span>如果发送给所有人请填：<font color="#FF0000"><strong>0</strong></font>，否则请填写用户名，多个用户用“|”隔开</td>
  </tr>
  <tr>
    <td align="right" class="TableRow1">信息内容：</td>
    <td class="TableRow1" id="input0" style="position:relative">
    <textarea name="content" onFocus="getmAjax('M',this.value)" onKeyDown="ChkPut(this,nr,250)" onKeyUp="ChkPut(this,nr,250)" id="content" cols="68" rows="12"><%= Mcont %></textarea>
      <span class="xing">*</span>可以输入<span id="nr">250</span>字符      </td>
  </tr>
<% If  id>0  Then %>
  <tr>
    <td align="right" class="TableRow2">发送者：</td>
    <td class="TableRow2"><input name="touser2" type="text" id="touser2" value="<%= adduser %>" size="14" readonly></td>
  </tr>
  <tr>
    <td align="right" class="TableRow1">阅读状态：</td>
    <td class="TableRow1"><input name="touser3" type="text" id="touser3" value="<%= states %>" size="50" readonly> 0为未读；1为已读；其它为公共信息已读用户。</td>
  </tr>
<% End If %>
  <tr>
    <td align="right" class="TableRow2">&nbsp;</td>
    <td class="TableRow2"><input type="submit" name="button" id="button" value="确认发送">
      &nbsp; <input type="button" name="button2" id="button2" value="返回管理" onClick="location.href='?'">
      <% If Trim(Request.QueryString("user"))<>"" Then %>&nbsp; <input type="button" name="button2" id="button2" value="返回用户管理" onClick="location.href='admin_user.asp'"><% End If %>
      </td>
  </tr>
  </form>
</table>
<%
End Sub
%>
<br>
</body>
</html>
