using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.CQMS.DataBase
{
    public partial class WBSFileEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.drpUnitId.DataValueField = "UnitId";
                this.drpUnitId.DataTextField = "UnitName";
                this.drpUnitId.DataSource = BLL.UnitService.GetMainAndSubUnitByProjectIdList(this.CurrUser.LoginProjectId);
                this.drpUnitId.DataBind();
                Funs.FineUIPleaseSelect(this.drpUnitId);
                //string dataTypeProjectId = Request.Params["dataTypeProjectId"];
                //string divisionProjectId = Request.Params["divisionProjectId"];
                string fileId = Request.Params["fileId"];
                if (!string.IsNullOrEmpty(fileId))
                {
                    Model.ProcessControl_InspectionManagement file = BLL.InspectionManagementService.GetInspectionManagementById(fileId);
                    this.txtFileCode.Text = file.InspectionCode;
                    this.txtFileName.Text = file.NoticeCode;
                    if (!string.IsNullOrEmpty(file.UnitId))
                    {
                        this.drpUnitId.SelectedValue = file.UnitId;
                    }
                }
            }
        }

        #region 附件上传
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.ProcessControl_InspectionManagement));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CommonUpLoadFile&menuId={1}", this.hdId.Text, BLL.Const.DataBaseProjectMenuId)));
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string dataTypeProjectId = Request.Params["dataTypeProjectId"];
            string divisionProjectId = Request.Params["divisionProjectId"];
            string fileId = Request.Params["fileId"];

            Model.ProcessControl_InspectionManagement newFile = new Model.ProcessControl_InspectionManagement();
            newFile.InspectionCode = this.txtFileCode.Text.Trim();
            newFile.NoticeCode = this.txtFileName.Text.Trim();
            if (this.drpUnitId.SelectedValue!=BLL.Const._Null)
            {
                newFile.UnitId = this.drpUnitId.SelectedValue;
            }
            if (!string.IsNullOrEmpty(fileId))
            {
                newFile.InspectionId = fileId;
                BLL.InspectionManagementService.UpdateInspectionManagement(newFile);
            }
            else
            {
                Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(divisionProjectId);
                if (divisionProject != null)
                {
                    newFile.UnitWorkId = divisionProject.UnitWorkId;
                }

                if (Request.Params["yellow"] == "Yellow")
                {
                    Model.WBS_BreakdownProject breakdownProject = Funs.DB.WBS_BreakdownProject.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.BreakdownName == "砌体结构基础或主体验收");
                    if (breakdownProject != null)
                    {
                        newFile.ControlPointType = breakdownProject.BreakdownProjectId;
                    }
                }
                newFile.FileType = "WBS";
                newFile.Branch = divisionProjectId;
                newFile.ProjectId = this.CurrUser.LoginProjectId;                
                newFile.CompileMan = this.CurrUser.UserId;
                newFile.CompileDate = DateTime.Now;
                if (!string.IsNullOrEmpty(this.hdId.Text.Trim()))
                {
                    newFile.InspectionId = this.hdId.Text.Trim();
                }
                else
                {
                    newFile.InspectionId = SQLHelper.GetNewID(typeof(Model.ProcessControl_InspectionManagement));
                    this.hdId.Text = newFile.InspectionId;
                }
                BLL.InspectionManagementService.AddInspectionManagement(newFile);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}