using BLL;
using FastReport.DevComponents.Editors;
using FineUIPro.Web.HSSE.Check;
using FineUIPro.Web.SysManage;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FineUIPro.Web.InformationProject
{
    public partial class ConstructionLogEdit : PageBase
    {
        #region 定义项
        /// <summary>
        /// 主键
        /// </summary>
        private string ConstructionLogId
        {
            get
            {
                return (string)ViewState["ConstructionLogId"];
            }
            set
            {
                ViewState["ConstructionLogId"] = value;
            }
        }
        #endregion

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideRefreshReference();
                this.ConstructionLogId = Request.Params["ConstructionLogId"] ?? SQLHelper.GetNewID();
                var getLog = BLL.ConstructionLogNewService.GetConstructionLogById(this.ConstructionLogId);
                if (getLog == null)
                {
                    getLog = BLL.ConstructionLogNewService.GetConstructionLogByPersonIdDate(this.CurrUser.LoginProjectId, this.CurrUser.UserId, DateTime.Now);
                }
                if (getLog != null)
                {
                    this.ConstructionLogId = getLog.ConstructionLogId;
                    this.txtUnitId.Text = UnitService.getUnitNamesUnitIds(getLog.UnitId);
                    this.txtPersonName.Text = getLog.PersonName;
                    this.hdPersonId.Text = getLog.PersonId;
                    this.txtWorkDate.Text = string.Format("{0:yyyy-MM-dd}", getLog.WorkDate);
                }
                else
                {
                    this.txtUnitId.Text = UnitService.getUnitNamesUnitIds(this.CurrUser.UnitId);
                    this.txtPersonName.Text = this.CurrUser.UserName;
                    this.hdPersonId.Text = this.CurrUser.UserId;
                    this.txtWorkDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }

                BindGrid();
            }
        }
        #endregion

        #region 绑定Grid
        /// <summary>
        /// 绑定Grid
        /// </summary>
        private void BindGrid()
        {
            var getLogItemList = BLL.ConstructionLogNewService.GetConstructionLogItemListByConstructionLogId(this.ConstructionLogId);
            if (getLogItemList.Count() == 0)
            {
                getLogItemList = (from x in DropListService.getExamType()
                                  select new Model.InformationProject_ConstructionLogItem
                                  {
                                      ConstructionLogItemId = SQLHelper.GetNewID(),
                                      ConstructionLogId = this.ConstructionLogId,
                                      ExamTypeId = x.Value,
                                      Frequency = 0,
                                  }).ToList();
            }

            this.Grid1.DataSource = getLogItemList;
            this.Grid1.PageIndex = 0;
            this.Grid1.DataBind();
        }

        /// <summary>
        /// 关闭弹出窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            Model.InformationProject_ConstructionLog newConstructionLog = new Model.InformationProject_ConstructionLog
            {
                ConstructionLogId = this.ConstructionLogId,
                ProjectId = this.CurrUser.LoginProjectId,
                WorkDate = Funs.GetNewDateTimeOrNow(this.txtWorkDate.Text),
                UnitId = this.CurrUser.UnitId,
                PersonId = this.CurrUser.UserId,
                PersonName = this.CurrUser.UserName,
            };

            var getLog = BLL.ConstructionLogNewService.GetConstructionLogById(this.ConstructionLogId);
            if (getLog != null)
            {
                BLL.ConstructionLogNewService.UpdateConstructionLog(newConstructionLog);
            }
            else
            {
                BLL.ConstructionLogNewService.AddConstructionLog(newConstructionLog);
            }

            ShowNotify(this.SaveDetail(), MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        /// <summary>
        ///  保存明细项
        /// </summary>
        private string SaveDetail()
        {
            JArray teamGroupData = Grid1.GetMergedData();
            foreach (JObject teamGroupRow in teamGroupData)
            {
                JObject values = teamGroupRow.Value<JObject>("values");
                string ConstructionLogItemId = teamGroupRow.Value<string>("id");
                Model.InformationProject_ConstructionLogItem newDetail = new Model.InformationProject_ConstructionLogItem
                {
                    ConstructionLogItemId = ConstructionLogItemId,
                    ConstructionLogId = this.ConstructionLogId,
                    ExamTypeId = values.Value<string>("ExamTypeId"),
                    Frequency = Funs.GetNewIntOrZero(values.Value<string>("Frequency")),
                    Remark = values.Value<string>("Remark"),
                };
                var getItem = ConstructionLogNewService.GetConstructionLogItemById(ConstructionLogItemId);
                if (getItem != null)
                {
                    ConstructionLogNewService.UpdateConstructionLogItem(newDetail);
                }
                else
                {
                    ConstructionLogNewService.AddConstructionLogItem(newDetail);
                }
            }

            return "保存成功!";
        }
        #endregion

        #region 获取  字面值,在 ASPX 中调用
        /// <summary>
        /// 获取本部人员角色
        /// </summary>
        /// <param name="states"></param>
        /// <returns></returns>
        protected string GetExamType(object value)
        {
            string name = string.Empty;
            if (value != null)
            {
                name = DropListService.getExamTypeNameByValue(value.ToString());
            }
            return name;
        }

        #endregion

        protected void txtWorkDate_Blur(object sender, EventArgs e)
        {
            var datav = Funs.GetNewDateTime(this.txtWorkDate.Text);
            if (datav.HasValue)
            {
                var getLog = BLL.ConstructionLogNewService.GetConstructionLogByPersonIdDate(this.CurrUser.LoginProjectId, this.CurrUser.UserId, datav.Value);
                if (getLog != null)
                {
                    this.ConstructionLogId = getLog.ConstructionLogId;
                }
                else
                {
                    this.ConstructionLogId = SQLHelper.GetNewID();
                }
                BindGrid();
            }
            else
            {
                Alert.ShowInTop("请选择日期!", MessageBoxIcon.Warning);
                return;
            }
        } 
    }
}