<!--#include file="conn.asp"-->
<!--#include file="inc/cook2.asp"-->
<!--#include file="inc/dimflag.asp"-->
<!--#include file="top.asp"-->
<%
tid=request("tid")
%>
<div id="container">
<div class="top_but">
 <li class="left">&nbsp;</li>
 <li><a href="book_anclass_wx.asp?Action=Add&ParentID=0&tid=<%=tid%>"><img src="images/but_1.gif" alt="添加" ></a></li>
 <li><img src="images/but_8.gif" alt="刷新" onClick="window.location.reload(true);"></li>
 <li><img src="images/but_9.gif" alt="打印" onClick="window.print() "/></li>
</div>
<%
Select Case Action
  Case "Add"
	addFolder
  	CallFolderView()
  Case "Del"
    Dim rs,sql,SortPath
    Set rs=server.CreateObject("adodb.recordset")
    sql="Select * From com_class where ID="&request.QueryString("id")
    rs.open sql,conn,1,1
	SortPath=rs("SortPath")
	conn.execute("delete from com_class where Instr(SortPath,'"&SortPath&"')>0")
    conn.execute("delete from com_books where Instr(SortPath,'"&SortPath&"')>0")
    response.write ("<script language=javascript> alert('成功删除本类、子类及所有下属产业条目，点击确定查看类别树！');location.replace('book_anclass_wx.asp?tid="&tid&"');</script>")
  Case "Save"
	saveFolder ()
  Case "Edit"
	editFolder
  	CallFolderView()	
  Case "Move"
	moveFolderForm ()
  	CallFolderView()
  Case "MoveSave"
	saveMoveFolder ()
  Case Else
	CallFolderView()
End Select
%>
</div>

<br>
<%
'调用显示节点------------------------------
Function CallFolderView()
response.Write "<div class='tableAll titlebgcolor'><li style='width:30px;'>&nbsp;</li><li>分类管理</li></div>"
Folder(0)
End Function

'列出所有节点------------------------------
Function Folder(id)
  Dim rs,sql,i,ChildCount,FolderType,FolderName,onMouseUp,ListType,ViewFlag,LevelID,pid
  Set rs=server.CreateObject("adodb.recordset")
  sql="Select * From com_class where type="&tid&" and ParentID="&id&" order by pid,ID"
  rs.open sql,conn,1,1
  if id=0 and rs.recordcount=0 then
    response.write ("暂无分类!")
    response.end
  end if  
  i=1

  while not rs.eof
    ChildCount=conn.execute("select count(*) from com_class where type="&tid&" and ParentID="&rs("id"))(0)
    if ChildCount=0 then
	  FolderType="- "
	  FolderName=rs("classnamecn")
    else
	  FolderType="+ "
	  FolderName=rs("classnamecn")
	  ListType="sortlist"
    end if
%>
<div class="tableAll trbgcolor">
	<li style="width:30px;">&nbsp;</li>
    <li style="width:550px;"><%=FolderType%><%=FolderName%></li>
    <li style="width:110px"><a href='book_anclass_wx.asp?Action=Add&ParentID=<%=rs("id")%>&LevelID=<%=rs("LevelID")%>&tid=<%=tid%>'>添加</a> | <a href='book_anclass_wx.asp?Action=Edit&ID=<%=rs("id")%>&tid=<%=tid%>'>修改</a> | <a href="?Action=Del&id=<%=rs("id")%>&tid=<%=tid%>" onClick="return confirm('您确实要删除本类、子类及所有下属信息条目吗？')">删除</a></li>
    <li style="width:50px"><a href='book_anclass_wx.asp?Action=Move&ID=<%=rs("id")%>&tid=<%=tid%>&ParentID=<%=rs("Parentid")%>&classnamecn=<%=rs("classnamecn")%>&SortPath=<%=rs("SortPath")%>'>移</a>→<a href='#' onClick="getElementById('tombwz').innerHTML='>&nbsp;<%=rs("classnamecn")%>';getElementById('toID').value='<%=rs("id")%>';getElementById('toParentID').value='<%=rs("ParentID")%>';getElementById('toSortPath').value='<%=rs("SortPath")%>';">至</a></li>
</div> 
	
	
<%
if ChildCount>0 then
%>
<div class="<%=ListType%>"><% Folder(rs("id")) %></div>
<%
end if
rs.movenext
i=i+1
wend

rs.close
set rs=nothing
end function
%>


<%

'添加节点---------------------------------
Function addFolder()
  Dim ParentID,LevelID,pid
  ParentID=request.QueryString("ParentID")
  LevelID=request.QueryString("LevelID")
  pid=request.QueryString("pid")
  addFolderForm ParentID,LevelID,pid
end function

'添加节点表单------------------------------
Function addFolderForm(ParentID,LevelID,pid)
  Dim ParentPath,SortTextPath,rs,sql
    Set rs=server.CreateObject("adodb.recordset")
    sql="Select * From com_class where type="&tid&" and ID="&ParentID
    rs.open sql,conn,1,1
 if ParentID=0 then
    ParentPath="0,"
	SortTextPath=""
  else 
    ParentPath=rs("SortPath")
  end if
  if LevelID<>"" then LevelID=rs("LevelID")+1 else LevelID=1 end if
  if pid="" then pid=0 end if
