using BLL;
using System;

namespace FineUIPro.Web.TestRun.DriverRun
{
    public partial class DriverRunEdit : 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);
                this.drpImplement.DataTextField = "Text";
                this.drpImplement.DataValueField = "Value";
                this.drpImplement.DataSource = BLL.DropListService.drpDriverRunImplementItemList();
                this.drpImplement.DataBind();
                Funs.FineUIPleaseSelect(this.drpImplement);

                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.DriverRun_DriverRun data = BLL.DriverRunService.GetDriverRunById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        if (!string.IsNullOrEmpty(data.UnitId))
                        {
                            this.drpUnitId.SelectedValue = data.UnitId;
                        }
                        if (!string.IsNullOrEmpty(data.Code))
                        {
                            this.drpImplement.SelectedValue = data.Code;
                        }
                        this.txtDescriptions.Text = data.Descriptions;
                        this.txtRemark.Text = data.Remark;
                    }
                }
            }
        }
        #endregion

        #region 附件上传
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.DriverRun_DriverRun));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverRun/DriverRun&menuId={1}", this.hdId.Text, BLL.Const.DriverRunMenuId)));
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpImplement.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择记录/报告/执行情况!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择单位名称!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.DriverRun_DriverRun newData = new Model.DriverRun_DriverRun();
            newData.Code = this.txtCode.Text.Trim();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                newData.UnitId = this.drpUnitId.SelectedValue;
            }
            if (this.drpImplement.SelectedValue != BLL.Const._Null)
            {
                newData.Implement = this.drpImplement.SelectedItem.Text;
            }
            newData.Descriptions = this.txtDescriptions.Text.Trim();
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.DriverRunId = id;
                BLL.DriverRunService.UpdateDriverRun(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.DriverRunId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.DriverRunId = SQLHelper.GetNewID(typeof(Model.DriverRun_DriverRun));
                    this.hdId.Text = newData.DriverRunId;
                }
                BLL.DriverRunService.AddDriverRun(newData);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region DropDownList下拉选择事件
        protected void drpImplement_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpImplement.SelectedValue == BLL.Const._Null)
            {
                this.txtCode.Text = string.Empty;
            }
            else
            {
                this.txtCode.Text = this.drpImplement.SelectedValue;
            }
        }
        #endregion
    }
}