using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fm.Bussiness;
using Fm.Tags;
using System.Text.RegularExpressions;
using System.IO;

public partial class admin_htm_article : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            ColumnBind();
        }
  
    }
    //绑定栏目的方法
    void ColumnBind()
    {
        //绑定栏目信息
        Column cn = new Column();
        cn.GetColumns(Column_List);

        //添加第一个栏目
        ListItem item = new ListItem();
        item.Text = "所有栏目";
        item.Value = "0";

        Column_List.Items.Insert(0, item);
    }
    /// <summary>
    /// 单击按钮启动静态生成!
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        public_select ps = new public_select();//公用查询类
        //查询栏目信息
        DataView dv = ps.Getps("Fm_column", "cl_id,cl_name,cl_parent", "");
        //Lab_info.Text = Column_List.SelectedValue;
        bool bo = false;//用来判断是否选择了所有栏目
        foreach (ListItem item in Column_List.Items)
        {
            if (item.Selected)
            {
                if (item.Value == "0")
                {
                    bo = true;
                }
            }
        }
        if (bo)
        {
            foreach (DataRow row in dv.Table.Rows)
            {
                WriteContent(row);
            }
        }
        else
        {
            foreach (ListItem item in Column_List.Items)
            {
                //如果是选择项才生成静态文档
                if (item.Selected)
                {
                    foreach (DataRow row in dv.Table.Rows)
                    {
                        if (row["cl_id"].ToString() == item.Value)
                        {
                            //判断是否连子栏目一起生成
                            if (Radio_Parent.SelectedValue == "0")
                            {
                                WriteContent(row);
                            }
                            else
                            { 
                                //获取所选栏目parent
                                string parent = row["cl_parent"].ToString();
                                //循环生成子栏目静态文档
                                foreach (DataRow dr in dv.Table.Rows)
                                {
                                    if (dr["cl_parent"].ToString().Length >= parent.Length)
                                    {
                                        if (dr["cl_parent"].ToString().Substring(0, parent.Length) == parent)
                                        {
                                            WriteContent(dr);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
       
        Lab_info.Text = "系统提示:内容页生成成功!!!";
        //生成后重新绑定栏目
        Column_List.Items.Clear();
        ColumnBind();
    }
    /// <summary>
    /// 静态文档写入
    /// </summary>
    /// <param name="row"></param>
    void WriteContent(DataRow row)
    {
        public_select ps = new public_select();//公用查询类
        Tags_sql sql = new Tags_sql();
        int cid = int.Parse(row["cl_id"].ToString());
        //获取栏目文章信息
        DataView dw = ps.Getps("Fm_model_main","docid","colid=" + cid + " and html=1");
        //调用生成方法
        HtmlWrite write = new HtmlWrite();
        //获取指定文档生成
        int index = 1;
        //循环生成
        foreach (DataRow dr in dw.Table.Rows)
        {
            if (Radio_All.SelectedValue == "1")
            {
                if (index > int.Parse(txtcount.Text))
                {
                    break;
                }
            }
            write.WriteContent(int.Parse(dr["docid"].ToString()));
            index++;
        }
    }
}