%>
<div class="tableAll titlebgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;">根类&nbsp;>&nbsp;<% if ParentID<>0 then TextPath(ParentID)%></li>
</div>
<form name="FolderForm" method="post" action="?Action=Save&From=Add&tid=<%=tid%>">
<div style="display:none">
级别ID：<input readonly name="LevelID" type="text" class="textfield" id="LevelID" size="6" value="<%=LevelID%>">
父类ID：<input readonly name="ParentID" type="text" class="textfield" id="ParentID" size="6" value="<%=ParentID%>">
父类数字路径：<input readonly name="ParentPath" type="text" style="width:100px;" class="textfield" id="ParentPath" size="44" value="<%=ParentPath%>">
</div>	
<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:500px;">
   <%=language_cn%> <input name="classnamecn" type="text" class="textfield" id="classnamecn" size="20">&nbsp;
<%if Const_webEn =1 then%><%=language_en%> <input name="classnameen" type="text" class="textfield" id="classnameen" size="20">&nbsp;<%end if%>
<%if Const_webUtf =1 then%><%=language_utf%> <input name="classnameutf" type="text" class="textfield" id="classnameutf" size="20"><%end if%>
</li>
 <li style="width:50px;"><input name="pid" type="text" class="textfield" id="pid" size="2" value=<%=pid%>></li>
 <li><input name="submitSave" type="submit" class="button" value="添加"></li>
</div>
</form>
<%End Function%>


<%
'生成节点文字路径--------------------------
Function TextPath(ID)
  Dim rs,sql,SortTextPath
  Set rs=server.CreateObject("adodb.recordset")
  sql="Select * From com_class where ID="&ID
  rs.open sql,conn,1,1
  SortTextPath=rs("classnamecn")&"&nbsp;>&nbsp;"
  if rs("ParentID")<>0 then TextPath rs("ParentID")
  response.write(SortTextPath)
End Function


