using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.Common.ProjectSet
{
    public partial class WorkAreaEdit : PageBase
    {
        #region 定义项
        /// <summary>
        /// 主键
        /// </summary>
        public string WorkAreaId
        {
            get
            {
                return (string)ViewState["WorkAreaId"];
            }
            set
            {
                ViewState["WorkAreaId"] = value;
            }
        }

        /// <summary>
        /// 项目主键
        /// </summary>
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }

        /// <summary>
        /// 装置主键
        /// </summary>
        public string InstallationId
        {
            get
            {
                return (string)ViewState["InstallationId"];
            }
            set
            {
                ViewState["InstallationId"] = value;
            }
        }
        #endregion

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtWorkAreaCode.Focus();
                btnClose.OnClientClick = ActiveWindow.GetHideReference();

                this.InstallationId = Request.Params["InstallationId"];
                Model.Project_Installation installation1 = BLL.Project_InstallationService.GetProject_InstallationByInstallationId(this.InstallationId);
                if (installation1 != null)
                {
                    this.ProjectId = installation1.ProjectId;
                }
                this.WorkAreaId = Request.Params["WorkAreaId"];
                if (!string.IsNullOrEmpty(this.WorkAreaId))
                {
                    Model.Project_WorkArea getWorkArea = BLL.Project_WorkAreaService.GetProject_WorkAreaByWorkAreaId(this.WorkAreaId);
                    if (getWorkArea != null)
                    {
                        this.txtWorkAreaCode.Text = getWorkArea.WorkAreaCode;
                        this.txtWorkAreaName.Text = getWorkArea.WorkAreaName;
                        this.txtEnWorkAreaName.Text = getWorkArea.EnWorkAreaName;
                        this.txtRemark.Text = getWorkArea.Remark;
                        this.ProjectId = getWorkArea.ProjectId;
                        this.InstallationId = getWorkArea.InstallationId;
                        BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpUnit, true, this.ProjectId, BLL.Const.UnitType_5,Resources.Lan.PleaseSelect);
                        BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, this.ProjectId, BLL.Const.UnitType_3,Resources.Lan.PleaseSelect);
                        if (!string.IsNullOrEmpty(getWorkArea.UnitId))
                        {
                            this.drpUnit.SelectedValue = getWorkArea.UnitId;
                        }
                        if (!string.IsNullOrEmpty(getWorkArea.SupervisorUnitId))
                        {
                            this.drpSupervisorUnit.SelectedValue = getWorkArea.SupervisorUnitId;
                        }
                    }
                }
                else
                {
                    BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpUnit, true, this.ProjectId, BLL.Const.UnitType_5,Resources.Lan.PleaseSelect);
                    BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, this.ProjectId, BLL.Const.UnitType_3,Resources.Lan.PleaseSelect);
                }
                Model.Base_Project project = BLL.Base_ProjectService.GetProjectByProjectId(this.ProjectId);
                if (project != null)
                {
                    this.txtProject.Text = project.ProjectName;
                }
                Model.Project_Installation installation = BLL.Project_InstallationService.GetProject_InstallationByInstallationId(this.InstallationId);
                if (installation != null)
                {
                    this.txtInstallation.Text = installation.InstallationName;
                }
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var q = Funs.DB.Project_WorkArea.FirstOrDefault(x => x.WorkAreaCode == this.txtWorkAreaCode.Text.Trim()
            && x.InstallationId == this.InstallationId && (x.WorkAreaId != this.WorkAreaId || (this.WorkAreaId == null && x.WorkAreaId != null)));
            if (q != null)
            {
                Alert.ShowInTop(Resources.Lan.CodeExists, MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnit.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop(Resources.Lan.PleaseSelectConstructionUnit, MessageBoxIcon.Warning);
                return;
            }
            Model.Project_WorkArea newWorkArea = new Model.Project_WorkArea
            {
                WorkAreaCode = this.txtWorkAreaCode.Text.Trim(),
                WorkAreaName = this.txtWorkAreaName.Text.Trim(),
                EnWorkAreaName = this.txtEnWorkAreaName.Text.Trim(),
                ProjectId = this.ProjectId,
                InstallationId = this.InstallationId,
                UnitId = this.drpUnit.SelectedValue,
                Remark = this.txtRemark.Text.Trim()
            };
            if (this.drpSupervisorUnit.SelectedValue != BLL.Const._Null)
            {
                newWorkArea.SupervisorUnitId = this.drpSupervisorUnit.SelectedValue;
            }

            if (!string.IsNullOrEmpty(this.WorkAreaId))
            {
                newWorkArea.WorkAreaId = this.WorkAreaId;
                BLL.Project_WorkAreaService.UpdateProject_WorkArea(newWorkArea);
                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnModify, this.WorkAreaId);
            }
            else
            {
                this.WorkAreaId = SQLHelper.GetNewID(typeof(Model.Project_WorkArea));
                newWorkArea.WorkAreaId = this.WorkAreaId;
                BLL.Project_WorkAreaService.AddProject_WorkArea(newWorkArea);
                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnAdd, this.WorkAreaId);  
            }
            ShowNotify(Resources.Lan.SaveSuccessfully, MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}