using BLL;
using System;
namespace FineUIPro.Web.TestRun.ProduceTestRun
{
    public partial class TestRunRecordEdit : PageBase
    {
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkId, this.CurrUser.LoginProjectId, true);
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.ProduceTestRun_TestRunRecord data = BLL.TestRunRecordService.GetTestRunRecordById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        if (!string.IsNullOrEmpty(data.UnitWorkId))
                        {
                            this.drpUnitWorkId.SelectedValue = data.UnitWorkId;
                        }
                        this.txtUnitHead.Text = data.UnitHead;
                        this.txtRunRecord.Text = data.RunRecord;
                        this.txtTestRunRecordCode.Text = data.TestRunRecordCode;
                        this.txtRecordMan.Text = data.RecordMan;
                        this.txtStartDate.Text = data.StartDate.HasValue ? string.Format("{0:yyyy-MM-dd}", data.StartDate) : "";
                        this.txtEndDate.Text = data.EndDate.HasValue ? string.Format("{0:yyyy-MM-dd}", data.EndDate) : "";
                        this.txtDescription.Text = data.Description;
                        this.txtProblemsAndSolutions.Text = data.ProblemsAndSolutions;
                        this.txtRemark.Text = data.Remark;
                    }
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.ProduceTestRun_TestRunRecord));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/ProduceTestRun/TestRunRecord&menuId={1}", this.hdId.Text, BLL.Const.TestRunRecordMenuId)));
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitWorkId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择装置/工序!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.ProduceTestRun_TestRunRecord newData = new Model.ProduceTestRun_TestRunRecord();
            newData.UnitWorkId = this.drpUnitWorkId.SelectedValue;
            newData.UnitHead = this.txtUnitHead.Text.Trim();
            newData.RunRecord = this.txtRunRecord.Text.Trim();
            newData.TestRunRecordCode = this.txtTestRunRecordCode.Text.Trim();
            newData.RecordMan = this.txtRecordMan.Text.Trim();
            newData.StartDate = Funs.GetNewDateTime(this.txtStartDate.Text.Trim());
            newData.EndDate = Funs.GetNewDateTime(this.txtEndDate.Text.Trim());
            newData.Description = this.txtDescription.Text.Trim();
            newData.ProblemsAndSolutions = this.txtProblemsAndSolutions.Text.Trim();
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.TestRunRecordId = id;
                BLL.TestRunRecordService.UpdateTestRunRecord(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.TestRunRecordId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.TestRunRecordId = SQLHelper.GetNewID(typeof(Model.ProduceTestRun_TestRunRecord));
                    this.hdId.Text = newData.TestRunRecordId;
                }
                BLL.TestRunRecordService.AddTestRunRecord(newData);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}