'保存添加、修改节点-------------------------
Function saveFolder
  if len(trim(request.Form("classnamecn")))=0 then
      response.write ("<script language=javascript> alert('""""类别名为必填项目！');history.back(-1);</script>")
      response.end
  end if
 
  Dim From,Action,rs,sql,SortTextPath,LevelID,pid
  From=request.QueryString("From")
  Set rs=server.CreateObject("adodb.recordset")
  
  if From="Add" then 
    sql="Select * From com_class"
    rs.open sql,conn,1,3
    rs.addnew
	Action="添加类别"
    rs("SortPath")=request.Form("ParentPath") & rs("ID") &","
	rs("type")=tid
  else
    sql="Select * From com_class where ID="&request.QueryString("ID")
    rs.open sql,conn,1,3
	Action="修改类别"
    rs("SortPath")=request.Form("SortPath")
	
  end if
  
  rs("pid")=request.Form("pid")
  rs("LevelID")=request.Form("LevelID")
  rs("classnamecn")=request.Form("classnamecn")
  rs("classnameen")=request.Form("classnameen")
  rs("classnameutf")=request.Form("classnameutf")
  rs("ParentID")=request.Form("ParentID")
  
  rs.update 
  response.write ("<script language=javascript> alert('"&Action&"保存成功，点击确定查看类别树！');location.replace('book_anclass_wx.asp?tid="&tid&"');</script>")
End Function 
'修改节点---------------------------------
Function editFolder()
  Dim ID
  ID=request.QueryString("ID")
  editFolderForm ID
end function

'修改节点表单------------------------------
Function editFolderForm(ID)
  Dim classnamecn,ViewFlagCh,classnameen,ViewFlagEn,ParentID,SortPath,rs,sql,LevelID,pid
  Set rs=server.CreateObject("adodb.recordset")
  sql="Select * From com_class where ID="&ID
  rs.open sql,conn,1,1
  classnamecn=rs("classnamecn")
  classnameen=rs("classnameen")
  classnameutf=rs("classnameutf")
  pid=rs("pid")
  LevelID=rs("LevelID")
  ParentID=rs("ParentID")
  SortPath=rs("SortPath")
%>

<div class="tableAll titlebgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;">根类&nbsp;>&nbsp;<% if ParentID<>0 then TextPath(ParentID)%></li>
</div>
<form name="FolderForm" method="post" action="book_anclass_wx.asp?Action=Save&From=Edit&ID=<%=ID%>&tid=<%=tid%>">
<div style="display:none">
级别ID：<input readonly name="LevelID" type="text" class="textfield" id="LevelID" size="6" value="<%=LevelID %>">
父类ID：<input readonly name="ParentID" type="text" class="textfield" id="ParentID" size="6" value="<%=ParentID %>">
父类数字路径：<input readonly name="SortPath" type="text" style="width:100px;" class="textfield" id="SortPath" size="44" value="<%=SortPath%>">   
</div>	

<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:500px;">
   <%=language_cn%> <input name="classnamecn" type="text" class="textfield" id="classnamecn" value="<%=classnamecn%>" size="20">&nbsp;
<%if Const_webEn =1 then%><%=language_en%> <input name="classnameen" type="text" class="textfield" id="classnameen" value="<%=classnameen%>" size="20">&nbsp;<%end if%>
<%if Const_webUtf =1 then%><%=language_utf%> <input name="classnameutf" type="text" class="textfield" id="classnameutf" value="<%=classnameutf%>" size="20"><%end if%>
</li>
 <li style="width:50px;"><input name="pid" type="text" class="textfield" id="pid" size="2" value=<%=pid%>></li>
 <li><input name="submitSave" type="submit" class="button" value="保存"></li>
</div>
</form>
<%End Function%>
<%
'转移节点表单------------------------------
Function moveFolderForm()
  Dim ID,ParentID,classnamecn,SortPath
  ID=request.QueryString("ID")
  ParentID=request.QueryString("ParentID")
  classnamecn=request.QueryString("classnamecn")
  SortPath=request.QueryString("SortPath")
%>
<div id="SortFromTo">
<form name="MoveForm" method="post" action="book_anclass_wx.asp?Action=MoveSave&tid=<%=tid%>">
<div class="tableAll titlebgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;">类别移动：通过点击分类树中类别对应的"移"可重新选择将要作移动的类别，包括本类、子类及所有下属产业条目将一起被移动。</li>
</div>

<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;">>&nbsp;<% response.write (classnamecn) %>  </li>
</div>

<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:200px;">移动类ID：<input readonly name="ID" type="text" class="textfield" id="ID" size="15" value="<%=ID%>"></li>
 <li style="width:200px;">移动类父ID：<input readonly name="ParentID" type="text" class="textfield" id="ParentID" size="15" value="<%=ParentID%>"></li>
 <li style="width:220px;">移动类数字路径：<input readonly name="SortPath" type="text" class="textfield" id="SortPath" size="15" value="<%=SortPath%>"></li>
</div>

<div class="tableAll titlebgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;">目标位置：通过点击"至"选择将要放置到的类别。</li>
</div>
<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:700px;"><span id="tombwz">>&nbsp;请选择...</span></li>
</div>
<div class="tableAll trbgcolor">
 <li style="width:30px;">&nbsp;</li>
 <li style="width:200px;">移动类ID：<input readonly name="toID" type="text" class="textfield" id="toID" size="15" value=""></li>
 <li style="width:200px;">移动类父ID：<input readonly name="toParentID" type="text" class="textfield" id="toParentID" size="15" value=""></li>
 <li style="width:280px;">移动类数字路径：<input readonly name="toSortPath" type="text" class="textfield" id="toSortPath" size="15" value="">
 <input name="submitMove" type="submit" class="button" value="转移"></li>
</div>
</form>
</div>
<br />
<%
End Function
'保存转移节点------------------------------
Function saveMoveFolder()
  Dim rs,sql,fromID,fromParentID,fromSortPath,toID,toParentID,toSortPath,fromParentSortPath
  fromID=request.Form("ID")
  fromParentID=request.Form("ParentID")
  fromSortPath=request.Form("SortPath")
  toID=request.Form("toID")
  toParentID=request.Form("toParentID")
  toSortPath=request.Form("toSortPath")

  if toID="" or toParentID="" or toSortPath="" then
    response.write ("<script language=javascript> alert('没有选择移动的目标位置，请返回选择！');history.back(-1);</script>")
    response.end
  end if
  if fromParentID=0 then
    response.write ("<script language=javascript> alert('一级分类不能被移动，请返回选择！');history.back(-1);</script>")
    response.end
  end if
  if fromSortPath=toSortPath then
    response.write ("<script language=javascript> alert('选择的移动类别和目标位置相同了，请返回重新选择！');history.back(-1);</script>")
    response.end
  end if
  if Instr(toSortPath,fromSortPath)>0 or fromParentID=toID then
    response.write ("<script language=javascript> alert('不能将类别移动到本类或下属类里，请返回重新选择！');history.back(-1);</script>")
    response.end
  end if
  Set rs=server.CreateObject("adodb.recordset")
  sql="Select * From com_class where ID="&fromParentID
  rs.open sql,conn,0,1
  fromParentSortPath=rs("SortPath")
  conn.execute("update com_class set SortPath='"&toSortPath&"'+Mid(SortPath,Len('"&fromParentSortPath&"')+1) where Instr(SortPath,'"&fromSortPath&"')>0")'更新分类数字路径
  conn.execute("update com_class set ParentID='"&toID&"' where ID="&fromID)'更新类别父类ID
  conn.execute("update com_books set SortPath='"&toSortPath&"'+Mid(SortPath,Len('"&fromParentSortPath&"')+1) where Instr(SortPath,'"&fromSortPath&"')>0")'更新分类数字路径
  response.write ("<script language=javascript> alert('移动类别成功，点击确定查看类别树！');location.replace('book_anclass_wx.asp?tid="&tid&"');</script>")
End Function

response.end
%>
