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

namespace FineUIPro.Web.ProjectData
{
    public partial class InstallationSave : PageBase
    {
        private static List<Model.WBS_WbsSetInit> wbsSetInits;
        private static string installationId;
        private static DateTime? startDate;
        private static DateTime? endDate;
        private static string superId;
        private static string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                superId = Request.Params["SuperId"];
                id = Request.Params["Id"];
                Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(superId);
                Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(superId);
                LoadData();
                if (project != null)
                {
                    if (project.StartDate != null)
                    {
                        this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", project.StartDate);
                    }
                    if (project.EndDate != null)
                    {
                        this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", project.EndDate);
                    }
                }
                else if (installation != null)
                {
                    if (installation.StartDate != null)
                    {
                        this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", installation.StartDate);
                    }
                    if (installation.EndDate != null)
                    {
                        this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", installation.EndDate);
                    }
                }
                Model.Project_Installation installation1 = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                if (installation1 != null)
                {
                    this.txtInstallationCode.Text = installation1.InstallationCode;
                    this.txtInstallationName.Text = installation1.InstallationName;
                    if (installation1.StartDate != null)
                    {
                        this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", installation1.StartDate);
                    }
                    if (installation1.EndDate != null)
                    {
                        this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", installation1.EndDate);
                    }
                    this.txtRemark.Text = installation1.Def;
                }
            }
        }

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

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string projectId = string.Empty;
            string superInstallationId = string.Empty;
            Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(superId);
            Model.Project_Installation parInstallation = BLL.Project_InstallationService.GetInstallationByInstallationId(superId);
            if (project != null)
            {
                projectId = project.ProjectId;
                superInstallationId = "0";
            }
            else if (parInstallation != null)
            {
                projectId = parInstallation.ProjectId;
                superInstallationId = superId;
            }

            Model.Project_Installation installation = new Project_Installation();
            installation.InstallationCode = txtInstallationCode.Text.Trim();
            installation.InstallationName = txtInstallationName.Text.Trim();
            installation.ProjectId = projectId;
            installation.SuperInstallationId = superInstallationId;
            if (!string.IsNullOrEmpty(txtStartDate.Text.Trim()))
            {
                installation.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtEndDate.Text.Trim()))
            {
                installation.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
            }
            installation.Def = txtRemark.Text.Trim();
            if (string.IsNullOrEmpty(id))
            {
                installation.InstallationId = SQLHelper.GetNewID(typeof(Model.Project_Installation));
                BLL.Project_InstallationService.AddInstallation(installation);
                if (parInstallation != null)
                {
                    Model.Project_Installation pparInstallation = BLL.Project_InstallationService.GetInstallationByInstallationId(parInstallation.SuperInstallationId);
                    if (pparInstallation != null && pparInstallation.SuperInstallationId == "0")   //当前装置级别为主项,拷贝WBS数据
                    {
                        installationId = installation.InstallationId;
                        startDate = installation.StartDate;
                        endDate = installation.EndDate;
                        //拷贝专业
                        var cnProfessionInits = from x in Funs.DB.WBS_CnProfessionInit where x.CnProfessionId != 20 select x;
                        foreach (var cnProfessionInit in cnProfessionInits)
                        {
                            Model.WBS_CnProfession cnProfession = new Model.WBS_CnProfession();
                            cnProfession.CnProfessionName = cnProfessionInit.CnProfessionName;
                            cnProfession.CnProfessionCode = cnProfessionInit.CnProfessionCode;
                            cnProfession.InstallationId = installationId;
                            cnProfession.ProjectId = projectId;
                            cnProfession.StartDate = startDate;
                            cnProfession.EndDate = endDate;
                            cnProfession.OldId = cnProfessionInit.CnProfessionId;
                            BLL.CnProfessionService.AddCnProfession(cnProfession);
                        }
                        //拷贝单位工程及子单位工程
                        var unitProjectInits = from x in Funs.DB.Wbs_UnitProjectInit where x.CnProfessionId != 20 orderby x.SuperUnitProject select x;
                        foreach (var unitProjectInit in unitProjectInits)
                        {
                            Model.Wbs_UnitProject unitProject = new Model.Wbs_UnitProject();
                            unitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                            unitProject.UnitProjectCode = unitProjectInit.UnitProjectCode;
                            unitProject.UnitProjectName = unitProjectInit.UnitProjectName;
                            unitProject.InstallationId = installationId;
                            //获取对应的专业Id
                            string cnProfessionId = (from x in Funs.DB.WBS_CnProfession
                                                     where x.InstallationId == installationId && x.OldId == unitProjectInit.CnProfessionId
                                                     select x.CnProfessionId).FirstOrDefault();
                            if (unitProjectInit.SuperUnitProject == null)
                            {
                                unitProject.SuperUnitProjectId = null;
                            }
                            else
                            {
                                unitProject.SuperUnitProjectId = (from x in Funs.DB.Wbs_UnitProject
                                                                  where x.UnitProjectCode == unitProjectInit.SuperUnitProject && x.CnProfessionId == cnProfessionId
                                                                  select x.UnitProjectId).FirstOrDefault();
                            }
                            unitProject.ProjectId = projectId;
                            unitProject.CnProfessionId = cnProfessionId;
                            unitProject.StartDate = startDate;
                            unitProject.EndDate = endDate;
                            unitProject.Remark = unitProjectInit.Remark;
                            unitProject.IsIn = true;
                            BLL.UnitProjectService.AddUnitProject(unitProject);
                        }
                        //拷贝分部/子分部/分项/子分项
                        wbsSetInits = (from x in Funs.DB.WBS_WbsSetInit where x.CnProfessionId != 20 orderby x.SuperWbsSetCode select x).ToList();
                        foreach (var wbsSetInit in wbsSetInits)
                        {
                            Model.Wbs_WbsSet wbsSet = new Model.Wbs_WbsSet();
                            wbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                            wbsSet.WbsSetCode = wbsSetInit.WbsSetCode;
                            wbsSet.WbsSetName = wbsSetInit.WbsSetName;
                            wbsSet.InstallationId = installationId;
                            //获取对应的专业Id
                            string cnProfessionId = (from x in Funs.DB.WBS_CnProfession
                                                     where x.InstallationId == installationId && x.OldId == wbsSetInit.CnProfessionId
                                                     select x.CnProfessionId).FirstOrDefault();
                            wbsSet.CnProfessionId = cnProfessionId;
                            wbsSet.UnitProjectId = (from x in Funs.DB.Wbs_UnitProject where x.UnitProjectCode == wbsSetInit.UnitProjectCode && x.CnProfessionId == cnProfessionId select x.UnitProjectId).FirstOrDefault();
                            if (wbsSetInit.SuperWbsSetCode == null)
                            {
                                wbsSet.SuperWbsSetId = null;
                            }
                            else
                            {
                                wbsSet.SuperWbsSetId = (from x in Funs.DB.Wbs_WbsSet
                                                        where x.WbsSetCode == wbsSetInit.SuperWbsSetCode && x.InstallationId == installationId && x.CnProfessionId == cnProfessionId
                                                        select x.WbsSetId).FirstOrDefault();
                            }
                            if (projectId.Contains("?"))
                            {
                                projectId = projectId.Substring(0, projectId.LastIndexOf("?"));
                            }
                            wbsSet.ProjectId = projectId;
                            wbsSet.StartDate = startDate;
                            wbsSet.EndDate = endDate;
                            wbsSet.Flag = wbsSetInit.Flag;
                            wbsSet.Way = wbsSetInit.Way;
                            wbsSet.ControlItemDef = wbsSetInit.ControlItemDef;
                            wbsSet.ControlPoint = wbsSetInit.ControlPoint;
                            wbsSet.Remark = wbsSetInit.Remark;
                            wbsSet.IsIn = true;
                            BLL.WbsSetService.AddWbsSet(wbsSet);
                        }
                    }
                }
                BLL.LogService.AddSys_Log(this.CurrUser, installation.InstallationId, installation.InstallationId, BLL.Const.ProjectInstallationMenuId, "增加装置/单元!");
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(installation.InstallationId) + ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Model.Project_Installation installation1 = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                installation.InstallationId = id;
                installation.SuperInstallationId = installation1.SuperInstallationId;
                BLL.Project_InstallationService.UpdateInstallation(installation);
                //更新子级WBS内容计划时间
                if (!BLL.Project_InstallationService.IsExitsInstallationsBySuperInstallationId(id))   //末级装置节点
                {
                    var cns = BLL.CnProfessionService.GetCnProfessionByInstallation(id);
                    if (cns.Count > 0)
                    {
                        foreach (var cn in cns)
                        {
                            cn.StartDate = installation.StartDate;
                            cn.EndDate = installation.EndDate;
                            BLL.CnProfessionService.UpdateCnProfession(cn);
                        }
                    }
                    var unitProjects = BLL.UnitProjectService.GetUnitProjectsByInstallationId(id);
                    if (unitProjects.Count > 0)
                    {
                        foreach (var up in unitProjects)
                        {
                            up.StartDate = installation.StartDate;
                            up.EndDate = installation.EndDate;
                            BLL.UnitProjectService.UpdateUnitProject(up);
                        }
                    }
                    var wbsSets = BLL.WbsSetService.GetWbsSetsByInstallationId(id);
                    if (wbsSets.Count > 0)
                    {
                        foreach (var ws in wbsSets)
                        {
                            ws.StartDate = installation.StartDate;
                            ws.EndDate = installation.EndDate;
                            BLL.WbsSetService.UpdateWbsSet(ws);
                        }
                    }
                }
                BLL.LogService.AddSys_Log(this.CurrUser, installation.InstallationId, installation.InstallationId, BLL.Const.ProjectInstallationMenuId, "修改装置/单元!");
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }

            //PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ProgressBar.aspx?InstallationId={0}", installation.InstallationId)));
        }

        /// <summary>
        /// 关闭弹出窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            Alert.ShowInParent("保存成功!");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}