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.Model;
using TopWinCMS.Common;

namespace TopWinCMS.WebUI.Admin.Model
{
    public partial class Model_Add : TopWinCMS.WebFactory.PageBase
    {
        protected string SettingUploadPath = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    RightsCheck.CheckWorkPower("M_2");
                    BT_OK.Text = "修改";
                    DDL_BaseModel.Enabled = false;
                    Edit_Load();
                }
                else
                {
                    RightsCheck.CheckWorkPower("M_1");
                    BT_OK.Text = "添加"; InitFilter();
                }
            }
        }

        #region 添加或修改
        private void Add()
        {
            if (IsValid == false)
            {
                MsgBox.Alert("页面数据验证发现错误。", this.UpdatePanel1);
                return;
            }


            ModelInfo model = new ModelInfo();

            model.ModelName = TB_ModelName.Text.Trim();
            model.DataTable = TB_DataTable.Text.Trim();
            model.BaseModel = DDL_BaseModel.SelectedValue;
            model.ContentName = TB_ContentName.Text.Trim();
            model.ContentUnit = TB_ContentUnit.Text.Trim();
            //model.Filter = TB_Filter.Text.Trim();
            model.Remark = TB_Remark.Text.Trim();
            model.EnableStatic = int.Parse(RBL_EnableStatic.SelectedValue);
            model.HtmlSavePath = TB_HtmlSavePath.Text.Trim();
            model.MaxClassPageNo = int.Parse(NTB_MaxClassPageNo.Text);

            model.AdminUploadPath = string.Format("{0}|{1}", RBL_AdminUploadPath.SelectedValue, DDL_AdminUploadPath.SelectedValue);

            model.AdminUploadSize = int.Parse(NTB_AdminUploadSize.Text);
            model.AdminFileTypes = TB_AdminFileTypes.Text.Trim();
            model.EnableMemberUpload = int.Parse(RBL_EnableMemberUpload.SelectedValue);

            model.MemberUploadPath = string.Format("{0}|{1}", RBL_MemberUploadPath.SelectedValue, DDL_MemberUploadPath.SelectedValue);


            model.MemberUploadSize = int.Parse(NTB_MemberUploadSize.Text);
            model.MemberFileTypes = TB_MemberFileTypes.Text.Trim();
            model.AdminPass = int.Parse(RBL_AdminPass.SelectedValue);
            model.CommentAutoPass = int.Parse(RBL_CommentAutoPass.SelectedValue);
            model.Contribution = int.Parse(RBL_Contribution.SelectedValue);
            model.SearchOption = int.Parse(RBL_SearchOption.SelectedValue);
            model.NewSignKeep = int.Parse(NTB_NewSignKeep.Text);
            model.PageSize = int.Parse(NTB_PageSize.Text);
            model.PageSizeBackGround = int.Parse(NTB_PageSizeBackGround.Text);
            model.TemplateFileName = TB_TemplateFileName.Text.Trim();

            if (model.NewSignKeep == 0)
            {
                model.IsShowNewSign = 0;
            }
            else
            {
                model.IsShowNewSign = 1;
            }

            if (Request["ID"] == null)
            {
                model.ID = 0;
            }
            else if (Request.QueryString["ID"] != null && Request.QueryString["Action"] == "Edit")
            {
                model.ID = byte.Parse(Request["ID"]);
            }

            string strErr = null;
            new ModelManage().Add(model, DL_Filter, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr.ToString(), UpdatePanel1);

            }
            else
            {
                MsgBox.Alert("操作成功！", "Model_Manage.aspx", UpdatePanel1);
            }
        }
        #endregion

        #region 加载
        private void Edit_Load()
        {
            ModelInfo model = ModelManage.GetInfo( int.Parse(Request["ID"]));
            if (model != null)
            {
                this.Title = "修改" + model.ModelName;

                TB_ModelName.Text = model.ModelName;
                TB_DataTable.Text = model.DataTable;
                TB_DataTable.Enabled = false;
                DDL_BaseModel.SelectedValue = model.BaseModel;
                TB_ContentName.Text = model.ContentName;
                TB_ContentUnit.Text = model.ContentUnit;

                TB_Remark.Text = model.Remark;
                RBL_EnableStatic.SelectedValue = model.EnableStatic.ToString();
                TB_HtmlSavePath.Text = model.HtmlSavePath;
                TB_HtmlSavePath.Enabled = false;
                NTB_MaxClassPageNo.Text = model.MaxClassPageNo.ToString();

                string[] ARR = model.AdminUploadPath.Split('|');
                if (ARR.Length == 2)
                {
                    RBL_AdminUploadPath.SelectedValue = ARR[0];

                    DDL_AdminUploadPath.SelectedValue = ARR[1];
                }


                NTB_AdminUploadSize.Text = model.AdminUploadSize.ToString();
                TB_AdminFileTypes.Text = model.AdminFileTypes;
                RBL_EnableMemberUpload.SelectedValue = model.EnableMemberUpload.ToString();

                ARR = model.MemberUploadPath.Split('|');
                if (ARR.Length == 2)
                {
                    RBL_MemberUploadPath.SelectedValue = ARR[0];

                    DDL_MemberUploadPath.SelectedValue = ARR[1];
                }



                NTB_MemberUploadSize.Text = model.MemberUploadSize.ToString();
                TB_MemberFileTypes.Text = model.MemberFileTypes;
                RBL_AdminPass.SelectedValue = model.AdminPass.ToString();
                RBL_CommentAutoPass.SelectedValue = model.CommentAutoPass.ToString();
                RBL_Contribution.SelectedValue = model.Contribution.ToString();
                RBL_SearchOption.SelectedValue = model.SearchOption.ToString();
                NTB_PageSize.Text = model.PageSize.ToString();
                NTB_PageSizeBackGround.Text = model.PageSizeBackGround.ToString();
                TB_TemplateFileName.Text = model.TemplateFileName;

                new ModelManage().Filter(model.Filter, DL_Filter);

            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }
        #endregion

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void DDL_BaseModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDL_BaseModel.SelectedValue == "Shop" || DDL_BaseModel.SelectedValue == "Ask")
            {
                MsgBox.Alert("该模型不能新建！", UpdatePanel1);
                return;
            }
            InitFilter();
        }

        private void InitFilter()
        {
            string str = null;

            if (DDL_BaseModel.SelectedValue == "Soft")
            {
                str = "版本|SoftVersion|True;大小|SoftSize|True;置顶|OnTop|True;推荐|Elite|True;演示地址|DemoUrl|True;软件作者|Author|True;软件开发商电邮|Email|True;软件开发商主页|Homepage|True;含图|IncludePic|True;缩略图|PicUrl|True;运行的系统|RunAtSystem|True;软件类型|SoftType|True;语言|SoftLanguage|True;授权类型|CopyrightType|True;等级|Stars|True;解压密码|DecompressPassword|True;更新时间|UpdateTime|True;最后点击时间|LastHitTime|True;流氓软件警告|BlackGuard|True;审核|Pass|True;浏览等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;稿件|IsDraft|True;";

                TB_TemplateFileName.Text = TopWinCMS.BLL.HTMLBuilder.Templates.DefaultTemplate.SoftChannelIndex_aspx;
            }
            if (DDL_BaseModel.SelectedValue == "Article")
            {
                str = "专题|SpecialID|True;文章小标题|Title1|True;摘要|Brief|True;文章作者|Author|True;文章来源|From|True;责任编辑|Editor|True;关键字|Key|True;点击次数|Hits|True;更新时间|UpdateTime|True;置顶|OnTop|True;推荐|Elite|True;文章内容|Content|True;含图|IncludePic|True;缩略图链接|PicUrl|True;等级|Stars|True;摘要审核|Pass|True;等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;稿件|IsDraft|True;";

                TB_TemplateFileName.Text = TopWinCMS.BLL.HTMLBuilder.Templates.DefaultTemplate.ArticleChannelIndex_aspx;
            }

            if (DDL_BaseModel.SelectedValue == "Product")
            {
                str = "专题|SpecialID|True;价格|ProductPrice|True;规格|Standard|True;包装|Packaging|True;商标|Brand|True;置顶|OnTop|True;推荐|Elite|True;含图|IncludePic|True;缩略图|PicUrl|True;大图|BigPicUrl|True;等级|Stars|True;更新时间|UpdateTime|True;审核|Pass|True;浏览等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;稿件|IsDraft|True;";

                TB_TemplateFileName.Text = TopWinCMS.BLL.HTMLBuilder.Templates.DefaultTemplate.ProductChannelIndex_aspx;
            }




            if (string.IsNullOrEmpty(str) == false)
                new ModelManage().Filter(str, DL_Filter);
        }



    }
}