<!--#include file="conn.asp"-->
<!--#include file="check.asp"-->
<%

dim Action,FoundErr,ErrMsg 
Action=trim(request("Action")) 
if Action="UpOrder" then 
	call UpOrder() 
elseif Action="DownOrder" then 
	call DownOrder() 
end if 
if FoundErr=True then 
	WriteErrMsg() 
else 
	call main() 
end if 
connclose 

function WriteErrMsg()
	response.Write(ErrMsg)
end function

sub UpOrder()
	dim ID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
	ID=trim(request("ID"))
	cRootID=Trim(request("cRootID"))
	MoveNum=trim(request("MoveNum"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
	else
		ID=CLng(ID)
	end if
	if cRootID="" then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>错误参数！</li>"
	else
		cRootID=Cint(cRootID)
	end if
	if MoveNum="" then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>错误参数！</li>"
	else
		MoveNum=Cint(MoveNum)
		if MoveNum=0 then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>请选择要提升的数字！</li>"
		end if
	end if
	if FoundErr=True then
		exit sub
	end if

	'得到本栏目的PrevID,NextID
	set rs=conn.execute("select PrevID,NextID from classname where ID=" & ID)
	PrevID=rs(0)
	NextID=rs(1)
	rs.close
	set rs=nothing
	'先修改上一栏目的NextID和下一栏目的PrevID
	if PrevID>0 then
		conn.execute "update classname set NextID=" & NextID & " where ID=" & PrevID
	end if
	if NextID>0 then
		conn.execute "update classname set PrevID=" & PrevID & " where ID=" & NextID
	end if

	dim mrs,MaxRootID
	set mrs=conn.execute("select max(RootID) From classname")
	MaxRootID=mrs(0)+1
	'先将当前栏目移至最后，包括子栏目
	conn.execute("update classname set RootID=" & MaxRootID & " where RootID=" & cRootID)
	
	'然后将位于当前栏目以上的栏目的RootID依次加一，范围为要提升的数字
	sqlOrder="select * From classname where  RootID<" & cRootID & " order by RootID desc"
	set rsOrder=server.CreateObject("adodb.recordset")
	rsOrder.open sqlOrder,conn,1,3
	if rsOrder.bof and rsOrder.eof then
		exit sub        '如果当前栏目已经在最上面，则无需移动
	end if
	i=1
	do while not rsOrder.eof
		tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子栏目
		conn.execute("update classname set RootID=RootID+1 where RootID=" & tRootID)
		i=i+1
		if i>MoveNum then
			rsOrder("PrevID")=ID
			rsOrder.update
			conn.execute("update classname set NextID=" & rsOrder("ID") & " where ID=" & ID)
			exit do
		end if
		rsOrder.movenext
	loop
	rsOrder.movenext
	if rsOrder.eof then
		conn.execute("update classname set PrevID=0 where ID=" & ID)
	else
		rsOrder("NextID")=ID
		rsOrder.update
		conn.execute("update classname set PrevID=" & rsOrder("ID") & " where ID=" & ID)
	end if	
	rsOrder.close
	set rsOrder=nothing
	
	'然后再将当前栏目从最后移到相应位置，包括子栏目
	conn.execute("update classname set RootID=" & tRootID & " where RootID=" & MaxRootID)
end sub

sub DownOrder()
	dim ID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
	ID=trim(request("ID"))
	cRootID=Trim(request("cRootID"))
	MoveNum=trim(request("MoveNum"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
	else
		ID=CLng(ID)
	end if
	if cRootID="" then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>错误参数！</li>"
	else
		cRootID=Cint(cRootID)
	end if
	if MoveNum="" then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>错误参数！</li>"
	else
		MoveNum=Cint(MoveNum)
		if MoveNum=0 then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>请选择要提升的数字！</li>"
		end if
	end if
	if FoundErr=True then
		exit sub
	end if

	'得到本栏目的PrevID,NextID
	set rs=conn.execute("select PrevID,NextID from classname where ID=" & ID)
	PrevID=rs(0)
	NextID=rs(1)
	rs.close
	set rs=nothing
	'先修改上一栏目的NextID和下一栏目的PrevID
	if PrevID>0 then
		conn.execute "update classname set NextID=" & NextID & " where ID=" & PrevID
	end if
	if NextID>0 then
		conn.execute "update classname set PrevID=" & PrevID & " where ID=" & NextID
	end if

	dim mrs,MaxRootID
	set mrs=conn.execute("select max(rootid) From classname")
	MaxRootID=mrs(0)+1
	'先将当前栏目移至最后，包括子栏目
	conn.execute("update classname set RootID=" & MaxRootID & " where RootID=" & cRootID)
	
	'然后将位于当前栏目以下的栏目的RootID依次减一，范围为要下降的数字
	sqlOrder="select * From classname where  RootID>" & cRootID & " order by RootID"
	set rsOrder=server.CreateObject("adodb.recordset")
	rsOrder.open sqlOrder,conn,1,3
	if rsOrder.bof and rsOrder.eof then
		exit sub        '如果当前栏目已经在最下面，则无需移动
	end if
	i=1
	do while not rsOrder.eof
		tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子栏目
		conn.execute("update classname set RootID=RootID-1 where RootID=" & tRootID)
		i=i+1
		if i>MoveNum then
			rsOrder("NextID")=ID
			rsOrder.update
			conn.execute("update classname set PrevID=" & rsOrder("ID") & " where ID=" & ID)
			exit do
		end if
		rsOrder.movenext
	loop
	rsOrder.movenext
	if rsOrder.eof then
		conn.execute("update classname set NextID=0 where ID=" & ID)
	else
		rsOrder("PrevID")=ID
		rsOrder.update
		conn.execute("update classname set NextID=" & rsOrder("ID") & " where ID=" & ID)
	end if	
	rsOrder.close
	set rsOrder=nothing
	
	'然后再将当前栏目从最后移到相应位置，包括子栏目
	conn.execute("update classname set RootID=" & tRootID & " where RootID=" & MaxRootID)
end sub

sub main() 
	dim sqlClass,rsClass,i,iCount,j 
	sqlClass="select * from classname order by RootID" 
	set rsClass=server.CreateObject("adodb.recordset") 
	rsClass.open sqlClass,conn,1,1 
	iCount=rsClass.recordcount 
%> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<link href="images/bbs.css" rel="stylesheet" type="text/css">
<script language=javascript> 
function xxg()
{
  if (form3.ClassName.value=="")
   {
     alert("请写上分类的名字！！！");
	 form3.ClassName.focus();
	 return false;
	 }
  return true
  }
</script>
</head>

<body topmargin="0">
<table width="715" height="166" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td height="32" colspan="2">
      <p align="center"><strong>一级栏目分类管理</strong></p>
    </td>
  </tr>
  <tr> 
    <td width="4%" height="111" rowspan="2">　</td>
    <td width="96%" height="46"> <form name="form3" method="post" action="admin_class.asp?style=add" onsubmit="return xxg()">
        <table width="100%" height="35" border="0" cellpadding="0" cellspacing="0">
          <tr> 
            <td width="14%" height="15">&nbsp;</td>
            <td width="60%" align="center">　</td>
            <td width="26%">　</td>
          </tr>
          <tr> 
            <td>填加分类</td>
            <td colspan="2"> 
            <input name="ClassName" type="text" id="classname" style="font-family:宋体; font-size:9pt; color:#000000; width:120px; height:19px; border:1px solid #000000; background-color:#F0F0F0;" size="20">
              &nbsp;&nbsp;链接（选填）
              <input name="url" type="text" id="url" style="font-family:宋体; font-size:9pt; color:#000000; width:80px; height:19px; border:1px solid #000000; background-color:#F0F0F0;" size="20">
              <select name="classtype">
                <option value="1" selected>摄影图库 </option>
                <option value="2">文章列表类 </option>
                <option value="3">单页说明类 </option>
                <option value="4">链接类 </option>
                <option value="5">人物介绍</option>
              </select>
              <input type="submit" name="Submit3" value="填加" style="font-family:宋体; font-size:9pt; color:#000000; width:30px; height:19px; border:1px solid #000000; background-color:#F0F0F0;">
            </td>
          </tr>
        </table>
      </form></td>
  </tr>
  <tr> 
    <td height="36"> 
      <hr>
      <form name="form1" method="post" action="admin_class.asp?style=modify">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td width="19%">更改分类</td>
            <td width="81%"><select name="ClassID" id="select2">
                <% sql1="select * from classname"
		        set rs1=conn.execute(sql1)
				do while not rs1.eof  %>
                <option value=<%=rs1("ID")%>><%=rs1("classname")%></option>
                <% i=i+1
				    rs1.movenext
					loop  %>
              </select> 改名为 
              <input name="ClassName" type="text" id="ClassName" size="20" style="font-family:宋体; font-size:9pt; color:#000000; width:100px; height:19px; border:1px solid #000000; background-color:#F0F0F0;">
              &nbsp;&nbsp;&nbsp;&nbsp;链接（选填）
              <input name="url" type="text" id="url" style="font-family:宋体; font-size:9pt; color:#000000; width:80px; height:19px; border:1px solid #000000; background-color:#F0F0F0;" size="20"> 
              <select name="classtype">
                <option value="1" selected>摄影图库类 </option>
                <option value="2">文章列表类 </option>
                <option value="3">单页说明类 </option>
                <option value="4">链接类 </option>
                <option value="5">人物介绍</option>
              </select>
              <input type="submit" name="Submit" value="修改" style="font-family:宋体; font-size:9pt; color:#000000; width:30px; height:19px; border:1px solid #000000; background-color:#F0F0F0;">
            </td>
          </tr>
        </table>
      </form></td>
  </tr>
  <tr> 
    <td>　</td>
    <td><form name=ff id="ff" method="post" action="admin_class.asp?style=del">
        <hr>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td width="19%">删除分类</td>
            <td width="81%"> <select name="ClassID" id="select3">
                <% sql1="select * from classname"
		        set rs1=conn.execute(sql1)
				do while not rs1.eof %>
                <option value=<%=rs1("ID")%>><%=rs1("classname")%></option>
                <%i=i+1
				rs1.movenext
				loop
				rs1.close
				set rs1=nothing%>
              </select> &nbsp; <input type="submit" name="Submit2" value="删除" style="font-family:宋体; font-size:9pt; color:#000000; width:30px; height:19px; border:1px solid #000000; background-color:#F0F0F0;"></td>
          </tr>
        </table>
      </form></td>
  </tr>
</table>
<table width="706" border="0" align="center" cellpadding="0" cellspacing="1" class="border">
  <tr class="title">  
    <td height="22" colspan="4" align="center">
      <hr>
      <strong>分类重新排序</strong></td> 
  </tr> 
  <% 
j=1 
do while not rsClass.eof 
%> 
    <tr>  
      <td width="200"><%=rsClass("classname")%> | [<%if rsClass("classtype")=1 then Response.write "摄影图库"%><%if rsClass("classtype")=2 then Response.write "文章列表类"%><%if rsClass("classtype")=3 then Response.write "单页说明类"%><%if rsClass("classtype")=4 then Response.write "其它链接类"%><%if rsClass("classtype")=5 then Response.write "人物介绍"%>]</td> 
      <td width="310">
	  	<table border="0" cellpadding="0" cellspacing="1">
			<tr><% 
			if j>1 then 
				response.write "<form action='?Action=UpOrder' method='post'><td width='150'>" 
				response.write "<select name=MoveNum size=1><option value=0>向上移动</option>" 
				for i=1 to j-1 
					response.write "<option value="&i&">"&i&"</option>" 
				next 
				response.write "</select>" 
				response.write "<input type=hidden name=ID value="&rsClass("ID")&">"
				response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
				response.write "</td></form>" 
			else 
				response.write "<td width='150'>&nbsp;</td>" 
			end if 
			if iCount>j then 
				response.write "<form action='?Action=DownOrder' method='post'><td width='150'>" 
				response.write "<select name=MoveNum size=1><option value=0>向下移动</option>" 
				for i=1 to iCount-j 
					response.write "<option value="&i&">"&i&"</option>" 
				next 
				response.write "</select>" 
				response.write "<input type=hidden name=ID value="&rsClass("ID")&">"
				response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
				response.write "</td></form>" 
			else 
				response.write "<td width='150'>&nbsp;</td>" 
			end if 
		%> </tr>
		</table>
	  </td>
	</tr> 
  <% 
	j=j+1 
	rsClass.movenext 
loop 
%> 
</table> 
</body>
</html>
<% 
	rsClass.close 
	set rsClass=nothing 
end sub 
%>