using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.EditorManage
{
    public partial class AreaConcernEditorEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string type = Request.Params["type"];
                if (type == "2")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();//权限设置
                }

                BLL.Sys_UserService.InitUserDropDownList(this.drpCreatedBy, true);
                BLL.ConstService.InitConstIdDropDownList(this.drpCategoryCause, BLL.Const.AreaCencerns_Category, true);

                string areaConcernId = Request.Params["areaConcernId"];
                if (areaConcernId != null)
                {
                    Model.Editor_AreaConcern areaConcern = BLL.AreaConcernService.GetAreaConcernById(areaConcernId);
                    if (areaConcern != null)
                    {
                        this.txtSN.Text = areaConcern.SN;
                        if (areaConcern.EntryDate.HasValue)
                        {
                            this.txtEntryDate.Text = string.Format("{0:yyyy-MM-dd}", areaConcern.EntryDate);
                        }
                        if (!string.IsNullOrEmpty(areaConcern.CreatedById))
                        {
                            this.drpCreatedBy.SelectedValue = areaConcern.CreatedById;
                        }
                        if (!string.IsNullOrEmpty(areaConcern.CategoryCauseId))
                        {
                            this.drpCategoryCause.SelectedValue = areaConcern.CategoryCauseId;
                        }
                        this.txtRemark.Text = areaConcern.Remark;
                        if (areaConcern.CreatedByName == this.CurrUser.UserName || 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 eproject = BLL.EProjectService.GeteProjectById(eprojectId);
                        if (eproject != null)
                        {
                            this.txtSN.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCodeByJobNo3", "dbo.Editor_AreaConcern", "SN", eproject.ProjectControl_JobNo + "-");
                        }
                    }
                    this.txtEntryDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.drpCreatedBy.SelectedValue = this.CurrUser.UserId;
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string areaConcernId = Request.Params["areaConcernId"];
            Model.Editor_AreaConcern areaConcern = new Model.Editor_AreaConcern();
            areaConcern.SN = this.txtSN.Text.Trim();
            areaConcern.EntryDate = Funs.GetNewDateTime(this.txtEntryDate.Text.Trim());
            if (this.drpCreatedBy.SelectedValue != BLL.Const._Null)
            {
                areaConcern.CreatedById = this.drpCreatedBy.SelectedValue;
                areaConcern.CreatedByName = this.drpCreatedBy.SelectedItem.Text;
            }
            if (this.drpCategoryCause.SelectedValue != BLL.Const._Null)
            {
                areaConcern.CategoryCauseId = this.drpCategoryCause.SelectedValue;
                areaConcern.CategoryCauseName = this.drpCategoryCause.SelectedItem.Text;
            }
            areaConcern.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(areaConcernId))
            {
                areaConcern.AreaConcernId = areaConcernId;
                BLL.AreaConcernService.UpdateAreaConcern(areaConcern);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Area Concern Editor information!");
            }
            else
            {
                areaConcern.EProjectId = Request.Params["eProjectId"];
                areaConcernId = SQLHelper.GetNewID(typeof(Model.Editor_AreaConcern));
                areaConcern.AreaConcernId = areaConcernId;
                BLL.AreaConcernService.AddAreaConcern(areaConcern);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Area Concern Editor information!");
            }
            ShowNotify("Save Successfully!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

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