using System;
using System.Collections.Generic;
using System.Linq;
using BLL;
using Newtonsoft.Json.Linq;

namespace FineUIPro.Web.ZHGL.ServerCheck
{
    public partial class CheckRectifyEdit : PageBase
    {
        /// <summary>
        /// 主键
        /// </summary>
        public string CheckRectifyId
        {
            get
            {
                return (string)ViewState["CheckRectifyId"];
            }
            set
            {
                ViewState["CheckRectifyId"] = value;
            }
        }

        /// <summary>
        /// 定义集合
        /// </summary>
        private static List<Model.View_CheckRectifyListFromSUB> CheckRectifyItems = new List<Model.View_CheckRectifyListFromSUB>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetButtonPower();
                this.CheckRectifyId = Request.Params["CheckRectifyId"];
                if (!string.IsNullOrEmpty(this.CheckRectifyId))
                {
                    var rectify = Funs.DB.Check_CheckRectify.FirstOrDefault(x => x.CheckRectifyId == this.CheckRectifyId);
                    if (rectify != null)
                    {
                        var unit = BLL.UnitService.GetUnitByUnitId(rectify.UnitId);
                        if (unit != null)
                        {
                            this.lbUnitName.Text = unit.UnitName;
                        }

                        this.lbProjectName.Text = rectify.ProjectId;
                        this.lbCheckRectifyCode.Text = rectify.CheckRectifyCode;
                        if (!string.IsNullOrEmpty(rectify.IssueMan))
                        {
                            this.txtIssueMan.Text = rectify.IssueMan;
                        }
                        if (rectify.IssueDate.HasValue)
                        {
                            this.txtIssueDate.Text = string.Format("{0:yyyy-MM-dd}", rectify.IssueDate);
                        }
                        if (rectify.HandleState == BLL.Const.State_3)
                        {
                            this.btnSave.Hidden = true;
                            this.btnSaveUp.Hidden = true;
                        }

                        CheckRectifyItems = (from x in Funs.DB.View_CheckRectifyListFromSUB where x.CheckRectifyId == this.CheckRectifyId orderby x.SortIndex select x).ToList();
                        Grid1.DataSource = CheckRectifyItems;
                        Grid1.DataBind();
                    }
                }
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SavaData(BLL.Const.BtnSave);
        }

        /// <summary>
        /// 保存并提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveUp_Click(object sender, EventArgs e)
        {
            this.SavaData(BLL.Const.BtnSaveUp);
        }

        /// <summary>
        /// 保存数据方法
        /// </summary>
        /// <param name="type"></param>
        private void SavaData(string type)
        {
            jerqueSaveList();
            foreach (var item in CheckRectifyItems)
            {
                var newCheckRectifyItem = BLL.CheckRectifyItemService.GetCheckRectifyItemByCheckRectifyItemId(item.CheckRectifyItemId);
                if (newCheckRectifyItem != null)
                {
                    newCheckRectifyItem.RealEndDate = item.RealEndDate;
                    newCheckRectifyItem.OrderEndPerson = item.OrderEndPerson;
                    newCheckRectifyItem.Verification = item.Verification;
                    BLL.CheckRectifyItemService.UpdateCheckRectifyItem(newCheckRectifyItem);
                }
            }

            var newCheckRectify = BLL.CheckRectifyService.GetCheckRectifyByCheckRectifyId(this.CheckRectifyId);
            if (newCheckRectify != null && newCheckRectify.HandleState != BLL.Const.State_3)
            {
                newCheckRectify.HandleState = BLL.Const.State_2;    //待上报              
                BLL.CheckRectifyService.UpdateCheckRectify(newCheckRectify);
            }
            if (type == BLL.Const.BtnSaveUp)
            {
                string code = CNCECHSSEWebService.UpCheck_CheckRectify(newCheckRectify.CheckRectifyId, this.CurrUser);
                if (code == "1")
                {
                    ShowNotify("同步成功!", MessageBoxIcon.Success);
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                    return;
                }
                else
                {
                    Alert.ShowInParent("同步异常,请退出后重试!", MessageBoxIcon.Error);
                }
            }

            BLL.LogService.AddSys_Log(this.CurrUser, string.Empty, string.Empty, BLL.Const.CheckRectifyMenuId, BLL.Const.BtnModify);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 保存集合
        /// </summary>
        private void jerqueSaveList()
        {
            JArray mergedData = Grid1.GetMergedData();
            foreach (JObject mergedRow in mergedData)
            {
                string status = mergedRow.Value<string>("status");
                JObject values = mergedRow.Value<JObject>("values");
                string checkRectifyItemId = values.Value<string>("CheckRectifyItemId").ToString();
                var item = CheckRectifyItems.FirstOrDefault(e => e.CheckRectifyItemId == checkRectifyItemId);
                if (item != null)
                {
                    item.OrderEndPerson = values.Value<string>("OrderEndPerson").ToString();
                    item.RealEndDate = Funs.GetNewDateTime(values.Value<string>("RealEndDate").ToString());
                    item.Verification = values.Value<string>("Verification").ToString();
                }
            }
        }

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

        #region 关闭按钮事件
        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}