using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.CQMS.DataBase
{
    public partial class DataBaseEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Request.Params["type"];
                string dataTypeId = Request.Params["dataTypeId"];
                if (type == "edit")
                {
                    var data = BLL.DataTypeService.GetDataTypeById(dataTypeId);
                    if (data != null)
                    {
                        this.txtDataTypeName.Text = data.DataTypeName;
                        this.txtDataTypeCode.Text = data.DataTypeCode;
                        this.txtSortIndex.Text = data.SortIndex.HasValue ? data.SortIndex.ToString() : "";
                        this.txtPages.Text = data.Pages.HasValue ? data.Pages.ToString() : "";
                        this.txtRemark.Text = data.Remark;
                        this.drpIsRelatedWBS.SelectedValue = data.IsRelatedWBS == true ? "True" : "False";
                    }
                }
                else if (type == "add")
                {
                    var q = BLL.DataTypeService.GetDataTypesBySuperDataTypeId(dataTypeId);
                    this.txtSortIndex.Text = (q.Count() + 1).ToString();
                }
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string type = Request.Params["type"];
            string dataTypeId = Request.Params["dataTypeId"];            
            if (type == "edit")
            {
                Model.DataBase_DataType dataType = BLL.DataTypeService.GetDataTypeById(dataTypeId);
                //dataType.DataTypeId = dataTypeId;
                dataType.DataTypeCode = this.txtDataTypeCode.Text.Trim();
                dataType.DataTypeName = this.txtDataTypeName.Text.Trim();
                dataType.SortIndex = Funs.GetNewInt(this.txtSortIndex.Text.Trim());
                dataType.Pages = Funs.GetNewInt(this.txtPages.Text.Trim());
                dataType.Remark = this.txtRemark.Text.Trim();
                dataType.IsRelatedWBS = Convert.ToBoolean(this.drpIsRelatedWBS.SelectedValue);
                BLL.DataTypeService.UpdateDataType(dataType);
            }
            else if (type == "add")
            {
                Model.DataBase_DataType newDataType = new Model.DataBase_DataType();
                newDataType.DataTypeName = this.txtDataTypeName.Text.Trim();
                newDataType.DataTypeCode = this.txtDataTypeCode.Text.Trim();
                newDataType.SortIndex = Funs.GetNewInt(this.txtSortIndex.Text.Trim());
                newDataType.Pages = Funs.GetNewInt(this.txtPages.Text.Trim());
                newDataType.Remark = this.txtRemark.Text.Trim();
                newDataType.IsRelatedWBS = Convert.ToBoolean(this.drpIsRelatedWBS.SelectedValue);
                newDataType.SuperDataTypeId = dataTypeId;
                newDataType.DataTypeId = SQLHelper.GetNewID(typeof(Model.DataBase_DataType));
                BLL.DataTypeService.AddDataType(newDataType);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}