using BLL;
using BLL.CQMS.Comprehensive;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.Comprehensive
{
    public partial class SpecialEquipmentEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string SpecialEquipmentId
        {
            get
            {
                return (string)ViewState["SpecialEquipmentId"];
            }
            set
            {
                ViewState["SpecialEquipmentId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                this.SpecialEquipmentId = Request.Params["SpecialEquipmentId"];
                BLL.UnitService.InitUnitDownList(this.drpUnit, this.CurrUser.LoginProjectId, true);
                BLL.SpecialEquipmentService.InitSpecialEquipmentDropDownList(this.drpEquipmentId, true, true);
                LoadAuditSelect();
                this.agree.Hidden = true;
                this.options.Hidden = true;
                
                this.btnSave.Hidden = true;
                this.btnSubmit.Hidden = true;
                Model.Comprehensive_SpecialEquipment specialEquipment = BLL.SpecialEquipmentServices.GetEquipmentById(this.SpecialEquipmentId);
                if (specialEquipment != null)
                {
                    this.SpecialEquipmentId = specialEquipment.SpecialEquipmentId;
                    if (!string.IsNullOrEmpty(specialEquipment.UnitId))
                    {
                        this.drpUnit.SelectedValue = specialEquipment.UnitId;
                    }
                    if (!string.IsNullOrEmpty(specialEquipment.EquipmentId))
                    {
                        this.drpEquipmentId.SelectedValue = specialEquipment.EquipmentId;
                    }
                    this.txtInformNumber.Text = specialEquipment.InformNumber.HasValue ? specialEquipment.InformNumber.ToString() : "";
                    this.txtMonitoringReportNumber.Text = specialEquipment.MonitoringReportNumber.HasValue ? specialEquipment.MonitoringReportNumber.ToString() : "";
                    this.txtSubmitDataNumber.Text = specialEquipment.SubmitDataNumber.HasValue ? specialEquipment.SubmitDataNumber.ToString() : "";
                    this.txtSunNumber.Text = specialEquipment.SunNumber.HasValue ? specialEquipment.SunNumber.ToString() : "";
                    var currApprove = SpecialEquipmentApproveService.GetCurrentApprove(specialEquipment.SpecialEquipmentId);
                    if (currApprove != null)
                    {
                        this.drpAudit.SelectedValue = currApprove.ApproveMan;
                        //重新编制  编制人 可以 显示 提交 保存按钮
                        if (currApprove.ApproveType == BLL.Const.Comprehensive_ReCompile && specialEquipment.CompileMan == CurrUser.UserId)
                        {
                            this.btnSubmit.Hidden = false;
                            this.btnSave.Hidden = false;
                        }//审核状态 审核人 可以显示  提交 保存按钮
                        else if (currApprove.ApproveType == BLL.Const.Comprehensive_Audit && currApprove.ApproveMan == CurrUser.UserId)
                        {
                            //审核状态不可编辑
                            Readonly();
                            this.agree.Hidden = false;
                            this.options.Hidden = false;
                            this.btnSubmit.Hidden = false;
                            this.btnSave.Hidden = false;
                        }
                    }//没有当前审核人,已完成状态 或者 待提交状态
                    else
                    {
                        if (specialEquipment.Status == BLL.Const.Comprehensive_Compile && specialEquipment.CompileMan == CurrUser.UserId)
                        {
                            this.btnSubmit.Hidden = false;
                            this.btnSave.Hidden = false;
                        }
                    }
                }
                else
                {
                    this.btnSave.Hidden = false;
                    this.btnSubmit.Hidden = false;
                }
            }
        }
        /// 
        /// 禁止编辑
        /// 
        public void Readonly()
        {
            this.txtInformNumber.Readonly = true;
            this.txtMonitoringReportNumber.Readonly = true;
            this.txtSubmitDataNumber.Readonly = true;
            this.txtSunNumber.Readonly = true;
            this.drpEquipmentId.Readonly = true;
            this.drpUnit.Readonly = true;
            this.drpAudit.Readonly = true;
      //      this.btnAttach.Enabled = false; 
        }
        /// 
        /// 加载专业工程师
        /// 
        public void LoadAuditSelect()
        {
            var db = Funs.DB;
            var userList = from x in db.Sys_User
                           join y in db.Project_ProjectUnit
                           on x.UnitId equals y.UnitId
                           join p in db.Project_ProjectUser
                           on x.UserId equals p.UserId
                           where y.UnitId == Const.UnitId_TCC && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                           where p.RoleId.Contains(Const.ZBCNEngineer)
                           select new { UserId = x.UserId, UserName = x.UserName };
            drpAudit.DataValueField = "UserId";
            drpAudit.DataTextField = "UserName";
            this.drpAudit.DataSource = userList.ToList();
            this.drpAudit.DataBind();
        }
        #endregion
        #region 保存
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (drpUnit.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择所属单位!", MessageBoxIcon.Warning);
                return;
            }
            Model.Comprehensive_SpecialEquipment specialEquipment = new Model.Comprehensive_SpecialEquipment();
            specialEquipment.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(this.drpAudit.SelectedValue))
            {
                specialEquipment.AuditMan = drpAudit.SelectedValue;
            }   //审核人
            if (!string.IsNullOrEmpty(this.txtSunNumber.Text))
            {
                specialEquipment.SunNumber = Convert.ToInt32(this.txtSunNumber.Text);
            }
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                specialEquipment.UnitId = this.drpUnit.SelectedValue;
            }
            if (this.drpEquipmentId.SelectedValue != BLL.Const._Null)
            {
                specialEquipment.EquipmentId = this.drpEquipmentId.SelectedValue;
            }
            if (!string.IsNullOrEmpty(this.txtSubmitDataNumber.Text))
            {
                specialEquipment.SubmitDataNumber = Convert.ToInt32(this.txtSubmitDataNumber.Text);
            }
            if (!string.IsNullOrEmpty(this.txtMonitoringReportNumber.Text))
            {
                specialEquipment.MonitoringReportNumber = Convert.ToInt32(this.txtMonitoringReportNumber.Text);
            }
            if (!string.IsNullOrEmpty(this.txtInformNumber.Text))
            {
                specialEquipment.InformNumber = Convert.ToInt32(this.txtInformNumber.Text);
            }
            if (string.IsNullOrEmpty(this.SpecialEquipmentId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    specialEquipment.SpecialEquipmentId = this.hdAttachUrl.Text;
                }
                else
                {
                    specialEquipment.SpecialEquipmentId = SQLHelper.GetNewID(typeof(Model.Comprehensive_SpecialEquipment));
                    this.hdAttachUrl.Text = specialEquipment.SpecialEquipmentId;
                }
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == specialEquipment.SpecialEquipmentId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                specialEquipment.CompileMan = this.CurrUser.UserId;
                specialEquipment.Status = BLL.Const.Comprehensive_Compile;
                BLL.SpecialEquipmentServices.AddEquipment(specialEquipment);
            }
            else
            {
                specialEquipment.SpecialEquipmentId = this.SpecialEquipmentId;
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.SpecialEquipmentId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                var model = Funs.DB.Comprehensive_SpecialEquipment.Where(u => u.SpecialEquipmentId == this.SpecialEquipmentId).FirstOrDefault();
                if (model != null)
                {
                    specialEquipment.Status = model.Status;
                }
                BLL.SpecialEquipmentServices.UpdateEquipment(specialEquipment);
            }
              
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (drpUnit.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择所属单位!", MessageBoxIcon.Warning);
                return;
            }
            Model.Comprehensive_SpecialEquipment specialEquipment = new Model.Comprehensive_SpecialEquipment();
            specialEquipment.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(this.drpAudit.SelectedValue))
            {
                specialEquipment.AuditMan = drpAudit.SelectedValue;
            }   //审核人
            if (!string.IsNullOrEmpty(this.txtSunNumber.Text))
            {
                specialEquipment.SunNumber = Convert.ToInt32(this.txtSunNumber.Text);
            }
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                specialEquipment.UnitId = this.drpUnit.SelectedValue;
            }
            if (this.drpEquipmentId.SelectedValue != BLL.Const._Null)
            {
                specialEquipment.EquipmentId = this.drpEquipmentId.SelectedValue;
            }
            if (!string.IsNullOrEmpty(this.txtSubmitDataNumber.Text))
            {
                specialEquipment.SubmitDataNumber = Convert.ToInt32(this.txtSubmitDataNumber.Text);
            }
            if (!string.IsNullOrEmpty(this.txtMonitoringReportNumber.Text))
            {
                specialEquipment.MonitoringReportNumber = Convert.ToInt32(this.txtMonitoringReportNumber.Text);
            }
            if (!string.IsNullOrEmpty(this.txtInformNumber.Text))
            {
                specialEquipment.InformNumber = Convert.ToInt32(this.txtInformNumber.Text);
            }
            if (string.IsNullOrEmpty(this.SpecialEquipmentId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    specialEquipment.SpecialEquipmentId = this.hdAttachUrl.Text;
                }
                else
                {
                    specialEquipment.SpecialEquipmentId = SQLHelper.GetNewID(typeof(Model.Comprehensive_SpecialEquipment));
                    this.hdAttachUrl.Text = specialEquipment.SpecialEquipmentId;
                }
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == specialEquipment.SpecialEquipmentId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                specialEquipment.CompileMan = this.CurrUser.UserId;
                specialEquipment.Status = BLL.Const.Comprehensive_Audit;
                BLL.SpecialEquipmentServices.AddEquipment(specialEquipment);
            }
            else
            {
                specialEquipment.SpecialEquipmentId = this.SpecialEquipmentId;
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.SpecialEquipmentId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
               //  BLL.SpecialEquipmentServices.UpdateEquipment(specialEquipment);
                var oldSpecialEquipment = Funs.DB.Comprehensive_SpecialEquipment.Where(u => u.SpecialEquipmentId == this.SpecialEquipmentId).FirstOrDefault();
                if (oldSpecialEquipment == null) //数据库没有记录 直接点提交 当前状态为 审核状态
                {
                    specialEquipment.CompileMan = this.CurrUser.UserId;
                    //specialEquipment.CompileDate = DateTime.Now;
                    specialEquipment.Status = BLL.Const.Comprehensive_Audit;
                    BLL.SpecialEquipmentServices.AddEquipment(specialEquipment);
                }
                else
                {
                    specialEquipment.CompileMan = oldSpecialEquipment.CompileMan;
                    //specialEquipment.CompileDate = oldSpecialEquipment.CompileDate;
                    if (oldSpecialEquipment.Status == BLL.Const.Comprehensive_Compile)//编制状态提交变审核
                    {
                        specialEquipment.Status = BLL.Const.Comprehensive_Audit;
                    }
                    else if (oldSpecialEquipment.Status == BLL.Const.Comprehensive_ReCompile)//重新编制状态提交变审核
                    {
                        specialEquipment.Status = BLL.Const.Comprehensive_Audit;
                    }
                    else //审核状态  提交 变 完成 或者 重新编制
                    {
                        if (Convert.ToBoolean(rblIsAgree.SelectedValue))
                        {
                            specialEquipment.Status = BLL.Const.Comprehensive_Complete;
                        }
                        else
                        {
                            specialEquipment.Status = BLL.Const.Comprehensive_ReCompile;
                        }
                    }
                     
                    BLL.SpecialEquipmentServices.UpdateEquipment(specialEquipment);
                }
            }
            #region 审核记录
            var currApprove = SpecialEquipmentApproveService.GetCurrentApprove(specialEquipment.SpecialEquipmentId);
            if (currApprove == null) //为获取到为 当前编制状态 直接提交
            {
                var approve = new Model.Comprehensive_SpecialEquipmentApprove();
                approve.SpecialEquipmentId = specialEquipment.SpecialEquipmentId;
                approve.ApproveMan = this.CurrUser.UserId;
                approve.ApproveType = Const.Comprehensive_Compile;
                approve.ApproveDate = DateTime.Now;
                SpecialEquipmentApproveService.EditApprove(approve);  //新增编制记录
                Model.Comprehensive_SpecialEquipmentApprove newApprove = new Model.Comprehensive_SpecialEquipmentApprove();
                newApprove.SpecialEquipmentId = specialEquipment.SpecialEquipmentId;
                newApprove.ApproveMan = this.drpAudit.SelectedValue;
                newApprove.ApproveType = Const.InspectionManagement_Audit;
                SpecialEquipmentApproveService.EditApprove(newApprove); //新增专业工程师审核记录
            }
            else if (currApprove.ApproveMan == CurrUser.UserId)
            {
                if (currApprove.ApproveType == BLL.Const.Comprehensive_ReCompile)
                {
                    currApprove.ApproveDate = DateTime.Now;
                    //currApprove.IsAgree = true;
                    SpecialEquipmentApproveService.EditApprove(currApprove); //新增专业工程师审核记录
                    Model.Comprehensive_SpecialEquipmentApprove newApprove = new Model.Comprehensive_SpecialEquipmentApprove();
                    newApprove.SpecialEquipmentId = specialEquipment.SpecialEquipmentId;
                    newApprove.ApproveMan = this.drpAudit.SelectedValue;
                    newApprove.ApproveType = Const.InspectionManagement_Audit;
                    SpecialEquipmentApproveService.EditApprove(newApprove); //新增专业工程师审核记录
                }
                else
                {
                    currApprove.ApproveDate = DateTime.Now;  //更新审核时间
                    currApprove.IsAgree = Convert.ToBoolean(rblIsAgree.SelectedValue);
                    currApprove.ApproveIdea = this.txtidea.Text;
                    SpecialEquipmentApproveService.EditApprove(currApprove);
                    if (Convert.ToBoolean(rblIsAgree.SelectedValue))  //同意时 审批完成
                    {
                        Model.Comprehensive_SpecialEquipmentApprove reApprove = new Model.Comprehensive_SpecialEquipmentApprove();
                        reApprove.SpecialEquipmentId = currApprove.SpecialEquipmentId;
                        reApprove.ApproveDate = DateTime.Now.AddSeconds(10);
                        reApprove.ApproveMan = CurrUser.UserId;
                        reApprove.ApproveType = Const.Comprehensive_Complete;
                        reApprove.ApproveIdea = txtidea.Text;
                        SpecialEquipmentApproveService.EditApprove(reApprove);
                    }
                    else
                    {
                        Model.Comprehensive_SpecialEquipmentApprove reApprove = new Model.Comprehensive_SpecialEquipmentApprove();
                        reApprove.SpecialEquipmentId = currApprove.SpecialEquipmentId;
                        reApprove.ApproveMan = specialEquipment.CompileMan;
                        reApprove.ApproveType = Const.Comprehensive_ReCompile;
                        SpecialEquipmentApproveService.EditApprove(reApprove);
                    }
                }
            }
            #endregion
            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));
            }
            Model.Comprehensive_SpecialEquipment specialEquipment = BLL.SpecialEquipmentServices.GetEquipmentById(this.SpecialEquipmentId);
            if (specialEquipment == null || ((specialEquipment.CompileMan == CurrUser.UserId && specialEquipment.Status == BLL.Const.Comprehensive_Compile) || (specialEquipment.CompileMan == CurrUser.UserId && specialEquipment.Status == BLL.Const.Comprehensive_ReCompile)))
            {
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/specialEquipment&menuId={1}", this.hdAttachUrl.Text, BLL.Const.SpecialEquipmentMenuId)));
            }
            else
            {
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=-1&toKeyId={0}&path=FileUpload/CQMS/specialEquipment&menuId={1}", this.hdAttachUrl.Text, BLL.Const.SpecialEquipmentMenuId)));
            }
        }
        #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.SpecialEquipmentsMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}