using System;
using System.Collections;
using System.Collections.Generic;
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 Class_Add : System.Web.UI.Page
    {
        ClassManage MyClassBLL = new ClassManage();
        protected ModelInfo model;
        int _ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {

            _ModelID = int.Parse(Request["ModelID"]);
            model = ModelManage.GetInfo(_ModelID);
            if (!IsPostBack)
            {
                string Action = Request["Action"];
                if (Action == "Edit")
                {
                    RightsCheck.CheckWorkRight("ClassEdit", true);
                    EditUiLoad();
                }
                else
                {
                    RightsCheck.CheckWorkRight("ClassAdd", true);
                    AddUiLoad();
                }
            }
        }

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

            new ClassManage().BindDDL(DDL_Class, TopWinCMS.Model.Enum.Class_BindOption.NoLink, "<添加为一级栏目>", _ModelID);

            new ClassManage().BindDDL(DDL_CopyClass, TopWinCMS.Model.Enum.Class_BindOption.NoLink, "<-无->", _ModelID);

            if (Request["ClassID"] != null)
            {
                DDL_Class.SelectedValue = Request["ClassID"];
            }
            UC_AdminSelect1.BT_SelectAdmin.Enabled = RightsCheck.HasRight("ClassControl", true);
            UC_AdminSelect2.BT_SelectAdmin.Enabled = RightsCheck.HasRight("ClassControl", true);

            ModelType BaseModel = ModelManage.GetBaseModel(_ModelID);
            if (ModelManage.GetEnableStatic(_ModelID) == 1)
            {
                RBL_URLType.SelectedValue = "1";
                RBL_ClassURLType.SelectedValue = "1";
            }

            switch (BaseModel)
            {
                case ModelType.Article:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ArticleClass_aspx;
                    break;
                case ModelType.Soft:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.SoftClass_aspx;
                    break;
                case ModelType.Product:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ProductClass_aspx;
                    break;
                case ModelType.Shop:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.ShopClass_aspx;
                    break;
                case ModelType.Ask:
                    TB_TemplateFileName.Text = BLL.HTMLBuilder.Templates.DefaultTemplate.AskClass_aspx;
                    break;

            }
            CBL_Fields.DataSource = new FieldManage().GetShopModelField(_ModelID);
            CBL_Fields.DataTextField = "Title";
            CBL_Fields.DataValueField = "FieldName";
            CBL_Fields.DataBind();
        }

        private void EditUiLoad()
        {
            this.Title = "修改栏目";
            BT_OK.Text = "修改";


            new ClassManage().BindDDL(DDL_Class, TopWinCMS.Model.Enum.Class_BindOption.NoLink, "<添加为一级栏目>", _ModelID);
            new ClassManage().BindDDL(DDL_CopyClass, TopWinCMS.Model.Enum.Class_BindOption.NoLink, "<-无->", _ModelID);

            ClassInfo model = new ClassInfo();
            model = MyClassBLL.EditLoad(int.Parse(Request["ID"]));
            DDL_Class.SelectedValue = Request["ID"];
            DDL_Class.Enabled = false;

            TB_ClassName.Text = model.ClassName;
            TB_ClassName_EN.Text = model.ClassName_EN;
            TB_TemplateFileName.Text = model.TemplateFileName;
            CB_IsShowInIndex.Checked = model.IsShowInIndex == 1 ? true : false;
            TB_PicPath.Text = model.PicPath;
            TB_LinkUrl.Text = model.LinkUrl;
            UC_AdminSelect1.LB_Selected.Text = model.ContentAdmin;
            UC_AdminSelect1.BT_SelectAdmin.Enabled = RightsCheck.HasRight("ClassControl", true);
            UC_AdminSelect2.LB_Selected.Text = model.ClassAdmin;
            UC_AdminSelect2.BT_SelectAdmin.Enabled = RightsCheck.HasRight("ClassControl", true);


            TB_FilePostfix.Text = model.FilePostfix;
            TB_FilePrefix.Text = model.FilePrefix;
            RBL_URLType.SelectedValue = model.URLStyle.ToString();
            TB_ClassFilePostfix.Text = model.ClassFilePostfix;
            TB_ClassFilePrefix.Text = model.ClassFilePrefix;
            RBL_ClassURLType.SelectedValue = model.ClassURLStyle.ToString();
            TB_ClassIntro.Text = model.ClassIntro;
            TB_ClassPic.Text = model.ClassPic;
            TB_ClassDescription.Text = model.ClassDescription;
            TB_ClassKeyWord.Text = model.ClassKeyWord;
            RBL_OpenGrade.SelectedValue = model.OpenGrade.ToString();
            RBL_EnableContribute.SelectedValue = model.EnableContribute.ToString();
            RBL_EnableComment.SelectedValue = model.EnableComment.ToString();



            CBL_Fields.DataTextField = "Title";
            CBL_Fields.DataValueField = "FieldName";
            CBL_Fields.DataSource = new FieldManage().GetShopModelField(_ModelID);
            CBL_Fields.DataBind();

            //加载被勾选的字段
            IList<GoodsCustomFieldsInfo> list = new GoodsCustomFieldsManage().GetFieldsList(int.Parse(Request["ID"]));
            foreach (GoodsCustomFieldsInfo g in list)
            {
                foreach (ListItem li in CBL_Fields.Items)
                {
                    if (li.Value == g.FieldName)
                        li.Selected = true;
                }

            }


        }

        protected void BT_OK_Click(object sender, System.EventArgs e)
        {
            if (ClassManage.CheckAllowClass(DDL_Class.SelectedValue, _ModelID, false) == false)
            {
                MsgBox.Alert("您不是该栏目的管理员！");

                return;
            }

            ClassInfo model = new ClassInfo();
            model.ID = int.Parse(DDL_Class.SelectedValue);
            model.ModelID =  _ModelID ;
            model.ClassName = TB_ClassName.Text.Trim();
            model.ClassName_EN = TB_ClassName_EN.Text.Trim();
            model.TemplateFileName = TB_TemplateFileName.Text.Trim();
            model.IsShowInIndex = CB_IsShowInIndex.Checked == true ? 1 : 0;
            model.PicPath = TB_PicPath.Text.Trim();
            model.LinkUrl = TB_LinkUrl.Text.Trim();
            model.ContentAdmin = UC_AdminSelect1.LB_Selected.Text.Trim();
            model.ClassAdmin = UC_AdminSelect2.LB_Selected.Text.Trim();

            model.FilePostfix = TB_FilePostfix.Text.Trim();
            model.FilePrefix = TB_FilePrefix.Text.Trim();
            model.URLStyle = int.Parse(RBL_URLType.SelectedValue);
            model.ClassFilePostfix = TB_ClassFilePostfix.Text.Trim();
            model.ClassFilePrefix = TB_ClassFilePrefix.Text.Trim();
            model.ClassURLStyle = int.Parse(this.RBL_ClassURLType.SelectedValue);
            model.ClassIntro = TB_ClassIntro.Text.Trim();
            model.ClassPic = TB_ClassPic.Text.Trim();
            model.ClassDescription = TB_ClassDescription.Text.Trim();
            model.ClassKeyWord = TB_ClassKeyWord.Text.Trim();
            model.OpenGrade = int.Parse(this.RBL_OpenGrade.SelectedValue);
            model.EnableContribute = int.Parse(this.RBL_EnableContribute.SelectedValue);
            model.EnableComment = int.Parse(this.RBL_EnableComment.SelectedValue);

            List<string> Fields = new List<string>();
            foreach (ListItem li in CBL_Fields.Items)
            {
                if (li.Selected)
                    Fields.Add(li.Value);
            }


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

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

        protected void DDL_CopyClass_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}