using BLL;
using System;

namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WBSSetAuditEdit : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                string id = Request.Params["Id"];
                string type = Request.Params["Type"];
                string oper = Request.Params["oper"];
                if (oper == "add")   //增加
                {
                    if (type == "cnProfession")
                    {
                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                        if (cnProfession != null)
                        {
                            txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", cnProfession.StartDate);
                            txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", cnProfession.EndDate);
                        }
                    }
                    else if (type == "unitProject" || type == "childUnitProject")
                    {
                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                        if (unitProject != null)
                        {
                            if (unitProject.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.EndDate);
                            }
                        }
                    }
                    else if (type == "wbsSet")
                    {
                        Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                        if (wbsSet != null)
                        {
                            if (wbsSet.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.EndDate);
                            }
                        }
                    }
                    else if (type == "installation")
                    {
                        Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                        if (installation != null)
                        {
                            if (installation.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", installation.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", installation.EndDate);
                            }
                        }
                    }
                }
                else    //修改
                {
                    if (type == "installation")
                    {
                        Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                        if (installation != null)
                        {
                            txtCode.Text = installation.InstallationCode;
                            txtName.Text = installation.InstallationName;
                            if (installation.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", installation.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", installation.EndDate);
                            }
                            txtRemark.Text = installation.Def;
                            this.trSortIndex.Hidden = true;
                            this.trUnit.Hidden = false;
                            this.drpUnit.DataTextField = "UnitName";
                            this.drpUnit.DataValueField = "UnitId";
                            this.drpUnit.DataSource = BLL.UnitService.GetUnitByProjectIdList(installation.ProjectId);
                            this.drpUnit.DataBind();
                            Funs.FineUIPleaseSelect(this.drpUnit);
                            if (!string.IsNullOrEmpty(installation.UnitId))
                            {
                                this.drpUnit.SelectedValue = installation.UnitId;
                            }
                        }
                    }
                    else if (type == "unitProject" || type == "childUnitProject")
                    {
                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                        if (unitProject != null)
                        {
                            txtCode.Text = unitProject.UnitProjectCode;
                            txtName.Text = unitProject.UnitProjectName;
                            if (unitProject.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.EndDate);
                            }
                            txtRemark.Text = unitProject.Remark;
                        }
                    }
                    else if (type == "wbsSet")
                    {
                        Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                        if (wbsSet != null)
                        {
                            txtCode.Text = wbsSet.WbsSetCode;
                            txtName.Text = wbsSet.WbsSetName;
                            if (wbsSet.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.EndDate);
                            }
                            txtRemark.Text = wbsSet.Remark;
                            if (BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(id).Count == 0)  //末级节点
                            {
                                this.trWbsSet2.Hidden = false;
                                txtControlItemDef.Text = wbsSet.ControlItemDef;
                            }
                        }
                    }
                    else if (type == "cnProfession")
                    {
                        this.trUnit.Hidden = false;
                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                        if (cnProfession != null)
                        {
                            txtCode.Text = cnProfession.CnProfessionCode;
                            txtName.Text = cnProfession.CnProfessionName;
                            if (cnProfession.StartDate != null)
                            {
                                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", cnProfession.StartDate);
                                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", cnProfession.EndDate);
                            }
                            this.trSortIndex.Hidden = true;
                            this.txtRemark.Hidden = true;
                            this.drpUnit.DataTextField = "UnitName";
                            this.drpUnit.DataValueField = "UnitId";
                            this.drpUnit.DataSource = BLL.UnitService.GetUnitByProjectIdList(cnProfession.ProjectId);
                            this.drpUnit.DataBind();
                            Funs.FineUIPleaseSelect(this.drpUnit);
                            if (!string.IsNullOrEmpty(cnProfession.UnitId))
                            {
                                this.drpUnit.SelectedValue = cnProfession.UnitId;
                            }
                        }
                    }
                }
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Params["Id"];
            string type = Request.Params["Type"];
            string oper = Request.Params["oper"];
            if (oper == "add")   //增加
            {
                string newId = string.Empty;
                if (type == "cnProfession")
                {
                    Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                    Model.Wbs_UnitProject unitProject = new Model.Wbs_UnitProject();
                    unitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                    newId = unitProject.UnitProjectId;
                    unitProject.UnitProjectCode = txtCode.Text.Trim();
                    unitProject.UnitProjectName = txtName.Text.Trim();
                    unitProject.SuperUnitProjectId = null;
                    if (cnProfession != null)
                    {
                        unitProject.ProjectId = cnProfession.ProjectId;
                        unitProject.InstallationId = cnProfession.InstallationId;
                    }
                    unitProject.CnProfessionId = id;
                    unitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    unitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    unitProject.Remark = txtRemark.Text.Trim();
                    unitProject.IsSelected = true;
                    unitProject.IsApprove = true;
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        unitProject.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    BLL.UnitProjectService.AddUnitProject(unitProject);
                }
                else if (type == "unitProject" || type == "childUnitProject")
                {
                    Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                    Model.Wbs_WbsSet wbsSet = new Model.Wbs_WbsSet();
                    wbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                    newId = wbsSet.WbsSetId;
                    wbsSet.WbsSetCode = txtCode.Text.Trim();
                    wbsSet.WbsSetName = txtName.Text.Trim();
                    if (unitProject != null)
                    {
                        wbsSet.InstallationId = unitProject.InstallationId;
                        wbsSet.CnProfessionId = unitProject.CnProfessionId;
                        wbsSet.ProjectId = unitProject.ProjectId;
                    }
                    wbsSet.UnitProjectId = id;
                    wbsSet.SuperWbsSetId = null;
                    wbsSet.Flag = 1;
                    wbsSet.IsSelected = true;
                    wbsSet.IsApprove = true;
                    wbsSet.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    wbsSet.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    wbsSet.Remark = txtRemark.Text.Trim();
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        wbsSet.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    wbsSet.ControlItemDef = this.txtControlItemDef.Text.Trim();
                    BLL.WbsSetService.AddWbsSet(wbsSet);
                }
                else if (type == "wbsSet")
                {
                    Model.Wbs_WbsSet oldWbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                    Model.Wbs_WbsSet wbsSet = new Model.Wbs_WbsSet();
                    wbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                    newId = wbsSet.WbsSetId;
                    wbsSet.WbsSetCode = txtCode.Text.Trim();
                    wbsSet.WbsSetName = txtName.Text.Trim();
                    if (oldWbsSet != null)
                    {
                        wbsSet.InstallationId = oldWbsSet.InstallationId;
                        wbsSet.CnProfessionId = oldWbsSet.CnProfessionId;
                        wbsSet.ProjectId = oldWbsSet.ProjectId;
                        wbsSet.UnitProjectId = oldWbsSet.UnitProjectId;
                    }
                    wbsSet.SuperWbsSetId = id;
                    wbsSet.Flag = (oldWbsSet.Flag ?? 0) + 1;
                    wbsSet.IsSelected = true;
                    wbsSet.IsApprove = true;
                    wbsSet.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    wbsSet.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    wbsSet.Remark = txtRemark.Text.Trim();
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        wbsSet.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    wbsSet.ControlItemDef = this.txtControlItemDef.Text.Trim();
                    BLL.WbsSetService.AddWbsSet(wbsSet);
                }
                else if (type == "installation")
                {
                    Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                    Model.Wbs_UnitProject unitProject = new Model.Wbs_UnitProject();
                    unitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                    newId = unitProject.UnitProjectId;
                    unitProject.UnitProjectCode = txtCode.Text.Trim();
                    unitProject.UnitProjectName = txtName.Text.Trim();
                    unitProject.SuperUnitProjectId = null;
                    if (installation != null)
                    {
                        unitProject.ProjectId = installation.ProjectId;
                        unitProject.InstallationId = installation.InstallationId;
                    }
                    unitProject.CnProfessionId = null;
                    unitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    unitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    unitProject.Remark = txtRemark.Text.Trim();
                    unitProject.IsSelected = true;
                    unitProject.IsApprove = true;
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        unitProject.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    BLL.UnitProjectService.AddUnitProject(unitProject);
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(newId)
                   + ActiveWindow.GetHidePostBackReference());
            }
            else    //修改
            {
                if (type == "unitProject" || type == "childUnitProject")
                {
                    Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                    if (unitProject != null)
                    {
                        unitProject.UnitProjectCode = txtCode.Text.Trim();
                        unitProject.UnitProjectName = txtName.Text.Trim();
                        unitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                        unitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                        unitProject.Remark = txtRemark.Text.Trim();
                        if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                        {
                            unitProject.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                        }
                        BLL.UnitProjectService.UpdateUnitProject(unitProject);
                    }
                }
                else if (type == "wbsSet")
                {
                    Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                    if (wbsSet != null)
                    {
                        wbsSet.WbsSetCode = txtCode.Text.Trim();
                        wbsSet.WbsSetName = txtName.Text.Trim();
                        wbsSet.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                        wbsSet.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                        wbsSet.Remark = txtRemark.Text.Trim();
                        if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                        {
                            wbsSet.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                        }
                        wbsSet.ControlItemDef = this.txtControlItemDef.Text.Trim();
                        BLL.WbsSetService.UpdateWbsSet(wbsSet);
                    }
                }
                else if (type == "cnProfession")
                {
                    Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                    if (cnProfession != null)
                    {
                        cnProfession.CnProfessionCode = txtCode.Text.Trim();
                        cnProfession.CnProfessionName = txtName.Text.Trim();
                        cnProfession.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                        cnProfession.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                        if (this.drpUnit.SelectedValue != BLL.Const._Null)
                        {
                            cnProfession.UnitId = this.drpUnit.SelectedValue;
                        }
                        BLL.CnProfessionService.UpdateCnProfession(cnProfession);
                    }
                }
                else if (type == "installation")
                {
                    Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                    if (installation != null)
                    {
                        installation.InstallationCode = txtCode.Text.Trim();
                        installation.InstallationName = txtName.Text.Trim();
                        installation.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                        installation.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                        installation.Def = this.txtRemark.Text.Trim();
                        if (this.drpUnit.SelectedValue != BLL.Const._Null)
                        {
                            installation.UnitId = this.drpUnit.SelectedValue;
                        }
                        BLL.Project_InstallationService.UpdateInstallation(installation);
                    }
                }
                //BLL.LogService.AddSys_Log(this.CurrUser, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "修改单位、分部、分项工程");
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion
    }
}