using BLL;
using System;
namespace FineUIPro.Web.TestRun.ProduceProperty
{
    public partial class AnalySampEdit : PageBase
    {
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkId, this.CurrUser.LoginProjectId, true);
                BLL.UserService.InitUserDropDownList(this.drpInstallationMan, this.CurrUser.LoginProjectId, true);
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.ProduceProperty_AnalySamp data = BLL.AnalySampService.GetAnalySampById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        if (!string.IsNullOrEmpty(data.UnitWorkId))
                        {
                            this.drpUnitWorkId.SelectedValue = data.UnitWorkId;
                        }
                        if (!string.IsNullOrEmpty(data.InstallationMan))
                        {
                            this.drpInstallationMan.SelectedValue = data.InstallationMan;
                        }
                        this.txtAnalySampDate.Text = data.AnalySampDate.HasValue ? string.Format("{0:yyyy-MM-dd}", data.AnalySampDate) : "";
                        this.txtAnalyRecord.Text = data.AnalyRecord;
                        this.txtAnalyCode.Text = data.AnalyCode;
                        this.txtTrackingMan.Text = BLL.UserService.GetUserNameByUserId(data.TrackingMan);
                        this.txtAnalyData.Text = data.AnalyData;
                        this.txtRemark.Text = data.Remark;
                    }
                }
                else
                {
                    this.txtTrackingMan.Text = this.CurrUser.UserName;
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.ProduceProperty_AnalySamp));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/ProduceProperty/AnalySamp&menuId={1}", this.hdId.Text, BLL.Const.AnalySampMenuId)));
        }
        #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.ProduceProperty_AnalySamp newData = new Model.ProduceProperty_AnalySamp();
            newData.Code = this.txtCode.Text.Trim();
            newData.UnitWorkId = this.drpUnitWorkId.SelectedValue;
            if (this.drpInstallationMan.SelectedValue != BLL.Const._Null)
            {
                newData.InstallationMan = this.drpInstallationMan.SelectedValue;
            }
            newData.AnalySampDate = Funs.GetNewDateTime(this.txtAnalySampDate.Text.Trim());
            newData.AnalyRecord = this.txtAnalyRecord.Text.Trim();
            newData.AnalyCode = this.txtAnalyCode.Text.Trim();
            newData.TrackingMan = this.CurrUser.UserId;
            newData.AnalyData = this.txtAnalyData.Text.Trim();
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.AnalySampId = id;
                BLL.AnalySampService.UpdateAnalySamp(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.AnalySampId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.AnalySampId = SQLHelper.GetNewID(typeof(Model.ProduceProperty_AnalySamp));
                    this.hdId.Text = newData.AnalySampId;
                }
                BLL.AnalySampService.AddAnalySamp(newData);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}