using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TopWinCMS.BLL;
using TopWinCMS.BLL.PageFactory;
using TopWinCMS.Common;
using TopWinCMS.Model;
using TopWinCMS.Model.Enum;
namespace TopWinCMS.WebUI.Admin.ContentRelease
{
    public partial class MakeHTML : TopWinCMS.WebFactory.PageBase
    {
        int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["ModelID"] == null)
                Response.Redirect("MakeHTML.aspx?ModelID=1");

            ModelID = int.Parse(Request["ModelID"]);


            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("R_1");

                this.Wizard1.ActiveStepIndex = 0;
                this.Title = "发布" + ModelManage.GetModelName(ModelID);
                LB_ContentName.Text = ModelManage.GetContentName(ModelID);
                LB_ContentUnit.Text = ModelManage.GetContentUnit(ModelID);

                new ClassManage().BindDDL(LTB_Class, Class_BindOption.NoLink, "生成全部", ModelID);

                new SpecialManage().BindDDL(LTB_Special, Special_BindOption.NoLink, "生成全部", ModelID);

                ModelInfo mm = ModelManage.GetInfo(ModelID);
                if (mm.EnableStatic == 0)
                {
                    BT_MakeNew.Enabled = false;
                    BT_MakeNewForID.Enabled = false;
                    BT_MakeForDate.Enabled = false;
                    BT_MakeClass.Enabled = false;
                    BT_MakeSpecial.Enabled = false;
                    BT_ChannelIndex.Enabled = false;
                    BT_ALL.Enabled = false;

                    Li_Err.Text = string.Format("该模型启用的是动态页，不能生成Ｈｔｍｌ了。<br />如果有需要，请点击此处<a href=\"#\"  style=\"font-weight: bold\" onclick=\"return NewTabPage('修改模型{0}','修改模型','Model/AddInfo.aspx?Action=Edit&ID={0}');\" >修改模型设置</a>", ModelID);
                }


                BT_Index.Enabled = Config.Static;
                TB_MaxDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                TB_MinDate.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");


