using BLL;
using System;
namespace FineUIPro.Web.TestRun.DriverSub
{
    public partial class DriverSubPlanEdit :PageBase
    {
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitService.InitUnitDownList(this.drpSubUnitId, this.CurrUser.LoginProjectId, true);
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkIds, this.CurrUser.LoginProjectId, true);
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.DriverSub_DriverSubPlan data = BLL.DriverSubPlanService.GetDriverSubPlanById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtCode.Text = data.Code;
                        if (!string.IsNullOrEmpty(data.SubUnitId))
                        {
                            this.drpSubUnitId.SelectedValue = data.SubUnitId;
                        }
                        this.txtIntroductions.Text = data.Introductions;
                        this.txtAchievement.Text = data.Achievement;
                        this.txtCooperation.Text = data.Cooperation;
                        this.drpUnitWorkIds.SelectedValueArray = data.InstallationIds.Split(',');
                        this.rblIsInvited.SelectedValue = data.IsInvited.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.DriverSub_DriverSubPlan));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverSub/DriverSubPlan&menuId={1}", this.hdId.Text, BLL.Const.DriverSubPlanMenuId)));
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpSubUnitId.SelectedValue==BLL.Const._Null)
            {
                Alert.ShowInTop("请选择开车分包单位!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            Model.DriverSub_DriverSubPlan newData = new Model.DriverSub_DriverSubPlan();
            newData.Code = this.txtCode.Text.Trim();
            if (this.drpSubUnitId.SelectedValue != BLL.Const._Null)
            {
                newData.SubUnitId = this.drpSubUnitId.SelectedValue;
            }
            newData.Introductions = this.txtIntroductions.Text.Trim();
            newData.Achievement = this.txtAchievement.Text.Trim();
            newData.Cooperation = this.txtCooperation.Text.Trim();
            if (!string.IsNullOrEmpty(this.drpUnitWorkIds.SelectedValue))
            {
                newData.InstallationIds = 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.IsInvited = Convert.ToBoolean(this.rblIsInvited.SelectedValue);
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.DriverSubPlanId = id;
                BLL.DriverSubPlanService.UpdateDriverSubPlan(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.DriverSubPlanId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.DriverSubPlanId = SQLHelper.GetNewID(typeof(Model.DriverSub_DriverSubPlan));
                    this.hdId.Text = newData.DriverSubPlanId;
                }
                BLL.DriverSubPlanService.AddDriverSubPlan(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
    }
}