using BLL;
using System;
namespace FineUIPro.Web.TestRun.DriverRun
{
    public partial class DriverRunPlanEdit : PageBase
    {
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitService.InitUnitDownList(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkIds, this.CurrUser.LoginProjectId, true);
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.DriverRun_DriverRunPlan data = BLL.DriverRunPlanService.GetDriverRunPlanById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        if (!string.IsNullOrEmpty(data.UnitId))
                        {
                            this.drpUnitId.SelectedValue = data.UnitId;
                        }
                        this.txtEnterpriseDescription.Text = data.EnterpriseDescription;
                        this.txtDriverRunPerformance.Text = data.DriverRunPerformance;
                        this.txtTeamworkHistory.Text = data.TeamworkHistory;
                        this.drpUnitWorkIds.SelectedValueArray = data.InstallationId.Split(',');
                        this.rblIsAcceptInvite.SelectedValue = data.IsAcceptInvite.ToString();
                        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.DriverRun_DriverRunPlan));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverRun/DriverRunPlan&menuId={1}", this.hdId.Text, BLL.Const.DriverRunPlanMenuId)));
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择单位名称!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.DriverRun_DriverRunPlan newData = new Model.DriverRun_DriverRunPlan();
            newData.Code = this.txtCode.Text.Trim();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                newData.UnitId = this.drpUnitId.SelectedValue;
            }
            newData.EnterpriseDescription = this.txtEnterpriseDescription.Text.Trim();
            newData.DriverRunPerformance = this.txtDriverRunPerformance.Text.Trim();
            newData.TeamworkHistory = this.txtTeamworkHistory.Text.Trim();
            if (!string.IsNullOrEmpty(this.drpUnitWorkIds.SelectedValue))
            {
                newData.InstallationId = GetStringByArray(this.drpUnitWorkIds.SelectedValueArray);
                string unitWorkNames = string.Empty;
                foreach (var item in this.drpUnitWorkIds.SelectedValueArray)
                {
                    var unitWork = BLL.UnitWorkService.getUnitWorkByUnitWorkId(item);
                    if (unitWork != null)
                    {
                        unitWorkNames += unitWork.UnitWorkName + ",";
                    }
                }
                if (!string.IsNullOrEmpty(unitWorkNames))
                {
                    newData.InstallationNames = unitWorkNames.Substring(0, unitWorkNames.LastIndexOf(","));
                }
            }
            newData.IsAcceptInvite = Convert.ToBoolean(this.rblIsAcceptInvite.SelectedValue);
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.DriverRunPlanId = id;
                BLL.DriverRunPlanService.UpdateDriverRunPlan(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.DriverRunPlanId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.DriverRunPlanId = SQLHelper.GetNewID(typeof(Model.DriverRun_DriverRunPlan));
                    this.hdId.Text = newData.DriverRunPlanId;
                }
                BLL.DriverRunPlanService.AddDriverRunPlan(newData);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        private string GetStringByArray(string[] array)
        {
            string str = string.Empty;
            foreach (var item in array)
            {
                if (item != BLL.Const._Null)
                {
                    str += item + ",";
                }
            }
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Substring(0, str.LastIndexOf(","));
            }
            return str;
        }
        #endregion
    }
}