                div_Progress1.Attributes.Add("onclick", string.Format("return NewTabPage('内容页进度_ModelID_{0}','内容页进度','ContentRelease/Progress.aspx?Action=ContentPage&ModelID={0}');", ModelID));
                div_Progress2.Attributes.Add("onclick", string.Format("return NewTabPage('栏目页进度_ModelID_{0}','栏目页进度','ContentRelease/Progress.aspx?Action=ClassPage&ModelID={0}');", ModelID));
                div_Progress3.Attributes.Add("onclick", string.Format("return NewTabPage('专题页进度_ModelID_{0}','专题页进度','ContentRelease/Progress.aspx?Action=SpecialPage&ModelID={0}');", ModelID));
                div_Progress4.Attributes.Add("onclick", string.Format("return NewTabPage('全部生成进度_ModelID_{0}','全部生成进度','ContentRelease/Progress.aspx?Action=MakeAll&ModelID={0}');", ModelID));
              
            }

        }

        protected void BT_MakeNew_Click(object sender, EventArgs e)
        {
            if (NTB_ZuiXin.Text.Length == 0)
            {
                MsgBox.Alert("不能为空", UpdatePanel1);
                return;
            }
            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            int Top = int.Parse(NTB_ZuiXin.Text);

            if (Top < 1)
            {
                Top = int.MaxValue;
            }

            if (RBL_MakeType.SelectedIndex == 0)
            {
                string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ContentPage&ModelID=" + ModelID + "&Top=" + Top);

                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
            }

            MyBLL.ContentPage(Top);

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);
        }

        protected void BT_MakeNewForID_Click(object sender, EventArgs e)
        {
            if (RBL_MakeType.SelectedIndex == 0)
            {
                string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ContentPage&ModelID=" + ModelID + "&MinID=" + TB_MinID.Text.Trim() + "&MaxID=" + TB_MaxID.Text);

                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
            }

            string strWhere = string.Format(" ID>={0} and ID<={1} ", TB_MinID.Text.Trim(), TB_MaxID.Text);

            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            MyBLL.ContentPage(strWhere);

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);

        }

        protected void BT_MakeForDate_Click(object sender, EventArgs e)
        {
            if (RBL_MakeType.SelectedIndex == 0)
            {
                string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ContentPage&ModelID=" + ModelID + "&MinDate=" + TB_MinDate.Text.Trim() + "&MaxDate=" + TB_MaxDate.Text);

                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
            }

            string strWhere = string.Format(" UpdateTime between #{0}# and #{1}# ", TB_MinDate.Text, TB_MaxDate.Text);

            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            MyBLL.ContentPage(strWhere);

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);
        }

        protected void BT_MakeClass_Click(object sender, EventArgs e)
        {
            MyStringBuilder IDs = new MyStringBuilder();
            bool ALL = false;
            foreach (ListItem li in LTB_Class.Items)
            {
                if (li.Value == "0" && li.Selected == true)
                {
                    ALL = true;
                    IDs.Append(string.Format(" ModelID = {0}", ModelID));
                    break;
                }

                if (li.Selected)
                {
                    IDs.Append(li.Value + ",");
                }
            }
            if (IDs.ToString().Length == 0)
            {
                MsgBox.Alert("请选择", this.UpdatePanel1);
                return;
            }
            IDs.TrimEnd();

            if (RBL_MakeType.SelectedIndex == 0)
            {
                if (ALL == false)
                {
                    string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ClassPage&ModelID=" + ModelID + "&Where=" + string.Format(" ID in ({0}) and ModelID = {1}", IDs.ToString(), ModelID));

                    Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
                }
                else
                {

                    string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ClassPage&ModelID=" + ModelID + "&Where=" + IDs.ToString());

                    Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
                }
            }

            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            if (ALL == false)
                MyBLL.ClassPage(string.Format(" ID in ({0}) and ModelID = {1}", IDs.ToString(), ModelID));
            else
                MyBLL.ClassPage(IDs.ToString());

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);
        }

        protected void BT_MakeSpecial_Click(object sender, EventArgs e)
        {
            MyStringBuilder IDs = new MyStringBuilder();
            bool ALL = false;
            foreach (ListItem li in LTB_Special.Items)
            {
                if (li.Value == "0" && li.Selected == true)
                {
                    ALL = true;
                    IDs.Append(" 1=1 ");
                    break;
                }

                if (li.Selected)
                {
                    IDs.Append(li.Value + ",");
                }
            }
            if (IDs.ToString().Length == 0)
            {
                MsgBox.Alert("请选择", this.UpdatePanel1);
                return;
            }
            IDs.TrimEnd();


            if (RBL_MakeType.SelectedIndex == 0)
            {
                if (ALL == false)
                {
                    string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=SpecialPage&ModelID=" + ModelID + "&Where=" + string.Format(" ID in ({0}) and ModelID = {1}", IDs.ToString(), ModelID));

                    Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
                }
                else
                {
                    string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=SpecialPage&ModelID=" + ModelID + "&Where=" + IDs.ToString());

                    Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
                }
            }

            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            if (ALL == false)
                MyBLL.SpecialPage(string.Format(" ID in ({0})", IDs.ToString()));
            else
                MyBLL.SpecialPage(IDs.ToString());

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);
        }

        protected void BT_ChannelIndex_Click(object sender, EventArgs e)
        {
            if (RBL_MakeType.SelectedIndex == 0)
            {
                string URL = HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ChannelIndexPage&ModelID=" + ModelID + "&ALL=" + CB_AllChannel.Checked.ToString());

                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + URL);
            }

            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            MyBLL.ChannelIndexPage(CB_AllChannel.Checked);

            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);

        }

        protected void BT_Index_Click(object sender, EventArgs e)
        {
            if (RBL_MakeType.SelectedIndex == 0)
            {
                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=IndexPage"));
            }
            string SavePath = Server.MapPath("~/");

            SaveHTML MyBLL = new SaveHTML(ModelID);

            MyBLL.IndexPage();

            MsgBox.Alert("生成完毕.", this.UpdatePanel1);
        }

        protected void BT_ALL_Click(object sender, EventArgs e)
        {
            if (RBL_MakeType.SelectedIndex == 0)
            {
                Response.Redirect("MakeHTML_XmlHttp.aspx?URL=" + HttpUtility.UrlEncode("AjaxPostBack.ashx?Action=ALL&ModelID=" + ModelID));
            }

            string SavePath = Server.MapPath("~/");

            new SaveHTML(ModelID).ALL(ModelID);


            MsgBox.Alert("生成HTML的工作已经在后台开始执行,你可以进行其他工作了.", this.UpdatePanel1);
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            //MyJavascript.RegJS(UpdatePanel1, "Auto已经作废IframeHeight();");
            if (e.NextStepIndex == 2)
            {
                switch (RBL_DataType.SelectedValue)
                {
                    case "0":
                        ContentPage.Visible = true;
                        ClassPage.Visible = false;
                        SpecialPage.Visible = false;
                        IndexPage.Visible = false;
                        AllPage.Visible = false;
                        break;
                    case "1":
                        ContentPage.Visible = false;
                        ClassPage.Visible = true;
                        SpecialPage.Visible = false;
                        IndexPage.Visible = false;
                        AllPage.Visible = false;
                        break;
                    case "2":
                        ContentPage.Visible = false;
                        ClassPage.Visible = false;
                        SpecialPage.Visible = true;
                        IndexPage.Visible = false;
                        AllPage.Visible = false;
                        break;
                    case "3":
                        ContentPage.Visible = false;
                        ClassPage.Visible = false;
                        SpecialPage.Visible = false;
                        IndexPage.Visible = true;
                        AllPage.Visible = false;
                        break;
                    case "4":
                        ContentPage.Visible = false;
                        ClassPage.Visible = false;
                        SpecialPage.Visible = false;
                        IndexPage.Visible = false;
                        AllPage.Visible = true;
                        break;
                }
            }
        }

        protected void RBL_MakeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool b = RBL_MakeType.SelectedIndex == 0 ? false : true;

            div_Progress1.Visible = b;
            div_Progress2.Visible = b;
            div_Progress3.Visible = b;
            div_Progress4.Visible = b;
        }
    }
}