using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
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.Common;
using TopWinCMS.Model.Enum;
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin.Common
{
    public partial class Maintenance : System.Web.UI.Page
    {
        private int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelID = int.Parse(Request["ModelID"]);
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkRight("OP", true);
                new SpecialManage().BindDDL(this.DDL_Special, Special_BindOption.NoLink, "<请选择>", ModelID);
                ClassManage MyBLL_Class = new ClassManage();
                MyBLL_Class.BindDDL(this.DDL_Class, Class_BindOption.NoLink, "<请选择>", ModelID);
                MyBLL_Class.BindDDL(this.DDL_ClassSearch, Class_BindOption.NoLink, "<全部栏目>", ModelID);


            }
        }

        protected void BT_SelectOne_Click(object sender, EventArgs e)
        {
            int Count = LB_LeftList.Items.Count;
            int Index = 0;
            for (int i = 0; i < Count; i++)
            {
                ListItem Item = LB_LeftList.Items[Index];
                if (LB_LeftList.Items[Index].Selected == true)
                {
                    LB_LeftList.Items.Remove(Item);
                    LB_RightList.Items.Add(Item);
                    Index--;
                }
                Index++;
            }
        }

        protected void BT_SelectAll_Click(object sender, EventArgs e)
        {
            //foreach (ListItem li in LB_LeftList.Items)
            //{
            //    LB_RightList.Items.Add(li);
            //}
            ListItem[] list = new ListItem[LB_LeftList.Items.Count];
            for (int i = 0; i < LB_LeftList.Items.Count; i++)
            {
                list[i] = LB_LeftList.Items[i];
            }

            LB_RightList.Items.AddRange(list);
            LB_LeftList.Items.Clear();
        }

        protected void BT_ReMoveOne_Click(object sender, EventArgs e)
        {
            int Count = LB_RightList.Items.Count;
            int Index = 0;
            for (int i = 0; i < Count; i++)
            {
                ListItem Item = LB_RightList.Items[Index];
                if (LB_RightList.Items[Index].Selected == true)
                {
                    LB_RightList.Items.Remove(Item);
                    LB_LeftList.Items.Add(Item);
                    Index--;
                }
                Index++;
            }
        }

        protected void BT_ReMoveAll_Click(object sender, EventArgs e)
        {
            ListItem[] list = new ListItem[LB_RightList.Items.Count];
            for (int i = 0; i < LB_RightList.Items.Count; i++)
            {
                list[i] = LB_RightList.Items[i];
            }
            LB_LeftList.Items.AddRange(list);
            LB_RightList.Items.Clear();
        }



        protected void BT_MoveToNewSpecial_Click(object sender, EventArgs e)
        {

            if (CB_使用ID.Checked == false)
            {
                MyStringBuilder IDs = new MyStringBuilder();
                foreach (ListItem li in LB_RightList.Items)
                {
                    IDs.Append(li.Value + ",");
                }
                IDs.TrimEnd();

                if (string.IsNullOrEmpty(IDs.ToString()) == true)
                {
                    MsgBox.Alert("请选择文章", UpdatePanel1);
                    return;
                }

                new TopWinCMS.BLL.Common.Maintenance(ModelID).MoveToNewSpecial(IDs.ToString(), DDL_Special.SelectedValue, UpdatePanel1);


            }
            else
            {
                if (string.IsNullOrEmpty(TB_IDs.Text) == true)
                {
                    MsgBox.Alert("请输入ID", UpdatePanel1);
                    return;
                }
                new BLL.Common.Maintenance(ModelID).MoveToNewSpecial(this.TB_IDs.Text, DDL_Special.SelectedValue, UpdatePanel1);


            }

        }
        protected void BT_MoveToNewClass_Click(object sender, EventArgs e)
        {
            MyStringBuilder IDs = new MyStringBuilder();
            foreach (ListItem li in LB_RightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();


            new BLL.Common.Maintenance(ModelID).MoveToNewClass(IDs.ToString(), DDL_Class.SelectedValue, UpdatePanel1);

            TB_Keyword.Text = "";
            LB_LeftList.Items.Clear();
            LB_RightList.Items.Clear();
        }

        protected void BT_Search_Click(object sender, EventArgs e)
        {
            ContentManageManage MyClassBLL = new ContentManageManage(ModelID);
            SQLModel s_model = new SQLModel();
            s_model.Fields = "ID,ContentTitle";
            s_model.OrderField = "ID desc";

            s_model.PageSize = int.MaxValue;
            s_model.WHERE = " Deleted=0";

            if (string.IsNullOrEmpty(TB_Keyword.Text.Trim()) == false)
            {
                s_model.WHERE += " and ContentTitle like '%" + MyString.SQL_ESC(TB_Keyword.Text.Trim()) + "%'";
            }

            if (this.DDL_ClassSearch.SelectedValue != "0")
            {
                s_model.WHERE += @" and ClassID in(select ID from TopWinCMS_Class where Path like (select Path+'%' from TopWinCMS_Class where ID=" + DDL_ClassSearch.SelectedValue + "))";

            }

            MyClassBLL.BindList(LB_LeftList, s_model);
            if (LB_LeftList.Items.Count < 1)
            {
                MsgBox.Alert("查找不到数据", UpdatePanel1);
            }
        }

        protected void BT_Replace_Click(object sender, EventArgs e)
        {
            MyStringBuilder IDs = new MyStringBuilder();
            foreach (ListItem li in LB_RightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();
            new BLL.Common.Maintenance(ModelID).ReplaceContent(TB_OldString.Text, TB_NewString.Text, IDs.ToString(), CB_Regex.Checked, UpdatePanel1);

        }
        protected void CB_使用ID_CheckedChanged(object sender, EventArgs e)
        {
            //CB_使用选取方式.Checked = !CB_使用ID.Checked;
            //TB_IDs.Enabled = CB_使用ID.Checked;
        }

        protected void BT_UpData_Click(object sender, EventArgs e)
        {
            List<bool> Selected = new List<bool>();
            List<object> Values = new List<object>();

            Selected.Add(CB_TemplateFileName.Checked);
            Values.Add(TB_TemplateFileName.Text.Trim());

            Selected.Add(CB_key.Checked);
            Values.Add(TB_key.Text.Trim());

            Selected.Add(CB_Stars.Checked);
            Values.Add(DDL_Stars.SelectedValue);

            Selected.Add(CB_Elite.Checked);
            Values.Add(RBL_Elite.Items[0].Selected);

            Selected.Add(CB_OnTop.Checked);
            Values.Add(RBL_OnTop.Items[0].Selected);

            Selected.Add(CB_AllowComment.Checked);
            Values.Add(RBL_AllowComment.Items[0].Selected);

            Selected.Add(CB_Pass.Checked);
            Values.Add(RBL_Pass.Items[0].Selected);


            MyStringBuilder IDs = new MyStringBuilder();
            foreach (ListItem li in LB_RightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();


            new BLL.Common.Maintenance(ModelID).BatchUpData(Selected, Values, IDs.ToString(), UpdatePanel1);

        }


    }
}