<!--#include file="../common/conn.asp"-->
<%
'检查用户权限
call checkuser(",")
%>
<%
'通用分类管理模板
'参数说明：TableName--分类表，WindowName--分类管理的窗口名称，ClassLevel--分类允许的最大的层次，LinkSymbol--间隔字符
Dim TableName,WindowName,ClassLevel,LinkSymbol

TableName="class"		'所要操作的分类表
WindowName="九天后台"			'分类管理的名称
ClassLevel=1					'分类的最大级数，1表示只有一层
LinkSymbol=" ＞"				'显示分类层次时所用的连接字符

if GetQueryString("TableName")<>"" then
	TableName=GetQueryString("TableName")
end if
if GetQueryString("WindowName")<>"" then
	WindowName=GetQueryString("WindowName")
end if

if GetQueryString("ClassLevel")<>"" then
	ClassLevel=Cint(GetQueryString("ClassLevel"))
end if

if GetQueryString("LinkSymbol")<>"" then
	LinkSymbol=GetQueryString("LinkSymbol")
end if

strQueryString="&mnid=" & GetQueryString("mnid") & "&TableName=" & TableName & "&ClassLevel=" & ClassLevel & "&WindowName=" & WindowName 
%>
<%
if GetVariable("action")="dele" then
	strIDs=GetVariable("id")
	Sql="select count(id) from " & TableName & " where c_code like (select c_code from " & TableName & " where id="&strIDs&")+'_%' and id<>"&strIDs&" And mn_id=" & GetQueryString("mnid")
	Under=conn.execute(sql)(0)
	If Under>0 Then
		response.Redirect("../Error.asp?msg=对不起，栏目尚有下属版块！")
	else
		strSQL="Delete From " & TableName & " Where id in(" & strIDs & ",0)"
	conn.execute strSQL
	end if
end if

%>
<%
if GetVariable("action")="up" or GetVariable("action")="down" then
	On Error Resume Next
	if GetVariable("action")="up" then
		IsUp=1
	else
		IsUp=0
	end if
	ColumnCode=GetVariable("colcode")
	CodeLen=Len(ColumnCode)
	NBStr=String(CodeLen,"-")
	
	If ColumnCode<>"" And (Len(ColumnCode) Mod 4)=0 Then 
		If CodeLen>4 Then WStr="and left(c_code,"&CodeLen-4&")='"&Left(ColumnCode,CodeLen-4)&"'"
		If IsUp Then 
			SQL="select top 1 c_code from " & TableName & " where len(c_code)="&CodeLen&" and c_code<'"&ColumnCode&"' "&WStr& " And mn_id=" & GetQueryString("mnid")&" order by c_code desc"
			'Response.Write sql&"<br>"
			TempStr=conn.execute(sql)(0)
		Else
			SQL="select top 1 c_code from " & TableName & " where len(c_code)="&CodeLen&" and c_code>'"&ColumnCode&"' "&WStr& " And mn_id=" & GetQueryString("mnid")&" order by c_code"
			'Response.Write sql&"<br>"
			TempStr=conn.execute(sql)(0)
		End If
		
		If TempStr<>"" And (Len(TempStr) Mod 4)=0 Then 
		
			'移动上层栏目
			SQL="update " & TableName & " set c_code='"&NBStr&"'+MID(c_code,"&CodeLen+1&",len(c_code)) where left(c_code,"&CodeLen&")='"&TempStr&"'"
			'Response.Write sql&"<br>"
			conn.execute(sql)
			'更新目标栏目
			SQL="update " & TableName & " set c_code='"&TempStr&"'+MID(c_code,"&CodeLen+1&",len(c_code)) where left(c_code,"&CodeLen&")='"&ColumnCode&"'"
			'Response.Write sql&"<br>"
			conn.execute(sql)
			'更新上层栏目
			SQL="update " & TableName & " set c_code='"&ColumnCode&"'+MID(c_code,"&CodeLen+1&",len(c_code)) where left(c_code,"&CodeLen&")='"&NBStr&"'"
			'Response.Write sql&"<br>"
			conn.execute(sql)
		End If
	End If
