using BLL;
using BLL.CQMS.Comprehensive;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.Comprehensive
{
    public partial class FinalFileEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string FinalFileId
        {
            get
            {
                return (string)ViewState["FinalFileId"];
            }
            set
            {
                ViewState["FinalFileId"] = value;
            }
        }
        #endregion
        
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                this.FinalFileId = Request.Params["FinalFileId"];
                BLL.UnitService.InitUnitDownList(this.dpUnit, this.CurrUser.LoginProjectId, true);
                var punit = ProjectUnitService.GetProjectUnitByUnitIdProjectId(this.CurrUser.LoginProjectId, this.CurrUser.UnitId);
                if (CurrUser.UserId == Const.sysglyId || CurrUser.UnitId == Const.UnitId_TCC || punit.UnitType == Const.ProjectUnitType_3)
                {
                    BLL.UnitService.InitUnitDownList(this.dpUnit, this.CurrUser.LoginProjectId, true);
                }
                else
                {
                    dpUnit.SelectedValue = this.CurrUser.UnitId;
                    dpUnit.Enabled = false;
                }
                Model.Comprehensive_FinalFile finalFile = BLL.FinalFileService.getFinalFile(this.FinalFileId);
                if (finalFile != null)
                {
                    this.FinalFileId = finalFile.FinalFileId;
                    this.hdAttachUrl.Text = finalFile.FinalFileId;
                    if (!string.IsNullOrEmpty(finalFile.UnitId))
                    {
                        this.dpUnit.SelectedValue = finalFile.UnitId;
                    }
                    if (!string.IsNullOrEmpty(finalFile.InspectionCode)) {
                        this.txtInspectionCode.Text = finalFile.InspectionCode;
                    }
                    if (!string.IsNullOrEmpty(finalFile.InspectionName)) {
                        this.txtInspectionName.Text = finalFile.InspectionName;
                    }
                    if (!string.IsNullOrEmpty(finalFile.Remark))
                    {
                        this.txtRemark.Text = finalFile.Remark;
                    }
                    if (finalFile.InspectionDate.HasValue) {
                        this.dpDate.Text = finalFile.InspectionDate.Value.ToString("yyyy-MM-dd");
                    }
                }
                
            }
        }
        /// 
        /// 禁止编辑
        /// 
        public void Readonly()
        {
            this.txtInspectionCode.Readonly = true;
            this.txtInspectionName.Readonly = true;
            this.txtRemark.Readonly = true;
            this.dpDate.Readonly = true;
            this.dpUnit.Readonly = true;
        }
        #region 保存
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.Comprehensive_FinalFile finalFile = new Model.Comprehensive_FinalFile();
            finalFile.ProjectId = this.CurrUser.LoginProjectId;
            finalFile.InspectionCode = this.txtInspectionCode.Text.Trim();
            finalFile.InspectionName = this.txtInspectionName.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtRemark.Text))
            {
                finalFile.Remark = this.txtRemark.Text.Trim();
            }
            finalFile.InspectionDate = this.dpDate.SelectedDate;
            finalFile.UnitId = this.dpUnit.SelectedValue;
            if (string.IsNullOrEmpty(this.FinalFileId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    finalFile.FinalFileId = this.hdAttachUrl.Text;
                }
                else
                {
                    finalFile.FinalFileId = SQLHelper.GetNewID(typeof(Model.Comprehensive_SpecialEquipment));
                    this.hdAttachUrl.Text = finalFile.FinalFileId;
                }
                finalFile.CreateMan = this.CurrUser.UserId;
                finalFile.CreateDate = DateTime.Now;
                BLL.FinalFileService.AddFinalFile(finalFile);
            }
            else
            {
                finalFile.FinalFileId = this.FinalFileId;
                BLL.FinalFileService.UpdateFinalFile(finalFile);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdAttachUrl.Text))   //新增记录
            {
                this.hdAttachUrl.Text = SQLHelper.GetNewID(typeof(Model.Comprehensive_SpecialEquipment));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/specialEquipment&menuId={1}", this.hdAttachUrl.Text, BLL.Const.FinalFileMenuId)));
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == BLL.Const._Null)
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.FinalFileMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}