<%
dim strNodeName,iLevelMain,iChildrenMain,strChannel
dim iNewPID,iPID,iPos,iStatus

function genClassOption(pid)
	dim iRootID,myClassOption

	sql = "SELECT id,className FROM cm_newsClass WHERE pid=0"
	rs.Open sql,conn,1,1
	if not rs.Eof then
		iRootID = rs("id")
		myClassOption = myClassOption &"              <option value="""& rs("id") &""""
		if rs("id") = pid then
			myClassOption = myClassOption &" selected"
        end if
        myClassOption = myClassOption &">"
        myClassOption = myClassOption & rs("className") &"</option>"& vbCrLf
	end if
	rs.Close

	if iRootID > 0 then
		sql = "SELECT id,className FROM cm_newsClass WHERE pid="& iRootID &" AND id <>"& id &" ORDER BY pos ASC"
		rs.Open sql,conn,1,1
		do while not rs.Eof
			myClassOption = myClassOption &"              <option value="""& rs("id") &""""
			if rs("id") = pid then
			  myClassOption = myClassOption &" selected"
			end if
			myClassOption = myClassOption &">"
			myClassOption = myClassOption &"　　"& rs("className") &"</option>"& vbCrLf
			rs.MoveNext
		loop
		rs.Close
	end if

	genClassOption = myClassOption
end function

function nodeInfo()
	dim strClassOption

	iPos = "999"
	strChannel = "N"
	if subaction <> "addSon" then
		sql = "select pid,pos,className,channel,status from cm_newsClass where id="& id
		rs.Open sql,conn,1,1
		if not rs.Eof then
			iPID = rs("pid")
			iPos = rs("pos")
			strNodeName = rs("className")
			strChannel = rs("channel")
			iStatus = rs("status")
		end if
		rs.Close
	end if

	iLevelMain = levelNum(id,0)
	strClassOption = genClassOption(iPID)
%>
      <table width="450" border="1" cellspacing="0" cellpadding="0" bordercolorlight="#CCE0CF" bordercolordark="#FFFFFF" align="center">
        <form name="InfoEdit" action="functions.asp?action=<%=action%>" method="post" onSubmit="return checkForm();">
        <input type="hidden" name="LOGINID" value="<%=LOGINID%>">
        <input type="hidden" name="id" value="<%=id%>">
        <tr class="ListTableHeader">
          <td colspan="2" background="images/TableHeaderBg.gif" nowrap><b>类别编辑：</b></td>
        </tr>
        <tr class="InputTableRow">
          <td align="right" width="80">位置：</td>
          <td><input name="pos" value="<%=iPos%>" size="3" class="ButtonReply"></td>
        </tr>
        <tr class="InputTableRow">
          <td align="right" width="80">频道代码：</td>
          <td><input name="channel" value="<%=strChannel%>" size="12" class="ButtonReply"></td>
        </tr>
        <tr class="InputTableRow">
          <td align="right">父结点：</td>
          <td>
            <select name="iNewPID" class="ButtonSelect">
              <%=trim(strClassOption)%>
            </select>
          </td>
        </tr>
        <tr class="InputTableRow">
          <td align="right">名称：</td>
          <td><input name="className" value="<%=strNodeName%>" size="48" class="ButtonReply"><font color="RED">*</font></td>
        </tr>
        <tr class="InputTableRow">
          <td align="right">状态：</td>
          <td>
            <select name="status" class="ButtonSelect">
<%
	for i=1 to ubound(newsStatus)-1
		Response.Write "              <option value="""& i &""""
		if iStatus = i then
			Response.Write " selected"
		end if
		Response.Write ">"& newsStatus(i) &"</option>"& vbCrLf
	next
%>
            </select>
          </td>
        </tr>
        <tr class="MainPageBar">
          <td align="center" colspan="2">
<%
	if (subaction <> "addSon") and (iLevelMain > 1) then
%>
            <input type="hidden" name="subaction" value="edit">
            <input type="submit" name="submit" value="  更新  " class="ButtonReply">&nbsp;&nbsp;
            <input type="button" name="submit" value="  发布  " class="ButtonReply" onclick="javascript: doIssue(<%=id%>);">&nbsp;&nbsp;
<%
		if iLevelMain > 1 then
%>
            <input type="button" name="submit" value="  删除  " class="ButtonReply" onclick="if (confirm('确定要删除吗？')) window.location='functions.asp?action=<%=action%>&LOGINID=<%=LOGINID%>&id=<%=id%>&subaction=del';">
<%
		end if
	end if

	if subaction = "addSon" then
%>
            <input type="hidden" name="subaction" value="add">
            <input type="submit" name="submit" value="  添加  " class="ButtonReply">
<%
	elseif iLevelMain < 3 then
%>
            <input type="button" name="submit" value="  添加子节点  " class="ButtonReply" onclick="javascript:window.location='functions.asp?action=<%=action%>&LOGINID=<%=LOGINID%>&id=<%=id%>&subaction=addSon'">
<%
	end if
%>
          </td>
        </tr>
      </form>
      </table>
	  <br>
      <script language="JavaScript">
      <!--
        function checkForm()
        {
          if (document.InfoEdit.channel.value.length == 0)
          {
            alert("请输入新闻所属频道代码！");
            document.InfoEdit.channel.focus();
            return false;
          }
          if (document.InfoEdit.className.value.length == 0)
          {
            alert("请输入新闻类别名称！");
            document.InfoEdit.className.focus();
            return false;
          }
          return true;
        }

        function doIssue(pid)
		{
		  window.showModalDialog("frame.asp?action=newsMenu&subaction=preEdit&LOGINID=<%=LOGINID%>&pid="+ pid,null,"dialogwidth=640pt;dialogheight=450pt;toolbar=no;status=no;resizable=no");
		  return;
        }
        //-->
      </script>
<%
end function

function levelNum(id,iLevel)
	dim iPid,myRS

	set myRS = server.createobject("adodb.recordset")
	sql = "select pid from cm_newsClass where id="& id
	myRS.Open sql,conn,1,1
	if not myRS.Eof then
		iLevel = iLevel + 1
		iPid = myRS("pid")
		myRS.Close

		levelNum = levelNum(iPid,iLevel)
	else
		myRS.Close

		levelNum = iLevel
	end if
end function

function showNode(iNodeID,strName)
	response.write "<table><tr><td>"
	response.write "<table border=""1"" cellpadding=""0"" cellspacing=""0"" bordercolorlight=""#FF9900"" bordercolordark=""#FFFFFF""><tr bgcolor=""efefef"" align=""center""><td height=""22"" nowrap>&nbsp;"
	if iNodeID = id then
		response.write "<b><font color=ff000 title=""当前节点"">"& strName &"</font></b>"
	else
		response.write "<a href=""functions.asp?action="& action &"&LOGINID="& LOGINID &"&id="& iNodeID &""">"& strName &"</a>"
	end if
	response.write "&nbsp;</td></table>"
	response.write "</table>"
