using BLL;
using System;

namespace FineUIPro.Web.TestRun.DriverSub
{
    public partial class DriverSubEdit :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.drpSubUnitId, this.CurrUser.LoginProjectId, true);
                this.drpImplement.DataTextField = "Text";
                this.drpImplement.DataValueField = "Value";
                this.drpImplement.DataSource = BLL.DropListService.drpImplementItemList();
                this.drpImplement.DataBind();
                Funs.FineUIPleaseSelect(this.drpImplement);

                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.DriverSub_DriverSub data = BLL.DriverSubService.GetDriverSubById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        if (!string.IsNullOrEmpty(data.SubUnitId))
                        {
                            this.drpSubUnitId.SelectedValue = data.SubUnitId;
                        }
                        if (!string.IsNullOrEmpty(data.Code))
                        {
                            this.drpImplement.SelectedValue = data.Code;
                        }
                        this.txtInstruction.Text = data.Instruction;
                        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.DriverSub_DriverSub));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverSub/DriverSub&menuId={1}", this.hdId.Text, BLL.Const.DriverSubMenuId)));
        }
        #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.drpSubUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择记录/报告/执行情况!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.DriverSub_DriverSub newData = new Model.DriverSub_DriverSub();
            newData.Code = this.txtCode.Text.Trim();
            if (this.drpSubUnitId.SelectedValue != BLL.Const._Null)
            {
                newData.SubUnitId = this.drpSubUnitId.SelectedValue;
            }
            if (this.drpImplement.SelectedValue!=BLL.Const._Null)
            {
                newData.Implement = this.drpImplement.SelectedItem.Text;
            }
            newData.Instruction = this.txtInstruction.Text.Trim();
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.DriverSubId = id;
                BLL.DriverSubService.UpdateDriverSub(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.DriverSubId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.DriverSubId = SQLHelper.GetNewID(typeof(Model.DriverSub_DriverSub));
                    this.hdId.Text = newData.DriverSubId;
                }
                BLL.DriverSubService.AddDriverSub(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
    }
}