using BLL;
using System;

namespace FineUIPro.Web.TestRun.BeforeTestRun
{
    public partial class BeforeTestRunEdit : PageBase
    {
        #region 加载
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitService.InitUnitDownList(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkId, this.CurrUser.LoginProjectId, true);

                this.hdType.Text = Request.Params["type"];
                if (this.hdType.Text == "1")
                {
                    this.txtTime.Label = "单试时间";
                    this.txtCount.Label = "设备总数";
                }
                else if (this.hdType.Text == "2")
                {
                    this.txtTime.Label = "吹扫时间";
                    this.txtCount.Label = "吹扫包数";
                }
                else if (this.hdType.Text == "3")
                {
                    this.txtTime.Label = "化洗时间";
                    this.txtCount.Label = "化洗系统数";
                }
                else if (this.hdType.Text == "4")
                {
                    this.txtTime.Label = "烘炉时间";
                    this.txtCount.Label = "设备总数";
                }
                else if (this.hdType.Text == "5")
                {
                    this.txtTime.Label = "装填时间";
                    this.txtCount.Label = "设备总数";
                }
                else if (this.hdType.Text == "6")
                {
                    this.txtTime.Label = "装填时间";
                    this.txtCount.Label = "设备总数";
                }
                else if (this.hdType.Text == "7")
                {
                    this.txtTime.Label = "调试时间";
                    this.txtCount.Label = "设备总数";
                }
                else if (this.hdType.Text == "8")
                {
                    this.txtTime.Label = "调试时间";
                    this.txtCount.Label = "回路总数";
                }

                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.Driver_BeforeTestRun data = BLL.BeforeTestRunService.GetBeforeTestRunById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        this.txtTime.Text = data.Time.HasValue ? string.Format("{0:yyyy-MM-dd}", data.Time) : "";
                        if (!string.IsNullOrEmpty(data.UnitId))
                        {
                            this.drpUnitId.SelectedValue = data.UnitId;
                        }
                        if (!string.IsNullOrEmpty(data.UnitWorkId))
                        {
                            this.drpUnitWorkId.SelectedValue = data.UnitWorkId;
                        }
                        this.txtCount.Text = data.Count.HasValue ? data.Count.ToString() : "";
                        this.txtCompleteCount.Text = data.CompleteCount.HasValue ? data.CompleteCount.ToString() : "";
                        this.txtTotalCount.Text = data.TotalCount.HasValue ? data.TotalCount.ToString() : "";
                        this.txtRemark.Text = data.Remark;
                    }
                }
                else
                {
                    this.txtTime.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion

        #region 附件上传
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            string menuid = "";
            if (this.hdType.Text == "1")
            {
                menuid = BLL.Const.SingleTestRunMenuId;
            }
            else if (this.hdType.Text == "2")
            {
                menuid = BLL.Const.ClearMenuId;
            }
            else if (this.hdType.Text == "3")
            {
                menuid = BLL.Const.ChemClearMenuId;
            }
            else if (this.hdType.Text == "4")
            {
                menuid = BLL.Const.OvenMenuId;
            }
            else if (this.hdType.Text == "5")
            {
                menuid = BLL.Const.CatalystMenuId;
            }
            else if (this.hdType.Text == "6")
            {
                menuid = BLL.Const.ZeoliteMenuId;
            }
            else if (this.hdType.Text == "7")
            {
                menuid = BLL.Const.ELDebugMenuId;
            }
            else if (this.hdType.Text == "8")
            {
                menuid = BLL.Const.INDebugMenuId;
            }
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.Driver_BeforeTestRun));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/BeforeTestRun&menuId={1}", this.hdId.Text, menuid)));
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择所属单位!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnitWorkId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择单位工程!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.Driver_BeforeTestRun newData = new Model.Driver_BeforeTestRun();
            newData.Code = this.txtCode.Text.Trim();
            newData.Time = Funs.GetNewDateTime(this.txtTime.Text.Trim());
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                newData.UnitId = this.drpUnitId.SelectedValue;
            }
            if (this.drpUnitWorkId.SelectedValue != BLL.Const._Null)
            {
                newData.UnitWorkId = this.drpUnitWorkId.SelectedValue;
            }
            newData.Count = Funs.GetNewIntOrZero(this.txtCount.Text.Trim());
            newData.CompleteCount = Funs.GetNewIntOrZero(this.txtCompleteCount.Text.Trim());
            newData.TotalCount = Funs.GetNewIntOrZero(this.txtTotalCount.Text.Trim());
            if (newData.Count > 0)
            {
                newData.CompleteRate = Math.Round((Convert.ToDecimal(newData.TotalCount) / Convert.ToDecimal(newData.Count)) * 100, 2);
            }
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.BeforeTestRunId = id;
                BLL.BeforeTestRunService.UpdateBeforeTestRun(newData);
            }
            else
            {
                newData.Type = this.hdType.Text.Trim();
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.BeforeTestRunId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.BeforeTestRunId = SQLHelper.GetNewID(typeof(Model.Driver_BeforeTestRun));
                    this.hdId.Text = newData.BeforeTestRunId;
                }
                BLL.BeforeTestRunService.AddBeforeTestRun(newData);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}