<%@ Page Language="C#" AutoEventWireup="true" CodeFile="column_generats.aspx.cs"
    Inherits="Whir_System_ajax_content_column_generats" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<%@ Import Namespace="Whir.Framework" %>
<%@ Import Namespace="Whir.Service" %>
<%@ Import Namespace="Whir.Domain" %>
<%@ Import Namespace="Whir.Framework" %>
<%@ Import Namespace="Whir.Language" %>
<%@ Import Namespace="System.Linq.Expressions" %>
<%@ Import Namespace="System.Linq" %>
<script type="text/C#" runat="server">

    public int siteid { get; set; }

    /// <summary>
    /// 页面加载, 处理异步请求, 根据栏目名称返回一串json字符串
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        new Whir.ezEIP.Web.SysManagePageBase().IsHadLogin(); //判断是否登录了
        int nodeid = WebUtil.Instance.GetQueryInt("nodeid", 0);
        string nodes = ServiceFactory.ColumnService.GetParentsByID(nodeid);
        siteid = WebUtil.Instance.GetQueryInt("siteid", 1);

        Response.Write(GetNode(0).ToStr().Trim());

    }

    /// <summary>
    /// 获取子节点
    /// </summary>
    /// <returns></returns>
    private string GetNode(int parentId)
    {
        StringBuilder nodeBuilderAll = new StringBuilder();

        IList<Column> columnList = GetChildList(parentId);
        nodeBuilderAll.Append("[");

        foreach (Column cl in columnList)
        {
            NodeChildBuilder = new StringBuilder();
            StringBuilder nodeBuilder = new StringBuilder();

            nodeBuilder.Append("{");
            nodeBuilder.Append(("text: '{0}',").FormatWith(Regex.Replace(cl.ColumnName, "[\\W]", "")));
            nodeBuilder.Append("href: '#',");
            nodeBuilder.Append(("columnid: '{0}',").FormatWith(cl.ColumnId));

            List<Column> childColumnList = GetChildList(cl.ColumnId);
            nodeBuilder.Append(("tags: [{0}],").FormatWith(childColumnList.Count));

            GetChildNode(cl.ColumnId);
            nodeBuilder.Append(NodeChildBuilder);

            nodeBuilder.Append("},");
            nodeBuilderAll.Append(nodeBuilder.ToStr());

        }

        nodeBuilderAll.Append("]");
        return nodeBuilderAll.ToStr();
    }
    //NodeChildBuilder NodeChildBuilder
    StringBuilder NodeChildBuilder = new StringBuilder();

    private void GetChildNode(int parentId)
    {
        List<Column> childColumnList = GetChildList(parentId);

        if (childColumnList.Count > 0)
        {
            NodeChildBuilder.Append("nodes: [");
            //子节点
            foreach (Column columnItem in childColumnList)
            {
                NodeChildBuilder.Append("{");
                NodeChildBuilder.Append(("text: '{0}',").FormatWith(Regex.Replace(columnItem.ColumnName, "[\\W]", "")));
                NodeChildBuilder.Append("href: '#',");
                NodeChildBuilder.Append(("columnid: '{0}',").FormatWith(columnItem.ColumnId));
                NodeChildBuilder.Append(("tags: [{0}],").FormatWith(GetChildList(columnItem.ColumnId).Count));

                GetChildNode(columnItem.ColumnId);
                NodeChildBuilder.Append("},");
            }
            NodeChildBuilder.Append("]");
        }

    }

    /// <summary>
    /// 获取子节点数
    /// </summary>
    /// <returns></returns>
    private List<Column> GetChildList(int parentId)
    {
        return ServiceFactory.ColumnService.Query<Column>("WHERE IsDel=0 AND MarkParentId=0 AND ParentId=@1 AND SiteType=0 AND SiteID=@0 ORDER BY Sort ASC", siteid, parentId).ToList<Column>();
    }


    /// <summary>
    /// 创建一个树形类
    /// </summary>
    class ColumnTree
    {
        public int id { get; set; }//本身Id
        public int pId { get; set; }//子Id
        public string name { get; set; }//显示名称
        public bool open { get; set; }//开放闭合显示
        public bool nocheck { get; set; }//是否显示选中框
    }

</script>