end if
%>
<%
strParent=GetQueryString("parent")		'当然层父类ID号
if strParent="" then strParent="0"
strLocation=""
p_Parent=strParent							'父类ID，用这个变量用到控制循环
intLevel=1
strUPID="0"									
Do while p_Parent<>"0"
	strSQL="Select * From " & TableName & " Where id=" & p_Parent & " And mn_id=" & GetQueryString("mnid")			'这里要判断是哪个栏目的分类，对应menu表中的ID字段
	set rsSub=Server.CreateObject("ADODB.RecordSet")
	rsSub.open strSQL,conn,1,1
	strClassName=trim(rsSub("c_name"))
	
	if intLevel>1 then
		strLocation=LinkSymbol & "<a href=?parent=" & p_Parent & ">" & strClassName & "</a>" & strLocation
	else
		strLocation=LinkSymbol & strClassName & strLocation
		strUPID=p_Parent
	end if
	p_Parent=trim(rsSub("c_parent"))
	if intLevel=1 then strUPID=p_Parent		'记录返回上层的Parent
	intLevel=intLevel+1
loop
strLocation="<a href=?parent=0>所有分类</a>" & strLocation
if strParent="0" then 
	'如果是最高层，则没有上层了
	strGoUp="style='display:none'"
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><%=WindowName%>--分类管理</title>
<STYLE type="text/css">
td			{font-size:9pt;line-height:140%}
A:active	{COLOR: #FFFFFF;}
A:visited	{COLOR: #333333; TEXT-DECORATION: none}
A:hover		{COLOR: #000033;TEXT-DECORATION: none}
A:link		{COLOR: #333333; TEXT-DECORATION: none}
</STYLE>
</head>
<%
'从这开始读数据
strSQL="Select * From " & TableName & " Where c_parent=" & strParent  & " And mn_id=" & GetQueryString("mnid") & " Order By c_code"
rs.open strSQL,conn,1,1
'response.Write(strsql&"<br>")
'分页显示初始化
page=GetVariable("page")
if page="" then
	page=1
else
	page=Cint(page)
end if

Section=GetVariable("section")
if Section="" then
	Section=1
else
	Section=Cint(Section)
end if
'变量赋值
intPageSize=10		'每页显示记录数
intSectionSize=5		'多少页为一段，像GOOGLE一样
PrevSectionText="<FONT face=webdings>7</FONT>"		'上一段的链接文字
NextSectionText="<FONT face=webdings>8</FONT>"		'下一段的链接文字
PrevPageText="<FONT face=webdings>3</FONT>"			'上页的链接文字	
NextPageText="<FONT face=webdings>4</FONT>"			'下页的链接文字

strPageUrl="?t=" & strQueryString

rs.pagesize=intPageSize
intCount=rs.RecordCount 
intPageCount=rs.pagecount
intSectionCount=(intPageCount - 1) \ intSectionSize + 1
if intCount>0 then
	rs.AbsolutePage=page 
end if 
%>
<body leftmargin="0" topmargin="0" BodyBegin>
  <table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#336600">
    <tr> 
      <td width="100%" bgcolor="#669900"><strong><font color="#FFFFFF"><%=WindowName%>--分类管理</font></strong></td>
    </tr>
    <tr align="center" bgcolor="#F9F9F9"> 
      <td> <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr> 
            <td height="20" colspan="6">＞<strong><%=strLocation%></strong>，共 <%=intCount%> 个下级分类</td>
          </tr>
          <tr> 
            <td height="40" colspan="6"> 　 <input type="button" <%=strGoUp%> name="Submit22" value=" 返回上层 " onClick="location='?parent=<%=strUPID%><%=strQueryString%>'"> 
              <input type="button" name="Submit2" value=" 新建分类 " onClick="window.open('edit.asp?parent=<%=strParent%>&index=<%=intCount%>&TableName=<%=TableName%>&mnid=<%=GetQueryString("mnid")%>','','width=280,height=155')">
          </td>
          </tr>
          <tr bgcolor="#669900">
            
          <td align="center"><strong></strong></td>
            <td align="center"><font color="#FFFFFF"><strong>排序&nbsp;</strong></font></td>
            <td><font color="#FFFFFF"><strong>类别名称</strong></font></td>
            <td align="center"><font color="#FFFFFF"><strong>修　改</strong></font></td>
            <td align="center"><font color="#FFFFFF">&nbsp;<strong>删　除</strong></font></td>
            <td width="26%" align="center"><font color="#FFFFFF">&nbsp;</font></td>
          </tr>
          <%
		 '循环输出
		 for i=1 to intPageSize
			if rs.eof then exit for
			if i mod 2=0 then
				strColor="#eeeeee"
			else
				strColor="#f9f9f9"
			end if
		 %>
          <tr bgcolor="<%=strColor%>">
            
          <td width="8%" align="center" bgcolor="<%=strColor%>">&nbsp;</td>
            <td width="8%" align="center" bgcolor="<%=strColor%>"><a href="?action=up&colcode=<%=trim(rs("c_code"))%><%=strQueryString%>">↑</a> 
              | <a href="?action=down&colcode=<%=trim(rs("c_code"))%><%=strQueryString%>">↓</a></td>
            <td width="31%">
			<%
			Level=(Len(rs("c_code"))/4-1)*3
			If Len(rs("c_code"))>4 Then Response.Write "├"
			Response.Write String(Level,"-")
			%>
			<%=trim(rs("c_name"))%></td>
            <td width="15%" align="center">[<a href="javascript:" onClick="window.open('edit.asp?id=<%=trim(rs("id"))%><%=strQueryString%>','','width=280,height=155')">修改</a>]</td>
            <td width="12%" align="center">[<a href="?parent=<%=strParent%>&id=<%=trim(rs("id"))%>&action=dele<%=strQueryString%>">删除</a>]　</td>
            <td align="center">
			<%if intLevel<ClassLevel then%>
			<input type="button" name="Submit4" value=" 修改下级分类 " onClick="location='?parent=<%=trim(rs("id"))%><%=strQueryString%>'"></td>
			<%else%>
            <font color="#777777">[无]</font>
            <%end if%>
          </tr>
          <%
			 rs.movenext
		  next
		  %>
          <tr> 
            <td colspan="6" align="center"> <hr width="95%" style="color:#669900"></td>
          </tr>
        </table></td>
    </tr>
    <tr> 
      <td bgcolor="#669900"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td align="right"> <font color="#FFFFFF">共有<%=intCount%>记录，分<%=intPageCount%>页，当前页<%=Page%>，每页显示<%=intPageSize%>条记录 
              <%
			'计算每一段的开始页
			intStarPage=(Section-2) * intSectionSize + 1
			'前一段
			if Section<=1 then
				response.Write(PrevSectionText & " ")
			else
				response.Write("<a href='" & strPageUrl & "&page=" & intStarPage & "&section=" & Section-1 & "'>" & PrevSectionText & "</a> ")
			end if 
			
			'显示页码列表
			response.Write("第")
			intStarPage=(Section-1) * intSectionSize + 1
		    for p=intStarPage to intStarPage + intSectionSize - 1
				if p > intPageCount then exit for
				if p=page then
		    		response.Write("<strong>[" & p & "]</strong> ")
				else
		    		response.Write("<a href='" & strPageUrl & "&page=" & p & "&section=" & Section & "'>[" & p & "]</a> ")
				end if	
		    next
			response.Write("页")
			'后一段
			intStarPage=(Section) * intSectionSize + 1
			if Section>=intSectionCount then
				response.Write(" " & NextSectionText)
			else
				response.Write(" <a href='" & strPageUrl & "&page=" & intStarPage & "&section=" & Section+1 & "'>" & NextSectionText & "</a> ")
			end if 
		  %>
              <select name="page" size="1" onChange="window.location='<%=strPageUrl & "&section="%>' + (Math.floor((parseInt(this.value)-1)/<%=intSectionSize%>)+1) + '<%="&page="%>' + this.value">
                <option value="<%=page%>" selected>第<%=page%>页</option>
                <%
							for i=1 to rs.pagecount
					%>
                <option value="<%=i%>" >第<%=i%>页</option>
                <%
							next
					 %>
              </select>
              </font> 　 </td>
          </tr>
        </table></td>
    </tr>
  </table>
  <iframe name="BackFrame" width="0" height="0" src="about:blank"></iframe>
</body>
</html>
