using BLL;
using System;
namespace FineUIPro.Web.TestRun.DriverRun
{
    public partial class DriverRunEdit : PageBase
    {
        /// 
        /// 办理类型
        /// 
        public string State
        {
            get
            {
                return (string)ViewState["State"];
            }
            set
            {
                ViewState["State"] = value;
            }
        }
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UnitService.InitUnitDownList(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                ListItem[] list1 = new ListItem[3];
                list1[0]= new ListItem("未开始","1");
                list1[1] = new ListItem("进行中", "2");
                list1[2] = new ListItem("已完成", "3");
                this.drpStatus.DataTextField = "Text";
                this.drpStatus.DataValueField = "Value";
                this.drpStatus.DataSource = list1;
                this.drpStatus.DataBind();
                Funs.FineUIPleaseSelect(this.drpStatus);
                ListItem[] list2 = new ListItem[3];
                list2[0] = new ListItem("低", "1");
                list2[1] = new ListItem("中", "2");
                list2[2] = new ListItem("高", "3");
                this.drpPriority.DataTextField = "Text";
                this.drpPriority.DataValueField = "Value";
                this.drpPriority.DataSource = list2;
                this.drpPriority.DataBind();
                Funs.FineUIPleaseSelect(this.drpPriority);
                BLL.UnitWorkService.InitUnitWorkDropDownList(this.drpUnitWorkIds, this.CurrUser.LoginProjectId, true);
                plApprove1.Hidden = true;
                plApprove2.Hidden = true;
                txtRealCompletedDate.Hidden = true;
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.DriverRun_DriverRun data = BLL.DriverRunService.GetDriverRunById(id);
                    if (data != null)
                    {
                        plApprove1.Hidden = false;
                        plApprove2.Hidden = false;
                        var dt = DriverRunApproveService.getListData(id);
                        gvApprove.DataSource = dt;
                        gvApprove.DataBind();
                        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(',');
                        this.txtDescriptions.Text = data.Descriptions;
                        this.txtObjective.Text = data.Objective;
                        if (data.NeedCompletedDate != null)
                        {
                            this.txtNeedCompletedDate.Text = string.Format("{0:yyyy-MM-dd}", data.NeedCompletedDate);
                        }
                        if (data.RealCompletedDate != null)
                        {
                            this.txtRealCompletedDate.Text = string.Format("{0:yyyy-MM-dd}", data.RealCompletedDate);
                        }
                        if (!string.IsNullOrEmpty(data.Status))
                        {
                            this.drpStatus.SelectedValue = data.Status;
                        }
                        if (!string.IsNullOrEmpty(data.Priority))
                        {
                            this.drpPriority.SelectedValue = data.Priority;
                        }
                        if (!string.IsNullOrEmpty(data.State))
                        {
                            State = data.State;
                        }
                        else
                        {
                            State = BLL.Const.DriverRun_Compile;
                        }
                        if (State != BLL.Const.DriverRun_Complete)
                        {
                            DriverRunService.Init(drpHandleType, State, false);
                        }
                        if (State == BLL.Const.DriverRun_Compile)
                        {
                            UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                            this.drpHandleMan.SelectedIndex = 1;
                        }
                        else
                        {
                            if (State == BLL.Const.DriverRun_Audit2)
                            {
                                Model.DriverRun_DriverRunApprove approve = BLL.DriverRunApproveService.GetAudit2(id);
                                if (approve != null)
                                {
                                    ListItem[] list = new ListItem[1];
                                    list[0] = new ListItem(BLL.UserService.GetUserByUserId(approve.ApproveMan).UserName ?? "", approve.ApproveMan);
                                    drpHandleMan.DataValueField = "Value";
                                    drpHandleMan.DataTextField = "Text";
                                    drpHandleMan.DataSource = list;
                                    drpHandleMan.DataBind();
                                }
                            }
                            else if (State == BLL.Const.DriverRun_Audit3)
                            {
                                txtRealCompletedDate.Hidden = false;
                                Model.DriverRun_DriverRunApprove approve = BLL.DriverRunApproveService.GetAudit1(id);
                                if (approve != null)
                                {
                                    ListItem[] list = new ListItem[1];
                                    list[0] = new ListItem(BLL.UserService.GetUserByUserId(approve.ApproveMan).UserName ?? "", approve.ApproveMan);
                                    drpHandleMan.DataValueField = "Value";
                                    drpHandleMan.DataTextField = "Text";
                                    drpHandleMan.DataSource = list;
                                    drpHandleMan.DataBind();
                                }
                            }
                            else if (State == BLL.Const.DriverRun_Audit4)
                            {
                                txtRealCompletedDate.Hidden = false;
                                drpHandleMan.Items.Clear();
                                drpHandleMan.Enabled = false;
                            }
                            else
                            {
                                UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                            }
                        }
                        this.txtOpinions.Text = "同意";
                    }
                }
                else
                {
                    State = Const.DriverRun_Compile;
                    DriverRunService.Init(drpHandleType, State, false);
                    UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        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 保存/提交
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择单位名称!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnitWorkIds.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择区域!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpStatus.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择实施状态!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpPriority.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择优先级!", MessageBoxIcon.Warning);
                return;
            }
            if (State == BLL.Const.DriverRun_Audit4 && string.IsNullOrEmpty(this.txtRealCompletedDate.Text.Trim()))
            {
                Alert.ShowInTop("请选择实际完成时间!", MessageBoxIcon.Warning);
                return;
            }
            SaveData("save");
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择单位名称!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnitWorkIds.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择区域!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpStatus.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择实施状态!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpPriority.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择优先级!", MessageBoxIcon.Warning);
                return;
            }
            if (State == BLL.Const.DriverRun_Audit4 && string.IsNullOrEmpty(this.txtRealCompletedDate.Text.Trim()))
            {
                Alert.ShowInTop("请选择实际完成时间!", MessageBoxIcon.Warning);
                return;
            }
            SaveData("submit");
            ShowNotify("提交成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        private void SaveData(string saveType)
        {
            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 (!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.Descriptions = this.txtDescriptions.Text.Trim();
            newData.Objective = this.txtObjective.Text.Trim();
            if (this.drpStatus.SelectedValue != BLL.Const._Null)
            {
                newData.Status = this.drpStatus.SelectedValue;
            }
            if (this.drpPriority.SelectedValue != BLL.Const._Null)
            {
                newData.Priority = this.drpPriority.SelectedValue;
            }
            newData.NeedCompletedDate = Funs.GetNewDateTime(this.txtNeedCompletedDate.Text.Trim());
            newData.ProjectId = this.CurrUser.LoginProjectId;
            newData.RealCompletedDate = Funs.GetNewDateTime(this.txtRealCompletedDate.Text.Trim());
            if (saveType == "submit")
            {
                newData.State = drpHandleType.SelectedValue.Trim();
            }
            else
            {
                Model.DriverRun_DriverRun newData1 = BLL.DriverRunService.GetDriverRunById(id);
                if (newData1 != null)
                {
                    if (string.IsNullOrEmpty(newData1.State))
                    {
                        newData.State = BLL.Const.DriverRun_Compile;
                    }
                    else
                    {
                        newData.State = newData1.State;
                    }
                }
                else
                {
                    newData.State = BLL.Const.DriverRun_Compile;
                }
            }
            if (!string.IsNullOrEmpty(id))
            {
                Model.DriverRun_DriverRunApprove approve1 = BLL.DriverRunApproveService.GetDriverRunApproveByDriverRunId(id);
                if (approve1 != null && saveType == "submit")
                {
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveIdea = txtOpinions.Text.Trim();
                    BLL.DriverRunApproveService.UpdateDriverRunApprove(approve1);
                }
                if (saveType == "submit" && !string.IsNullOrEmpty(this.drpHandleMan.SelectedValue))
                {
                    Model.DriverRun_DriverRunApprove approve = new Model.DriverRun_DriverRunApprove();
                    approve.DriverRunId = id;
                    if (!string.IsNullOrEmpty(this.drpHandleMan.SelectedValue))
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    if (this.drpHandleType.SelectedValue == BLL.Const.DriverRun_Complete)
                    {
                        approve.ApproveDate = DateTime.Now;
                    }
                    BLL.DriverRunApproveService.AddDriverRunApprove(approve);
                    APICommonService.SendSubscribeMessage(approve.ApproveMan, "开车保运委托单待办理", this.CurrUser.UserName, string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
                }
                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;
                }
                newData.CompileMan = this.CurrUser.UserId;
                newData.CompileDate = DateTime.Now;
                BLL.DriverRunService.AddDriverRun(newData);
                if (saveType == "submit")
                {
                    Model.DriverRun_DriverRunApprove approve1 = new Model.DriverRun_DriverRunApprove();
                    approve1.DriverRunId = newData.DriverRunId;
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.DriverRun_Compile;
                    BLL.DriverRunApproveService.AddDriverRunApprove(approve1);
                    Model.DriverRun_DriverRunApprove approve = new Model.DriverRun_DriverRunApprove();
                    approve.DriverRunId = newData.DriverRunId;
                    if (this.drpHandleMan.SelectedValue != "0")
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    BLL.DriverRunApproveService.AddDriverRunApprove(approve);
                    APICommonService.SendSubscribeMessage(approve.ApproveMan, "开车保运委托单待办理", this.CurrUser.UserName, string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
                }
                else
                {
                    Model.DriverRun_DriverRunApprove approve1 = new Model.DriverRun_DriverRunApprove();
                    approve1.DriverRunId = newData.DriverRunId;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.DriverRun_Compile;
                    BLL.DriverRunApproveService.AddDriverRunApprove(approve1);
                }
            }
        }
        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
        /// 
        /// 把状态转换代号为文字形式
        /// 
        /// 
        /// 
        protected string ConvertState(object state)
        {
            if (state != null)
            {
                if (state.ToString() == BLL.Const.DriverRun_Compile)
                {
                    return "编制";
                }
                else if (state.ToString() == BLL.Const.DriverRun_Audit1)
                {
                    return "开车负责人审批";
                }
                else if (state.ToString() == BLL.Const.DriverRun_Audit2)
                {
                    return "保运主任审批";
                }
                else if (state.ToString() == BLL.Const.DriverRun_Audit3)
                {
                    return "保运主任确认完成";
                }
                else if (state.ToString() == BLL.Const.DriverRun_Audit4)
                {
                    return "开车负责人确认完成";
                }
                else if (state.ToString() == BLL.Const.DriverRun_Complete)
                {
                    return "流程闭环";
                }
                else
                {
                    return "";
                }
            }
            return "";
        }
    }
}