end function

sub showTree(iNodeID,strName,iPrefix)	'iPrefix = 1,2,3,0 1=First, 2=Middle, 3=Last, 0= display no iPrefix.
	dim iNode,myRS,iNewID,strNewNode,iChildren

	response.write "<table border=""0"" cellspacing=""0"" cellpadding=""0"" height=""100%""><tr>"
	if iPrefix > 0 then
		response.write "<td><table height=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"" width=""1px"">"
		if iPrefix = 1 then
			response.write "<tr height=""50%""><td width=""1px""></td></tr><tr height=""50%""><td width=""1px"" bgcolor=""#777777""></td></tr>"
		elseif iPrefix = 3 then
			response.write "<tr height=""50%""><td width=""1px"" bgcolor=""#777777""></td></tr><tr height=""50%""><td></td></tr>"
		else
			response.write "<tr height=""100%""><td height=""100%"" width=""1px"" bgcolor=""#777777""></td></tr>"
		end if
		response.write "</table></td>"
		response.write "<td nowrap>--</td>"
	end if

	response.write "<td>"
	showNode iNodeID,strName
	response.write "</td>"

	set myRS = server.createobject("adodb.recordset")
	sql = "select id,className from cm_newsClass where pid="& iNodeID
	myRS.Open sql,conn,1,1
	iChildren = myRS.RecordCount
	if iChildren > 0 then
		response.write "<td nowrap>--</td>"
		response.write "<td><table border=""0"" cellspacing=""0"" cellpadding=""0"">"

		iNode = 0
		do while not myRS.Eof
			if iChildren = 1 then
				iPrefix = 0
			elseif iNode = 0 then
				iPrefix = 1
			elseif iNode = iChildren-1 then
				iPrefix = 3
			else
				iPrefix = 2
			end if
			response.write "<tr><td>"
			iNewID = myRS("id")
			strNewNode = myRS("className")

			showTree iNewID,strNewNode,iPrefix
			response.write "</td></tr>"

			myRS.MoveNext
			iNode = iNode + 1
		loop

		response.write "</table></td>"
	end if
	myRS.Close

	response.write "</tr></table>"
end sub

id = Request("id")
strNodeName = Request("className")
strChannel = Request("channel")
iNewPID = Request("iNewPID")
iPos = Request("pos")
iStatus = Request("status")
if id <> 0 then
	if subaction = "del" then
		sql = "select count(*) from cm_newsClass where pid="& id
		rs.Open sql,conn,1,1
		if not rs.Eof then
		  iChildrenMain = rs(0)
		end if
		rs.Close
		if iChildrenMain > 0 then
			errorMsg = "不能直接删除包含子节点的节点。请先删除所有子节点。"
		else
			sql = "delete from cm_newsClass where id="& id
			conn.Execute(sql)
			errorMsg = "删除成功！"
		end if
		id = 0
	elseif subaction = "edit" then
		sql = "update cm_newsClass set pos="& iPos &",className='"& strNodeName &"',channel='"& strChannel &"',pid="& iNewPID &",status="& iStatus &" where id="& id
		if strNodeName = "" then
			errorMsg = "请填写类型名称！"
		else
			conn.Execute(sql)
			errorMsg = "更新成功！"
		end if
		id = 0
	elseif subaction = "add" then
		sql = "insert into cm_newsClass (pos,className,channel,pid,status,createDate) values("& iPos &",'"& strNodeName &"','"& strChannel &"',"& id &","& iStatus &",GETDATE())"
		if strNodeName = "" then
			errorMsg = "请填写类型名称！"
		else
			conn.Execute(sql)
			errorMsg = "添加成功！"
		end if
		id = 0
	else
		nodeInfo
	end if
end if

if errorMsg <> "" then
	DisplayMessage errorMsg,True
end if

sql = "select id,className from cm_newsClass where pid=0"
rs.Open sql,conn,1,1
if not rs.Eof then
	cid = rs("id")
	strNodeName = rs("className")
else
	cid = 0
	strNodeName = "学优网"
	conn.execute("insert into cm_newsClass(className,pid,createDate) values('"& strNodeName &"','0',GETDATE())")
end if
rs.Close
%>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td align="center">
	<%showTree cid,strNodeName,0%>
    </td>
  </tr>
</table>