using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.EditorManage
{
    public partial class SQIBEditorEdit : PageBase
    {
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();//权限设置
                }
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string eprojectId = Request.Params["EProjectId"];
                if (!string.IsNullOrEmpty(eprojectId))
                {
                    Model.Editor_EProject ep = BLL.EProjectService.GeteProjectById(eprojectId);
                    if (ep != null)
                    {
                        txtSQIB_PressureVessel.Text = ep.SQIB_PressureVessel == null ? "" : Convert.ToDateTime(ep.SQIB_PressureVessel).ToString("yyyy-MM-dd");
                        txtSQIB_PressurePiping.Text = ep.SQIB_PressurePiping == null ? "" : Convert.ToDateTime(ep.SQIB_PressurePiping).ToString("yyyy-MM-dd");
                        txtSQIB_SQIB.Text = ep.SQIB_SQIB == null ? "" : Convert.ToDateTime(ep.SQIB_SQIB).ToString("yyyy-MM-dd");
                    }
                }
                this.txtSQIB_PressureVessel.MaxDate = DateTime.Now;
                this.txtSQIB_PressurePiping.MaxDate = DateTime.Now;
                this.txtSQIB_SQIB.MaxDate = DateTime.Now;
            }
        }
        #endregion

        #region 保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string operationLog = string.Empty;
            string eprojectId = Request.Params["EProjectId"];
            if (!string.IsNullOrEmpty(eprojectId))
            {
                var eproject = BLL.EProjectService.GeteProjectById(eprojectId);
                if (eproject != null)
                {
                    #region 日志
                    operationLog = eproject.ProjectControl_JobNo + "_SQIB:" ;
                    if (eproject.SQIB_PressureVessel != Funs.GetNewDateTime(this.txtSQIB_PressureVessel.Text.Trim()))
                    {
                        operationLog += "Pressure Vessel 由" + (eproject.SQIB_PressureVessel == null ? "" : string.Format("{0:yyyy-MM-dd}", eproject.SQIB_PressureVessel)) + "修改为" + this.txtSQIB_PressureVessel.Text.Trim() + ",";
                    }
                    if (eproject.SQIB_PressurePiping != Funs.GetNewDateTime(this.txtSQIB_PressurePiping.Text.Trim()))
                    {
                        operationLog += "Pressure Piping 由" + (eproject.SQIB_PressurePiping == null ? "" : string.Format("{0:yyyy-MM-dd}", eproject.SQIB_PressurePiping)) + "修改为" + this.txtSQIB_PressurePiping.Text.Trim() + ",";
                    }
                    if (eproject.SQIB_SQIB != Funs.GetNewDateTime(this.txtSQIB_SQIB.Text.Trim()))
                    {
                        operationLog += "QIB 由" + (eproject.SQIB_SQIB == null ? "" : string.Format("{0:yyyy-MM-dd}", eproject.SQIB_SQIB)) + "修改为" + this.txtSQIB_SQIB.Text.Trim() + ",";
                    }
                    operationLog += "操作成功!";
                    #endregion
                    
                    eproject.EProjectId = eprojectId;
                    eproject.SQIB_PressureVessel = Funs.GetNewDateTime(txtSQIB_PressureVessel.Text.Trim());
                    eproject.SQIB_PressurePiping = Funs.GetNewDateTime(txtSQIB_PressurePiping.Text.Trim());
                    eproject.SQIB_SQIB = Funs.GetNewDateTime(txtSQIB_SQIB.Text.Trim());
                    eproject.ModifyDate = DateTime.Now;
                    eproject.ModifyPerson = CurrUser.UserId;
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, operationLog);

                    BLL.EProjectService.UpdateProject(eproject, "SQIB");
                    ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.SQIBEditorMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}