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.Field
{
    public partial class Field_Add : System.Web.UI.Page
    {
        private int iModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
             
            RightsCheck.CheckWorkPower("M_5");

            FieldManage MyClassBLL = new FieldManage();
            iModelID = int.Parse(Request["ModelID"]);

            if (iModelID == 5)
            {
                ErrClass.Redirect("问答系统不支持自定义字段。");
            }

            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    //“修改”界面
                    BT_OK.Text = "修改";
                    Edit_Load();
                    string strPath = string.Format("{0} → 字段管理 → 修改字段", ModelManage.GetModelName(iModelID));
                    LB_Path.Text = strPath;
                }
                else
                {
                    //“添加”界面
                    BT_OK.Text = "添加";
                    string strPath = string.Format("{0} → 字段管理 → 添加字段", ModelManage.GetModelName(iModelID));
                    LB_Path.Text = strPath;
                }


            }
        }

        #region 添加或修改
        private void Add()
        {
            FieldInfo model = new FieldInfo();

            model.ModelID = iModelID;
            model.FieldName = TB_FieldName.Text.Trim();
            model.Title = TB_Title.Text.Trim();
            model.Tips = TB_Tips.Text.Trim();
            model.FieldType = DDL_FieldType.SelectedValue;
            model.DefaultValue = TB_DefaultValue.Text.Trim();
            model.ControlType = DDL_ControlType.SelectedValue;
            model.IsShowInMember = int.Parse(RBL_IsShowInMember.SelectedValue);
            model.Indispensable = int.Parse(RBL_Indispensable.SelectedValue);
            model.Width = int.Parse(TB_Width.Text);
            model.MaxFieldLength = int.Parse(TB_MaxFieldLength.Text);
            model.Stop = int.Parse(RBL_Stop.SelectedValue);

            if (Request["ID"] == null)
            {
                model.ID = 0;
            }
            else
            {
                model.ID = int.Parse(Request["ID"]);
            }

            string strErr = null;

            new FieldManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
                return;
            }
            MsgBox.Alert("操作成功！", "Field_Manage.aspx?ModelID=" + iModelID, UpdatePanel1);
        }
        #endregion

        #region 加载
        private void Edit_Load()
        {
            FieldInfo model = new FieldManage().EditLoad(Request["ID"]);
            if (model != null)
            {
                TB_FieldName.Text = model.FieldName;

                TB_Title.Text = model.Title;
                TB_Tips.Text = model.Tips;
                DDL_FieldType.SelectedValue = model.FieldType;
                TB_DefaultValue.Text = model.DefaultValue;
                DDL_ControlType.SelectedValue = model.ControlType;
                RBL_IsShowInMember.SelectedValue = model.IsShowInMember.ToString();
                RBL_Indispensable.SelectedValue = model.Indispensable.ToString();
                TB_Width.Text = model.Width.ToString();
                TB_MaxFieldLength.Text = model.MaxFieldLength.ToString();
                RBL_Stop.SelectedValue = model.Stop.ToString();

                TB_FieldName.Enabled = false;
                DDL_FieldType.Enabled = false;
                TB_MaxFieldLength.Enabled = false;

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

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