using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.EditorManage
{
    public partial class LessonsLearnedEditorEdit :PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();//设置权限
                }
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();

                #region 加载下拉列表
                BLL.ConstService.InitConstValueDropDownList(this.drpAppliedDiscip, BLL.Const.LessonsLearned_AppliedDiscip, true); //专业
                BLL.ConstService.InitConstValueDropDownList(this.drpStage, BLL.Const.LessonsLearned_Stage, true); //阶段
                BLL.ConstService.InitConstValueDropDownList(this.drpKeyword, BLL.Const.LessonsLearned_Keyword, true); //关键词
                BLL.Sys_UserService.InitUserDropDownList(this.drpPostBy, true);
                #endregion
                
                string lessonsLearnedId = Request.Params["lessonsLearnedId"];
                if (!string.IsNullOrEmpty(lessonsLearnedId))
                {
                    Model.Editor_LessonsLearned lessonsLearned = BLL.LessonsLearnedService.GetLessonsLearnedById(lessonsLearnedId);
                    if (lessonsLearned != null)
                    {
                        this.txtId.Text = lessonsLearned.Id;
                        this.txtJobNo.Text = lessonsLearned.JobNo;
                        //this.txtPostBy.Text = lessonsLearned.PostBy;
                        if (!string.IsNullOrEmpty(lessonsLearned.PostBy))
                        {
                            var user = BLL.Sys_UserService.GetUserByUserName(lessonsLearned.PostBy);
                            if (user != null)
                            {
                                this.drpPostBy.SelectedValue = user.UserId;
                            }
                        }
                        if (lessonsLearned.EntryDate.HasValue)
                        {
                            this.txtEntryDate.Text = string.Format("{0:yyyy-MM-dd}", lessonsLearned.EntryDate);
                        }
                        if (!string.IsNullOrEmpty(lessonsLearned.AppliedDiscip))
                        {
                            this.drpAppliedDiscip.SelectedValue = BLL.ConstService.GetConstListByTextAndGroupId(lessonsLearned.AppliedDiscip, BLL.Const.LessonsLearned_AppliedDiscip).ConstValue;
                        }
                        if (!string.IsNullOrEmpty(lessonsLearned.Stage))
                        {
                            this.drpStage.SelectedValue = BLL.ConstService.GetConstListByTextAndGroupId(lessonsLearned.Stage, BLL.Const.LessonsLearned_Stage).ConstValue;
                        }
                        if (!string.IsNullOrEmpty(lessonsLearned.Keyword))
                        {
                            this.drpKeyword.SelectedValue = BLL.ConstService.GetConstListByTextAndGroupId(lessonsLearned.Keyword, BLL.Const.LessonsLearned_Keyword).ConstValue;
                        }
                        this.txtDescription.Text = lessonsLearned.Description;
                        this.txtRootCause.Text = lessonsLearned.RootCause;
                        this.txtLessonLearned.Text = lessonsLearned.LessonLearned;


                        if (this.CurrUser.UserId == this.drpPostBy.SelectedValue || this.CurrUser.UserId == BLL.Const.GlyId)
                        {
                            this.btnSave.Hidden = false;
                        }
                        else
                        {
                            this.btnSave.Hidden = true;
                        }
                    }
                }
                else
                {
                    string eProjectId = Request.Params["eProjectId"];
                    if (!string.IsNullOrEmpty(eProjectId))
                    {
                        var epr = BLL.EProjectService.GeteProjectById(eProjectId);
                        if (epr != null)
                        {
                            this.txtJobNo.Text = epr.ProjectControl_JobNo;
                            this.txtId.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCodeByJobNo3", "dbo.Editor_LessonsLearned", "Id", epr.ProjectControl_JobNo + "-");
                        }
                    }
                    //this.txtPostBy.Text = this.CurrUser.UserName;
                    this.drpPostBy.SelectedValue = this.CurrUser.UserId;
                    this.txtEntryDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {            
            string lessonsLearnedId = Request.Params["lessonsLearnedId"];
            Model.Editor_LessonsLearned newLessonsLearned = new Model.Editor_LessonsLearned();
            newLessonsLearned.Id = this.txtId.Text.Trim();
            newLessonsLearned.JobNo = this.txtJobNo.Text.Trim();
            //newLessonsLearned.PostBy = this.txtPostBy.Text.Trim();
            if(this.drpPostBy.SelectedValue!=BLL.Const._Null)
            {
                newLessonsLearned.PostBy = this.drpPostBy.SelectedItem.Text.Trim();
            }
            newLessonsLearned.EntryDate = Funs.GetNewDateTime(this.txtEntryDate.Text.Trim());
            if (this.drpAppliedDiscip.SelectedValue != BLL.Const._Null)
            {
                newLessonsLearned.AppliedDiscip = this.drpAppliedDiscip.SelectedItem.Text;
            }
            if (this.drpStage.SelectedValue != BLL.Const._Null)
            {
                newLessonsLearned.Stage = this.drpStage.SelectedItem.Text;
            }
            if (this.drpKeyword.SelectedValue != BLL.Const._Null)
            {
                newLessonsLearned.Keyword = this.drpKeyword.SelectedItem.Text;
            }
            newLessonsLearned.Description = this.txtDescription.Text.Trim();
            newLessonsLearned.RootCause = this.txtRootCause.Text.Trim();
            newLessonsLearned.LessonLearned = this.txtLessonLearned.Text.Trim();
            if (!string.IsNullOrEmpty(lessonsLearnedId))
            {
                newLessonsLearned.LessonsLearnedId = lessonsLearnedId;
                BLL.LessonsLearnedService.UpdateLessonsLearned(newLessonsLearned);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Lessons Learned Editor information!");
            }
            else
            {
                newLessonsLearned.EProjectId = Request.Params["eProjectId"];
                lessonsLearnedId = SQLHelper.GetNewID(typeof(Model.Editor_LessonsLearned));
                newLessonsLearned.LessonsLearnedId = lessonsLearnedId;
                BLL.LessonsLearnedService.AddLessonsLearned(newLessonsLearned);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Lessons Learned Editor information!");
            }
            ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.LessonsLearnedEditorMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}