using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.Opinion
{
    public partial class OpinionCollectionEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string OpinionCollectionId
        {
            get
            {
                return (string)ViewState["OpinionCollectionId"];
            }
            set
            {
                ViewState["OpinionCollectionId"] = value;
            }
        }
        /// 
        /// 主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        /// 
        /// 状态
        /// 
        public string State
        {
            get
            {
                return (string)ViewState["State"];
            }
            set
            {
                ViewState["State"] = value;
            }
        }
        /// 
        /// 定义集合
        /// 
        private static List approveLists = new List();
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
                this.ProjectId = this.CurrUser.LoginProjectId;
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList1(this.drpUnitId, this.ProjectId, true);
                BLL.UserService.InitUserDropDownList(this.drpCompileMan, this.ProjectId, true);
                BLL.ConstValue.InitConstNameDropDownList(this.drpProblemClass, "MenuType_P", true);
                BLL.ProjectUserService.InitProjectUserRoleIdDropDownList(this.drpAuditRoleId, this.ProjectId, false);
                this.drpAuditRoleId.SelectedValue = BLL.Const.QAManager;
                BLL.UserService.InitUserByRoleIdDropDownList(this.drpAudit, this.ProjectId, this.drpAuditRoleId.SelectedValue, false);
                this.OpinionCollectionId = Request.Params["opinionCollectionId"];
                var opinionCollection = OpinionCollectionService.GetOpinionCollectionById(this.OpinionCollectionId);
                if (opinionCollection != null)
                {
                    this.State = opinionCollection.State.Trim();
                    this.ProjectId = opinionCollection.ProjectId;
                    this.txtOpinion.Text = opinionCollection.Opinion;
                    if (!string.IsNullOrEmpty(opinionCollection.CompileMan))
                    {
                        this.drpCompileMan.SelectedValue = opinionCollection.CompileMan;
                    }
                    this.txtCompileDate.Text = opinionCollection.CompileDate.HasValue ? string.Format("{0:yyyy-MM-dd}", opinionCollection.CompileDate) : "";
                    if (!string.IsNullOrEmpty(opinionCollection.RoleId))
                    {
                        this.hdRoleId.Text = opinionCollection.RoleId;
                        this.txtRole.Text = BLL.RoleService.getRoleNamesRoleIds(opinionCollection.RoleId);
                    }
                    if (!string.IsNullOrEmpty(opinionCollection.UnitId))
                    {
                        this.drpUnitId.SelectedValue = opinionCollection.UnitId;
                    }
                    if (!string.IsNullOrEmpty(opinionCollection.ProblemClass))
                    {
                        this.drpProblemClass.SelectedValue = opinionCollection.ProblemClass;
                    }
                    #region 加载核实人角色、核实人
                    var approveList = BLL.OpinionCollectionApproveService.GetApproveListByOpinionCollectionId(this.OpinionCollectionId);
                    if (approveList.Count > 0)
                    {
                        var app = approveList.FirstOrDefault(x => x.ApproveDate == null && x.ApproveMan != this.CurrUser.UserId);
                        if (app != null)
                        {
                            this.drpAuditRoleId.SelectedValue = app.ApproveManRoleId;
                            this.drpAudit.Items.Clear();
                            BLL.UserService.InitUserByRoleIdDropDownList(this.drpAudit, this.ProjectId, this.drpAuditRoleId.SelectedValue, false);
                            this.drpAudit.SelectedValue = app.ApproveMan;
                        }
                    }
                    #endregion
                    //加载审批流程
                    approveLists = BLL.OpinionCollectionApproveService.getViewApproveLists(this.OpinionCollectionId);
                    if (approveLists.Count > 0)
                    {
                        this.Grid1.Hidden = false;
                        this.Grid1.DataSource = approveLists;
                        this.Grid1.DataBind();
                    }
                    else
                    {
                        this.Grid1.Hidden = true;
                    }
                }
                else
                {
                    this.Grid1.Hidden = true;
                    //this.agree.Hidden = true;
                    this.options.Hidden = true;
                    this.nextStep.Hidden = true;
                    if (!string.IsNullOrEmpty(this.CurrUser.UserId))
                    {
                        var user = BLL.UserService.GetUserByUserId(this.CurrUser.UserId);
                        if (user != null)
                        {
                            this.drpCompileMan.SelectedValue = this.CurrUser.UserId;
                            //this.drpRoleId.SelectedValue = user.RoleId;
                            this.drpUnitId.SelectedValue = user.UnitId;
                            var projectUser = BLL.ProjectUserService.GetCurrProjectUserByUserId(this.CurrUser.UserId);
                            if (projectUser != null)
                            {
                                if (!string.IsNullOrEmpty(projectUser.RoleId))
                                {
                                    this.hdRoleId.Text = projectUser.RoleId;
                                    this.txtRole.Text = BLL.RoleService.getRoleNamesRoleIds(projectUser.RoleId);
                                }
                            }
                        }
                    }
                    this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
                if (this.State == BLL.Const.State_0)
                {
                    //this.agree.Hidden = true;
                    this.options.Hidden = true;
                    this.nextStep.Hidden = true;
                }
                else
                {
                    var approveList = BLL.OpinionCollectionApproveService.GetApproveListByOpinionCollectionId(this.OpinionCollectionId);
                    if (approveList.Count > 0)
                    {
                        var app = approveList.FirstOrDefault(x => x.ApproveDate == null && x.ApproveMan == this.CurrUser.UserId);
                        if (app != null)
                        {
                            //agree.Hidden = false;
                            options.Hidden = false;
                            this.nextStep.Hidden = false;
                        }
                        else
                        {
                            //agree.Hidden = true;
                            options.Hidden = true;
                            nextStep.Hidden = true;
                            this.btnSubmit.Hidden = true;
                            this.btnSave.Hidden = true;
                        }
                    }
                }
            }
        }
        #endregion
        #region 提交按钮
        /// 
        /// 提交按钮
        /// 
        /// 
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData(BLL.Const.BtnSubmit);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        #endregion
        #region 保存按钮
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData(BLL.Const.BtnSave);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 保存方法
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(string type)
        {
            Model.Opinion_OpinionCollection opinionCollection = new Model.Opinion_OpinionCollection
            {
                ProjectId = this.ProjectId,
                Opinion = this.txtOpinion.Text.Trim(),
                CompileDate = Funs.GetNewDateTime(this.txtCompileDate.Text.Trim()),
                RoleId = this.hdRoleId.Text.Trim()
            };
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                opinionCollection.UnitId = this.drpUnitId.SelectedValue;
            }
            if (this.drpProblemClass.SelectedValue != BLL.Const._Null)
            {
                opinionCollection.ProblemClass = this.drpProblemClass.SelectedValue;
            }
            ////单据状态
            opinionCollection.State = BLL.Const.State_0.Trim();
            if (!string.IsNullOrEmpty(this.OpinionCollectionId))
            {
                opinionCollection.OpinionCollectionId = this.OpinionCollectionId;
                BLL.OpinionCollectionService.UpdateOpinionCollection(opinionCollection);
            }
            else
            {
                opinionCollection.CompileMan = this.CurrUser.UserId;
                opinionCollection.OpinionCollectionId = SQLHelper.GetNewID(typeof(Model.Opinion_OpinionCollection));
                this.OpinionCollectionId = opinionCollection.OpinionCollectionId;               
                BLL.OpinionCollectionService.AddOpinionCollection(opinionCollection);
            }
            if (type == Const.BtnSubmit)
            {
                var approves = BLL.OpinionCollectionApproveService.GetApproveListByOpinionCollectionId(this.OpinionCollectionId);
                if (approves.Count == 0)
                {
                    if (this.CurrUser.UserId == this.drpCompileMan.SelectedValue)
                    {
                        Model.Opinion_OpinionCollectionApprove newApprove = new Model.Opinion_OpinionCollectionApprove();
                        newApprove.OpinionCollectionApproveId = SQLHelper.GetNewID(typeof(Model.Opinion_OpinionCollectionApprove));
                        newApprove.OpinionCollectionId = this.OpinionCollectionId;
                        if (this.drpAuditRoleId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpAuditRoleId.SelectedValue))
                        {
                            newApprove.ApproveManRoleId = this.drpAuditRoleId.SelectedValue;
                        }
                        if (this.drpAudit.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpAudit.SelectedValue))
                        {
                            newApprove.ApproveMan = this.drpAudit.SelectedValue;
                        }
                        BLL.OpinionCollectionApproveService.AddOpinionCollectionApprove(newApprove);
                    }
                }
                else
                {
                    var approve = approves.Where(x => x.ApproveDate == null && x.ApproveMan == this.CurrUser.UserId).FirstOrDefault();
                    if (approve != null)
                    {
                        approve.ApproveDate = DateTime.Now;
                        //if (rblIsAgree.SelectedValue == "true")
                        //{
                        //    approve.IsAgree = true;
                        //}
                        //else
                        //{
                        //    approve.IsAgree = false;
                        //}
                        approve.ApproveIdea = this.txtidea.Text.Trim();
                        BLL.OpinionCollectionApproveService.UpdateOpinionCollectionApprove(approve);
                        if (this.drpNextStep.SelectedValue == "下一步")
                        {
                            Model.Opinion_OpinionCollectionApprove newApprove = new Model.Opinion_OpinionCollectionApprove();
                            newApprove.OpinionCollectionApproveId = SQLHelper.GetNewID(typeof(Model.Opinion_OpinionCollectionApprove));
                            newApprove.OpinionCollectionId = this.OpinionCollectionId;
                            if (this.drpAuditRoleId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpAuditRoleId.SelectedValue))
                            {
                                newApprove.ApproveManRoleId = this.drpAuditRoleId.SelectedValue;
                            }
                            if (this.drpAudit.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpAudit.SelectedValue))
                            {
                                newApprove.ApproveMan = this.drpAudit.SelectedValue;
                            }
                            BLL.OpinionCollectionApproveService.AddOpinionCollectionApprove(newApprove);
                        }
                    }
                }
                //更改状态
                var opin = BLL.OpinionCollectionService.GetOpinionCollectionById(this.OpinionCollectionId);
                if (opin!=null)
                {
                    //if (rblIsAgree.SelectedValue == "true")
                    //{
                        if (this.drpNextStep.SelectedValue == "审核完成")
                        {
                            opin.State = BLL.Const.State_2;
                        }
                        else
                        {
                            opin.State = BLL.Const.State_1;
                        }
                    //}
                    //else
                    //{
                    //    opin.State = BLL.Const.State_0;
                    //}
                    BLL.OpinionCollectionService.UpdateOpinionCollection(opin);
                }
            }
        }
        #endregion
        
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (this.drpProblemClass.SelectedValue == BLL.Const._Null)
            {
                ShowNotify("请选择问题分类!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(this.OpinionCollectionId))
            {
                this.SaveData(BLL.Const.BtnSave);
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/OpinionCollection&menuId={1}&type=0", this.OpinionCollectionId, BLL.Const.OpinionCollectionMenuId)));
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 核实人角色下拉选择事件
        /// 
        /// 
        /// 
        protected void drpAuditRoleId_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpAudit.Items.Clear();
            if (!string.IsNullOrEmpty(this.drpAuditRoleId.SelectedValue)&&this.drpAuditRoleId.SelectedValue!=BLL.Const._Null)
            {
                BLL.UserService.InitUserByRoleIdDropDownList(this.drpAudit, this.ProjectId, this.drpAuditRoleId.SelectedValue, false);
                this.drpAudit.SelectedIndex = 0;
            }
        }
        /// 
        ///下一步选择事件
        /// 
        /// 
        /// 
        protected void drpNextStep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpNextStep.SelectedText=="审核完成")
            {
                audit.Hidden = true;
            }
            else
            {
                audit.Hidden = false;
            }
        }
        #endregion
    }
}