using BLL;
using System;
using System.IO;
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.drpUnitWorkIds.SelectedValueArray = data.InstallationId.Split(',');
                        if (data.EstimatedInsuredPersonNum != null)
                        {
                            this.txtEstimatedInsuredPersonNum.Text = data.EstimatedInsuredPersonNum.ToString();
                        }
                        if (data.GuaranteedOperationPeriod != null)
                        {
                            this.txtGuaranteedOperationPeriod.Text = data.GuaranteedOperationPeriod.ToString();
                        }
                        if (data.TotalGuaranteedOperationDays != null)
                        {
                            this.txtTotalGuaranteedOperationDays.Text = data.TotalGuaranteedOperationDays.ToString();
                        }
                        this.txtRemark.Text = data.Remark;
                    }
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_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;
            }
            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.EstimatedInsuredPersonNum = Funs.GetNewInt(this.txtEstimatedInsuredPersonNum.Text.Trim());
            newData.GuaranteedOperationPeriod = Funs.GetNewInt(this.txtGuaranteedOperationPeriod.Text.Trim());
            newData.TotalGuaranteedOperationDays = Funs.GetNewInt(this.txtTotalGuaranteedOperationDays.Text.Trim());
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(this.hdId.Text))
            {
                newData.DriverRunPlanId = this.hdId.Text;
                BLL.DriverRunPlanService.UpdateDriverRunPlan(newData);
            }
            else
            {
                newData.DriverRunPlanId = SQLHelper.GetNewID(typeof(Model.DriverRun_DriverRunPlan));
                this.hdId.Text = newData.DriverRunPlanId;
                BLL.DriverRunPlanService.AddDriverRunPlan(newData);
            }
            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)));
        }
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttachK_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;
            }
            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.EstimatedInsuredPersonNum = Funs.GetNewInt(this.txtEstimatedInsuredPersonNum.Text.Trim());
            newData.GuaranteedOperationPeriod = Funs.GetNewInt(this.txtGuaranteedOperationPeriod.Text.Trim());
            newData.TotalGuaranteedOperationDays = Funs.GetNewInt(this.txtTotalGuaranteedOperationDays.Text.Trim());
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(this.hdId.Text))
            {
                newData.DriverRunPlanId = this.hdId.Text;
                BLL.DriverRunPlanService.UpdateDriverRunPlan(newData);
            }
            else
            {
                newData.DriverRunPlanId = SQLHelper.GetNewID(typeof(Model.DriverRun_DriverRunPlan));
                this.hdId.Text = newData.DriverRunPlanId;
                BLL.DriverRunPlanService.AddDriverRunPlan(newData);
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverRun/DriverRunPlanK&menuId={1}", this.hdId.Text + "K", BLL.Const.DriverRunPlanMenuId)));
        }
        #endregion
        #region 下载模板
        /// 
        /// 下载模板按钮
        /// 
        /// 
        /// 
        protected void btnDownLoad_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
        }
        /// 
        /// 下载导入模板
        /// 
        /// 
        /// 
        protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
        {
            if (e.EventArgument == "Confirm_OK")
            {
                string rootPath = Server.MapPath("~/");
                string uploadfilepath = rootPath + "File\\Excel\\TestRun\\保运考勤表模板.xls";
                string filePath = "File\\Excel\\TestRun\\保运考勤表模板.xls";
                string fileName = Path.GetFileName(filePath);
                FileInfo info = new FileInfo(uploadfilepath);
                long fileSize = info.Length;
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.ContentType = "excel/plain";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("Content-Length", fileSize.ToString().Trim());
                Response.TransmitFile(uploadfilepath, 0, fileSize);
                Response.End();
            }
        }
        #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;
            }
            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.EstimatedInsuredPersonNum = Funs.GetNewInt(this.txtEstimatedInsuredPersonNum.Text.Trim());
            newData.GuaranteedOperationPeriod = Funs.GetNewInt(this.txtGuaranteedOperationPeriod.Text.Trim());
            newData.TotalGuaranteedOperationDays = Funs.GetNewInt(this.txtTotalGuaranteedOperationDays.Text.Trim());
            newData.Remark = this.txtRemark.Text.Trim();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(this.hdId.Text))
            {
                newData.DriverRunPlanId = this.hdId.Text;
                BLL.DriverRunPlanService.UpdateDriverRunPlan(newData);
            }
            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
    }
}