using BLL;
using BLL.CQMS.Comprehensive;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.Comprehensive
{
    public partial class DataReceivingEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string DataReceivingId
        {
            get
            {
                return (string)ViewState["DataReceivingId"];
            }
            set
            {
                ViewState["DataReceivingId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                this.DataReceivingId = Request.Params["DataReceivingId"];
                BLL.UnitService.InitUnitDownList(this.drpUnitIds, this.CurrUser.LoginProjectId, false);
                LoadAuditSelect();
                this.agree.Hidden = true;
                this.options.Hidden = true;
                
                this.btnSave.Hidden = true;
                this.btnSubmit.Hidden = true;
                Model.Comprehensive_DataReceiving dataReceiving = BLL.DataReceivingService.GetDataReceivingById(this.DataReceivingId);
                if (dataReceiving != null)
                {
                    this.DataReceivingId = dataReceiving.DataReceivingId;
                    this.hdAttachUrl.Text = this.DataReceivingId;
                    this.txtFileCode.Text = dataReceiving.FileCode;
                    this.txtFileName.Text = dataReceiving.FileName;
                    if (dataReceiving.DataReceivingDate != null)
                    {
                        this.txtDataReceivingDate.Text = string.Format("{0:yyyy-MM-dd}", dataReceiving.DataReceivingDate);
                    }
                    if (!string.IsNullOrEmpty(dataReceiving.SendUnit))
                    {
                        this.drpUnitIds.SelectedValueArray = dataReceiving.SendUnit.Split(',');
                    }
                    this.txtSendMan.Text = dataReceiving.SendMan;
                    if (!string.IsNullOrEmpty(dataReceiving.FileType))
                    {
                        this.drpFileType.SelectedValue = dataReceiving.FileType;
                    }
                    if (dataReceiving.CopiesCount != null)
                    {
                        this.txtCopiesCount.Text = Convert.ToString(dataReceiving.CopiesCount);
                    }
                    this.txtReceivingMan.Text = dataReceiving.ReceivingMan;
                    this.txtFileHandler.Text = dataReceiving.FileHandler;
                    if (dataReceiving.IsReply == true)
                    {
                        this.rblIsReply.SelectedValue = "true";
                    }
                    else
                    {
                        this.rblIsReply.SelectedValue = "false";
                    }
                    this.txtRemark.Text = dataReceiving.Remark;
                    var currApprove = DataReceivingApproveService.GetCurrentApprove(dataReceiving.DataReceivingId);
                    if (currApprove != null)
                    {
                        this.drpAudit.SelectedValue = currApprove.ApproveMan;
                        //重新编制  编制人 可以 显示 提交 保存按钮
                        if (currApprove.ApproveType == BLL.Const.Comprehensive_ReCompile && dataReceiving.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 (dataReceiving.Status == BLL.Const.Comprehensive_Compile && dataReceiving.CompileMan == CurrUser.UserId)
                        {
                            this.btnSubmit.Hidden = false;
                            this.btnSave.Hidden = false;
                        }
                    }
                }
                else {
                    this.btnSave.Hidden = false;
                    this.btnSubmit.Hidden = false;
                }
            }
        }
        #endregion
        /// 
        /// 禁止编辑
        /// 
        public void Readonly()
        {
            this.txtCopiesCount.Readonly = true;
            this.txtDataReceivingDate.Readonly = true;
   
            this.txtReceivingMan.Readonly = true;
            this.txtRemark.Readonly = true;
            this.txtSendMan.Readonly = true;
            this.drpAudit.Readonly = true;
            this.drpFileType.Readonly = true;
            this.drpUnitIds.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_CWCEC && 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();
        }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.Comprehensive_DataReceiving dataReceiving = new Model.Comprehensive_DataReceiving();
            dataReceiving.ProjectId = this.CurrUser.LoginProjectId;
            dataReceiving.FileCode = this.txtFileCode.Text.Trim();
            dataReceiving.FileName = this.txtFileName.Text.Trim();
            dataReceiving.DataReceivingDate = Funs.GetNewDateTime(this.txtDataReceivingDate.Text);
            dataReceiving.SendMan = this.txtSendMan.Text.Trim();
            dataReceiving.FileType = this.drpFileType.SelectedValue;
            dataReceiving.CopiesCount = Funs.GetNewInt(this.txtCopiesCount.Text.Trim());
            dataReceiving.ReceivingMan = this.txtReceivingMan.Text.Trim();
            dataReceiving.FileHandler = this.txtFileHandler.Text.Trim();
            dataReceiving.IsReply = Convert.ToBoolean(this.rblIsReply.SelectedValue);
            dataReceiving.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(this.drpAudit.SelectedValue))
            {
                dataReceiving.AuditMan = drpAudit.SelectedValue;
            }   //审核人
            string unitIds = string.Empty;
            var unitList = this.drpUnitIds.SelectedValueArray;
            foreach (var item in unitList)
            {
                unitIds += item + ",";
            }
            if (!string.IsNullOrEmpty(unitIds))
            {
                unitIds = unitIds.Substring(0, unitIds.LastIndexOf(","));
            }
            dataReceiving.SendUnit = unitIds;
            if (string.IsNullOrEmpty(this.DataReceivingId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    dataReceiving.DataReceivingId = this.hdAttachUrl.Text;
                }
                else
                {
                    dataReceiving.DataReceivingId = SQLHelper.GetNewID(typeof(Model.Comprehensive_DataReceiving));
                    this.hdAttachUrl.Text = dataReceiving.DataReceivingId;
                }
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataReceiving.DataReceivingId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                dataReceiving.CompileMan = this.CurrUser.UserId;
                dataReceiving.CompileDate = DateTime.Now; 
                 dataReceiving.Status = BLL.Const.Comprehensive_Audit;
                BLL.DataReceivingService.AddDataReceiving(dataReceiving);
            }
            else
            {
                dataReceiving.DataReceivingId = this.DataReceivingId;
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.DataReceivingId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                dataReceiving.Status = BLL.Const.Comprehensive_Audit;
                var model = Funs.DB.Comprehensive_DataReceiving.Where(u => u.DataReceivingId == this.DataReceivingId).FirstOrDefault();
                if (model.Status == BLL.Const.Comprehensive_Compile)//编制状态提交变审核
                {
                    dataReceiving.Status = BLL.Const.Comprehensive_Audit;
                }
                else if (model.Status == BLL.Const.Comprehensive_ReCompile)//重新编制状态提交变审核
                {
                    dataReceiving.Status = BLL.Const.Comprehensive_Audit;
                }
                else //审核状态  提交 变 完成 或者 重新编制
                {
                    if (Convert.ToBoolean(rblIsAgree.SelectedValue))
                    {
                        dataReceiving.Status = BLL.Const.Comprehensive_Complete;
                    }
                    else
                    {
                        dataReceiving.Status = BLL.Const.Comprehensive_ReCompile;
                    }
                }
                BLL.DataReceivingService.UpdateDataReceiving(dataReceiving);
            }
             
            #region 审核记录
            var currApprove = DataReceivingApproveService.GetCurrentApprove(dataReceiving.DataReceivingId);
            if (currApprove == null) //为获取到为 当前编制状态 直接提交
            {
                var approve = new Model.Comprehensive_DataReceivingApprove();
                approve.DataReceivingId = dataReceiving.DataReceivingId;
                approve.ApproveMan = this.CurrUser.UserId;
                approve.ApproveType = Const.Comprehensive_Compile;
                approve.ApproveDate = DateTime.Now;
                DataReceivingApproveService.EditApprove(approve);  //新增编制记录
                Model.Comprehensive_DataReceivingApprove newApprove = new Model.Comprehensive_DataReceivingApprove();
                newApprove.DataReceivingId = dataReceiving.DataReceivingId;
                newApprove.ApproveMan = this.drpAudit.SelectedValue;
                newApprove.ApproveType = Const.InspectionManagement_Audit;
                DataReceivingApproveService.EditApprove(newApprove); //新增专业工程师审核记录
            }
            else if (currApprove.ApproveMan == CurrUser.UserId)
            {
                if (currApprove.ApproveType == BLL.Const.Comprehensive_ReCompile)
                {
                    currApprove.ApproveDate = DateTime.Now;
                    //currApprove.IsAgree = true;
                    DataReceivingApproveService.EditApprove(currApprove); //新增专业工程师审核记录
                    Model.Comprehensive_DataReceivingApprove newApprove = new Model.Comprehensive_DataReceivingApprove();
                    newApprove.DataReceivingId = dataReceiving.DataReceivingId;
                    newApprove.ApproveMan = this.drpAudit.SelectedValue;
                    newApprove.ApproveType = Const.InspectionManagement_Audit;
                    DataReceivingApproveService.EditApprove(newApprove); //新增专业工程师审核记录
                }
                else
                {
                    currApprove.ApproveDate = DateTime.Now;  //更新审核时间
                    currApprove.IsAgree = Convert.ToBoolean(rblIsAgree.SelectedValue);
                    currApprove.ApproveIdea = this.txtidea.Text;
                    DataReceivingApproveService.EditApprove(currApprove);
                    if (Convert.ToBoolean(rblIsAgree.SelectedValue))  //同意时 审批完成
                    {
                        Model.Comprehensive_DataReceivingApprove reApprove = new Model.Comprehensive_DataReceivingApprove();
                        reApprove.DataReceivingId = currApprove.DataReceivingId;
                        reApprove.ApproveDate = DateTime.Now.AddSeconds(10);
                        reApprove.ApproveMan = CurrUser.UserId;
                        reApprove.ApproveType = Const.Comprehensive_Complete;
                        //reApprove.ApproveIdea = txtidea.Text;
                        DataReceivingApproveService.EditApprove(reApprove);
                    }
                    else
                    {
                        Model.Comprehensive_DataReceivingApprove reApprove = new Model.Comprehensive_DataReceivingApprove();
                        reApprove.DataReceivingId = currApprove.DataReceivingId;
                        reApprove.ApproveMan = dataReceiving.CompileMan;
                        reApprove.ApproveType = Const.Comprehensive_ReCompile;
                        DataReceivingApproveService.EditApprove(reApprove);
                    }
                }
            }
            #endregion
             
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.Comprehensive_DataReceiving dataReceiving = new Model.Comprehensive_DataReceiving();
            dataReceiving.ProjectId = this.CurrUser.LoginProjectId;
            dataReceiving.FileCode = this.txtFileCode.Text.Trim();
            dataReceiving.FileName = this.txtFileName.Text.Trim();
            dataReceiving.DataReceivingDate = Funs.GetNewDateTime(this.txtDataReceivingDate.Text);
            dataReceiving.SendMan = this.txtSendMan.Text.Trim();
            dataReceiving.FileType = this.drpFileType.SelectedValue;
            dataReceiving.CopiesCount = Funs.GetNewInt(this.txtCopiesCount.Text.Trim());
            dataReceiving.ReceivingMan = this.txtReceivingMan.Text.Trim();
            dataReceiving.FileHandler = this.txtFileHandler.Text.Trim();
            dataReceiving.IsReply = Convert.ToBoolean(this.rblIsReply.SelectedValue);
            dataReceiving.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(this.drpAudit.SelectedValue))
            {
                dataReceiving.AuditMan = drpAudit.SelectedValue;
            }   //审核人
            string unitIds = string.Empty;
            var unitList = this.drpUnitIds.SelectedValueArray;
            foreach (var item in unitList)
            {
                unitIds += item + ",";
            }
            if (!string.IsNullOrEmpty(unitIds))
            {
                unitIds = unitIds.Substring(0, unitIds.LastIndexOf(","));
            }
            dataReceiving.SendUnit = unitIds;
            if (string.IsNullOrEmpty(this.DataReceivingId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    dataReceiving.DataReceivingId = this.hdAttachUrl.Text;
                }
                else
                {
                    dataReceiving.DataReceivingId = SQLHelper.GetNewID(typeof(Model.Comprehensive_DataReceiving));
                    this.hdAttachUrl.Text = dataReceiving.DataReceivingId;
                }
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataReceiving.DataReceivingId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                dataReceiving.CompileMan = this.CurrUser.UserId;
                dataReceiving.CompileDate = DateTime.Now;
                dataReceiving.Status = Const.Comprehensive_Compile;
                BLL.DataReceivingService.AddDataReceiving(dataReceiving);
            }
            else
            {
                dataReceiving.DataReceivingId = this.DataReceivingId;
                Model.Comprehensive_DataReceiving model = BLL.DataReceivingService.GetDataReceivingById(this.DataReceivingId);
                if (model != null)
                {
                    dataReceiving.Status = model.Status;
                }
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.DataReceivingId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }
                BLL.DataReceivingService.UpdateDataReceiving(dataReceiving);
            }
           
            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_DataReceiving));
            }
            Model.Comprehensive_DataReceiving dataReceiving = BLL.DataReceivingService.GetDataReceivingById(this.DataReceivingId);
            if (dataReceiving == null || ((dataReceiving.CompileMan == CurrUser.UserId && dataReceiving.Status == BLL.Const.Comprehensive_Compile) || (dataReceiving.CompileMan == CurrUser.UserId && dataReceiving.Status == BLL.Const.Comprehensive_ReCompile)))
            {
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/DataReceiving&menuId={1}", this.hdAttachUrl.Text, BLL.Const.DataReceivingMenuId)));
            }
            else
            {
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=-1&toKeyId={0}&path=FileUpload/CQMS/DataReceiving&menuId={1}", this.hdAttachUrl.Text, BLL.Const.DataReceivingMenuId)));
            }
        }
        #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.DataReceivingMenuId);
            if (buttonList.Count > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}