using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace FineUIPro.Web.Transfer
{
    public partial class CivilStructureEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Id = Request.Params["Id"];
                ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Id))
                {
                    var model = Funs.DB.Transfer_Civil_Structure.FirstOrDefault(x => x.Id == Id);
                    if (model != null)
                    {
                        txtSN.Text = model.SN.ToString();
                        txtCivil_Structure.Text = model.Civil_Structure;
                        txtSystemName.Text = model.SystemName;
                        txtSubsystem.Text = model.Subsystem;
                        txtTest_Package.Text = model.Test_Package;
                        ddlFoundation.SelectedValue = model.Foundation;
                        ddlMainstructure.SelectedValue = model.Mainstructure;
                        //ddlBuildingdecoration.SelectedValue = model.Buildingdecoration;
                        //ddlEquipment.SelectedValue = model.Equipment;
                        //ddlInstrument.SelectedValue = model.Instrument;
                        //ddlElectrical.SelectedValue = model.Electrical;
                        //ddlFireFighting.SelectedValue = model.FireFighting;
                        //ddlHVAC.SelectedValue = model.HVAC;
                        txtDescriptions.Text = model.Descriptions;
                    }
                }
                else
                {
                    var model = Funs.DB.Transfer_Civil_Structure.OrderByDescending(x => x.SN).FirstOrDefault(x => x.ProjectId == CurrUser.LoginProjectId);
                    if (model == null)
                        txtSN.Text = "1001";
                    else
                        txtSN.Text = (model.SN + 1).ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new Model.Transfer_Civil_Structure
            {
                SN = Convert.ToInt32(txtSN.Text),
                ProjectId = ProjectId,
                Civil_Structure = txtCivil_Structure.Text,
                SystemName = txtSystemName.Text,
                Subsystem = txtSubsystem.Text,
                Test_Package = txtTest_Package.Text,
                Foundation = ddlFoundation.SelectedValue,
                Mainstructure = ddlMainstructure.SelectedValue,
                //Buildingdecoration = ddlBuildingdecoration.SelectedValue,
                //Equipment = ddlEquipment.SelectedValue,
                //Instrument = ddlInstrument.SelectedValue,
                //Electrical = ddlElectrical.SelectedValue,
                //FireFighting = ddlFireFighting.SelectedValue,
                //HVAC = ddlHVAC.SelectedValue,
                Descriptions= txtDescriptions.Text
            };
            #region 判断状态
            var listObj = new List();
            listObj.Add(model.Foundation);
            listObj.Add(model.Mainstructure);
            //listObj.Add(model.Buildingdecoration);
            //listObj.Add(model.Equipment);
            //listObj.Add(model.Instrument);
            //listObj.Add(model.Electrical);
            //listObj.Add(model.FireFighting);
            //listObj.Add(model.HVAC);
            model.FINAL_Status = "Not Start";
            //全是NA或Completed 状态是Completed
            if (listObj.Where(x => x == "NA" || x == "Completed").ToList().Count == 2)
            {
                model.FINAL_Status = "Completed";
            }
            //如果全是NA或Not Start、空 就是 Not Start
            else if (listObj.Where(x => string.IsNullOrWhiteSpace(x) || x == "NA" || x == "Not Start").ToList().Count == 2)
            {
                model.FINAL_Status = "Not Start";
            }
            //如果其中有一项是In progress 或Not Start、空 是 In progress
            else if (listObj.Where(x => string.IsNullOrWhiteSpace(x) || x == "In progress" || x == "Not Start").ToList().Count >= 1)
            {
                model.FINAL_Status = "In progress";
            }
            #endregion
            if (!string.IsNullOrEmpty(Id))
            {
                var newModel = Funs.DB.Transfer_Civil_Structure.FirstOrDefault(x => x.Id == Id);
                if (newModel != null)
                {
                    newModel.SN = model.SN;
                    newModel.Civil_Structure = txtCivil_Structure.Text;
                    newModel.SystemName = txtSystemName.Text;
                    newModel.Subsystem = txtSubsystem.Text;
                    newModel.Test_Package = txtTest_Package.Text;
                    newModel.Foundation = ddlFoundation.SelectedValue;
                    newModel.Mainstructure = ddlMainstructure.SelectedValue;
                    //newModel.Buildingdecoration = ddlBuildingdecoration.SelectedValue;
                    //newModel.Equipment = ddlEquipment.SelectedValue;
                    //newModel.Instrument = ddlInstrument.SelectedValue;
                    //newModel.Electrical = ddlElectrical.SelectedValue;
                    //newModel.FireFighting = ddlFireFighting.SelectedValue;
                    //newModel.HVAC = ddlHVAC.SelectedValue;
                    newModel.FINAL_Status = model.FINAL_Status;
                    newModel.Descriptions = txtDescriptions.Text;
                }
            }
            else
            {
                model.Id = Id = Guid.NewGuid().ToString();
                Funs.DB.Transfer_Civil_Structure.InsertOnSubmit(model);
            }
            Funs.DB.SubmitChanges();
            ShowNotify("保存成功", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}