using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.Common.ProjectSet
{
    public partial class InstallationEdit : PageBase
    {
        #region 定义项
        /// <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.txtInstallationCode.Focus();
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
       
                this.InstallationId = Request.Params["InstallationId"];
                string projectId = Request.Params["projectId"];
                var pro = BLL.Base_ProjectService.GetProjectByProjectId(projectId);
                this.txtProject.Text = pro.ProjectName;
                if (!string.IsNullOrEmpty(this.InstallationId))
                {
                    Model.Project_Installation getInstallation = BLL.Project_InstallationService.GetProject_InstallationByInstallationId(this.InstallationId);
                    if (getInstallation != null)
                    {
                        this.txtInstallationCode.Text = getInstallation.InstallationCode;
                        this.txtInstallationName.Text = getInstallation.InstallationName;
                        this.txtRemark.Text = getInstallation.Remark;

                        BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, getInstallation.ProjectId, BLL.Const.UnitType_3,Resources.Lan.PleaseSelect);
                        this.drpSupervisorUnit.SelectedValue = getInstallation.SupervisorUnitId;
                    }
                }
                else
                {
                    BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, projectId, BLL.Const.UnitType_3,Resources.Lan.PleaseSelect);
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            string projectId = Request.Params["projectId"];
            this.drpSupervisorUnit.Items.Clear();
            BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, projectId, BLL.Const.UnitType_3, Resources.Lan.PleaseSelect);
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string projectId = Request.Params["projectId"];
            var q = Funs.DB.Project_Installation.FirstOrDefault(x => x.InstallationCode == this.txtInstallationCode.Text.Trim()
            && x.ProjectId == projectId && (x.InstallationId != this.InstallationId || (this.InstallationId == null && x.InstallationId != null)));
            if (q != null)
            {
                Alert.ShowInTop(Resources.Lan.InstallationCodeExists, MessageBoxIcon.Warning);
                return;
            }

            var q2 = Funs.DB.Project_Installation.FirstOrDefault(x => x.InstallationName == this.txtInstallationName.Text.Trim()
            && x.ProjectId == projectId && (x.InstallationId != this.InstallationId || (this.InstallationId == null && x.InstallationId != null)));
            if (q2 != null)
            {
                Alert.ShowInTop(Resources.Lan.InstallationNameExists, MessageBoxIcon.Warning);
                return;
            }

            Model.Project_Installation newInstallation = new Model.Project_Installation
            {
                InstallationCode = this.txtInstallationCode.Text.Trim(),
                InstallationName = this.txtInstallationName.Text.Trim(),
                ProjectId = projectId,
                SupervisorUnitId = this.drpSupervisorUnit.SelectedValue != BLL.Const._Null ? this.drpSupervisorUnit.SelectedValue : null,
                Remark = this.txtRemark.Text.Trim()
            };

            if (!string.IsNullOrEmpty(this.InstallationId))
            {
                newInstallation.InstallationId = this.InstallationId;
                BLL.Project_InstallationService.UpdateProject_Installation(newInstallation);
                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnModify, this.InstallationId);
            }
            else
            {
                this.InstallationId = SQLHelper.GetNewID(typeof(Model.Project_Installation));
                newInstallation.InstallationId = this.InstallationId;
                BLL.Project_InstallationService.AddProject_Installation(newInstallation);
                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnAdd, this.InstallationId);
            }

            ShowNotify(Resources.Lan.SaveSuccessfully, MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}