using System;
using System.Collections;
using System.Configuration;
using System.IO;
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.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.Common
{
    public partial class Special_Add : System.Web.UI.Page
    {
        SpecialManage MySpecialBLL = new SpecialManage();
        protected ModelInfo model;
        int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelID = int.Parse(Request.QueryString["ModelID"]);
            model = ModelManage.GetInfo(ModelID);
            if (!IsPostBack)
            {
                string Action = Request["Action"];
                if (Action == "Edit")
                {
                    RightsCheck.CheckWorkRight("SpecialEdit", true);
                    EditUiLoad();
                }
                else
                {
                    RightsCheck.CheckWorkRight("SpecialEdit", true);
                    AddUiLoad();
                }

            }
        }

        private void AddUiLoad()
        {
            this.Title = "添加专题";
            BT_OK.Text = "添加";

            new SpecialManage().BindDDL(DDL_Special, TopWinCMS.Model.Enum.Special_BindOption.NoLink, "<添加为一级专题栏目>", ModelID);

            if (Request["SpecialID"] != null)
            {
                DDL_Special.SelectedValue = Request["SpecialID"];
            }

            ModelType BaseModel = ModelManage.GetBaseModel(ModelID);
            switch (BaseModel)
            {
                case ModelType.Article:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ArticleSpecial_aspx;
                    break;
                case ModelType.Soft:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.SoftSpecial_aspx;
                    break;
                case ModelType.Product:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ProductSpecial_aspx;
                    break;
                case ModelType.Shop:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ShopSpecial_aspx;
                    break;
                case ModelType.Ask:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.AskSpecial_aspx;
                    break;
            }

            UC_AdminSelect1.BT_SelectAdmin.Enabled = RightsCheck.HasRight("SpecialControl", true);

            UC_AdminSelect2.BT_SelectAdmin.Enabled = RightsCheck.HasRight("SpecialControl", true);

        }

        private void EditUiLoad()
        {

            this.Title = "修改专题";
            BT_OK.Text = "修改";

            SpecialInfo model = new SpecialInfo();
            new SpecialManage().BindDDL(DDL_Special, TopWinCMS.Model.Enum.Special_BindOption.NoLink, "<添加为一级专题栏目>", ModelID);

            model = MySpecialBLL.EditLoad(Request["ID"]);
            DDL_Special.SelectedValue = Request["ID"];
            DDL_Special.Enabled = false;

            TB_SpecialName.Text = model.SpecialName;
            TB_SpecialName_EN.Text = model.SpecialName_EN;
            TB_TemplateFileName.Text = model.TemplateFileName;
            CB_IsShowInIndex.Checked = model.IsShowInIndex == 1 ? true : false;

            TB_LinkUrl.Text = model.LinkUrl;
            UC_AdminSelect1.LB_Selected.Text = model.ContentAdmin;
            UC_AdminSelect1.BT_SelectAdmin.Enabled = RightsCheck.HasRight("SpecialControl", true);
            UC_AdminSelect2.LB_Selected.Text = model.SpecialAdmin;
            UC_AdminSelect2.BT_SelectAdmin.Enabled = RightsCheck.HasRight("SpecialControl", true);


            TB_SpecialFilePostfix.Text = model.SpecialFilePostfix;
            TB_SpecialFilePrefix.Text = model.SpecialFilePrefix;
            RBL_SpecialURLType.SelectedValue = model.SpecialURLStyle.ToString();
            TB_SpecialIntro.Text = model.SpecialIntro;
            TB_SpecialPic.Text = model.SpecialPic;
            TB_SpecialDescription.Text = model.SpecialDescription;
            TB_SpecialKeyWord.Text = model.SpecialKeyWord;
            RBL_OpenGrade.SelectedValue = model.OpenGrade.ToString();


        }

        protected void BT_OK_Click(object sender, System.EventArgs e)
        {
            SpecialInfo model = new SpecialInfo();
            model.ID = int.Parse(DDL_Special.SelectedValue);
            model.ModelID = ModelID;
            model.SpecialName = TB_SpecialName.Text.Trim();
            model.SpecialName_EN = TB_SpecialName_EN.Text.Trim();
            model.TemplateFileName = TB_TemplateFileName.Text;
            model.IsShowInIndex = CB_IsShowInIndex.Checked == true ? 1 : 0;

            model.LinkUrl = TB_LinkUrl.Text;
            model.ContentAdmin = UC_AdminSelect1.LB_Selected.Text;
            model.SpecialAdmin = UC_AdminSelect2.LB_Selected.Text;


            model.SpecialFilePostfix = TB_SpecialFilePostfix.Text.Trim();
            model.SpecialFilePrefix = TB_SpecialFilePrefix.Text.Trim();
            model.SpecialURLStyle = int.Parse(this.RBL_SpecialURLType.SelectedValue);
            model.SpecialIntro = TB_SpecialIntro.Text.Trim();
            model.SpecialPic = TB_SpecialPic.Text.Trim();
            model.SpecialDescription = TB_SpecialDescription.Text.Trim();
            model.SpecialKeyWord = TB_SpecialKeyWord.Text.Trim();
            model.OpenGrade = int.Parse(this.RBL_OpenGrade.SelectedValue);



            string strErr = null;
            MySpecialBLL.Add(model, Request["Action"], ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr);
            }
            else
            {
                System.Web.HttpContext.Current.Response.Redirect("Special.aspx?ModelID=" + model.ModelID);
            }
        }
    }
}