using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.JDGL.WBS
{
    public partial class CostControlWeekItemDetailEdit : PageBase
    {
        /// 
        /// 控制项主键
        /// 
        public string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Id = Request.Params["Id"];
                Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(this.Id);
                string name = string.Empty;
                if (costControlDetail != null)
                {
                    Model.WBS_CostControl costControl = BLL.CostControlService.GetCostControlByCostControlId(costControlDetail.CostControlId);
                    if (costControl != null)
                    {
                        this.txtCostControlName.Text = costControl.CostControlName;
                    }
                    if (costControlDetail.Months != null)
                    {
                        this.txtMonths.Text = "        月份:" + string.Format("{0:yyyy-MM}", costControlDetail.Months);
                    }
                    if (costControlDetail.PlanNum != null)
                    {
                        this.txtTotalPlanNum.Text = "        本月计划完成量:" + costControlDetail.PlanNum.Value.ToString("F0");
                    }
                    var list = BLL.CostControlDetailService.GetWeekCostControlDetailsByCostControlId(costControlDetail.CostControlId, costControlDetail.Months.Value);
                    this.Grid1.DataSource = list;
                    this.Grid1.DataBind();
                }
            }
        }
        #region 增加按钮事件
        /// 
        /// 增加按钮事件
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            var list = GetDetails();
            Model.WBS_CostControlDetail detail = new Model.WBS_CostControlDetail();
            Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(this.Id);
            detail.CostControlDetailId = SQLHelper.GetNewID();
            detail.Months = costControlDetail.Months;
            detail.StartDate= costControlDetail.Months;
            detail.EndDate = costControlDetail.Months.Value.AddMonths(1).AddDays(-1);
            list.Add(detail);
            Grid1.DataSource = list;
            Grid1.DataBind();
        }
        #endregion
        private List GetDetails()
        {
            List details = new List();
            Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(this.Id);
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.WBS_CostControlDetail detail = new Model.WBS_CostControlDetail();
                detail.CostControlDetailId = Grid1.Rows[i].RowID;
                detail.CostControlId = costControlDetail.CostControlId;
                detail.Months = Funs.GetNewDateTime(values.Value("Months"));
                detail.StartDate = Funs.GetNewDateTime(values.Value("StartDate"));
                detail.EndDate = Funs.GetNewDateTime(values.Value("EndDate"));
                detail.PlanNum = Funs.GetNewDecimal(values.Value("PlanNum"));
                details.Add(detail);
            }
            return details;
        }
        #region 行点击事件
        /// 
        /// Grid行点击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string detailId = Grid1.DataKeys[e.RowIndex][0].ToString();
            var list = GetDetails();
            if (e.CommandName == "del")//删除
            {
                var detail = list.FirstOrDefault(x => x.CostControlDetailId == detailId);
                Model.WBS_CostControlDetail oldDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(detailId);
                if (detail != null)
                {
                    list.Remove(detail);
                }
                if (oldDetail != null)
                {
                    BLL.CostControlDetailService.DeleteCostControlDetail(detailId);
                }
                this.Grid1.DataSource = list;
                this.Grid1.DataBind();
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool hasDate = true;
            decimal totalNum = 0;
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.WBS_CostControlDetail detail = new Model.WBS_CostControlDetail();
                if (string.IsNullOrEmpty(values.Value("StartDate")) || string.IsNullOrEmpty(values.Value("EndDate")))
                {
                    hasDate = false;
                }
                totalNum += Funs.GetNewDecimalOrZero(values.Value("PlanNum"));
            }
            if (!hasDate)
            {
                ShowNotify("开始、结束日期不能为空!", MessageBoxIcon.Warning);
                return;
            }
            Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(this.Id);
            if (costControlDetail.PlanNum != totalNum)
            {
                ShowNotify("周计划累计值必须等于"+ costControlDetail.PlanNum.Value.ToString("F0") + "!", MessageBoxIcon.Warning);
                return;
            }
            var list = GetDetails();
            foreach (var item in list)
            {
                Model.WBS_CostControlDetail oldDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(item.CostControlDetailId);
                if (oldDetail == null)
                {
                    BLL.CostControlDetailService.AddCostControlDetail(item);
                }
                else
                {
                    item.ThisNum = oldDetail.ThisNum;
                    BLL.CostControlDetailService.UpdateCostControlDetail(item);
                }
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }
        #endregion
        #region 调整计划
        /// 
        /// 调整计划按钮
        /// 
        /// 
        /// 
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var list = GetDetails();
            Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(this.Id);
            int versionNum = BLL.CostControlDetailHistoryService.GetWeekMaxVersionNumByCostControlId(costControlDetail.CostControlId, costControlDetail.Months.Value);
            foreach (var item in list)
            {
                Model.WBS_CostControlDetailHistory detailHistory = new Model.WBS_CostControlDetailHistory();
                detailHistory.CostControlDetailHistoryId = SQLHelper.GetNewID();
                detailHistory.CostControlId = item.CostControlId;
                detailHistory.Months = item.Months;
                detailHistory.StartDate = item.StartDate;
                detailHistory.EndDate = item.EndDate;
                detailHistory.PlanNum = item.PlanNum;
                detailHistory.VersionNum = versionNum + 1;
                BLL.CostControlDetailHistoryService.AddCostControlDetailHistory(detailHistory);
            }
            ShowNotify("历史版本保存成功!", MessageBoxIcon.Success);
        }
        #endregion
        #region  查看历史版本
        /// 
        /// 查看历史版本
        /// 
        /// 
        /// 
        protected void btnSee_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CostControlWeekDetailHistoryShow.aspx?Id={0}", this.Id, "编辑 - ")));
        }
        #endregion
    }
}