using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace FineUIPro.Web.TestRun.DriverReport
{
    public partial class MonthReportEdit : PageBase
    {
        #region 定义集合
        /// 
        /// 工作包完成情况统计
        /// 
        public static List monthReportItems = new List();
        /// 
        /// 开车绩效测量数据
        /// 
        public static List monthReportItems2 = new List();
        #endregion
        #region 页面加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    Model.TestRun_MonthReport data = BLL.TestRun_MonthReportService.GetMonthReportById(id);
                    if (data != null)
                    {
                        this.hdId.Text = id;
                        this.txtMonthReportCode.Text = data.MonthReportCode;
                        this.txtMonthReportDate.Text = data.MonthReportDate.HasValue ? string.Format("{0:yyyy-MM-dd}", data.MonthReportDate) : "";
                        this.txtProjectOverview.Text = data.ProjectOverview;
                        this.txtProjectBaseInfo.Text = data.ProjectBaseInfo;
                        this.txtEngineeringPlant.Text = data.EngineeringPlant;
                        this.txtProjectContract.Text = data.ProjectContract;
                        this.txtMilestone.Text = data.Milestone;
                        this.txtCompleteWork.Text = data.CompleteWork;
                        this.txtProblemSituation.Text = data.ProblemSituation;
                        this.txtBudget.Text = data.Budget.HasValue ? data.Budget.ToString() : "";
                        this.txtBudgetTotal.Text = data.BudgetTotal.HasValue ? data.BudgetTotal.ToString() : "";
                        this.txtActualCost.Text = data.ActualCost.HasValue ? data.ActualCost.ToString() : "";
                        this.txtActualCostTotal.Text = data.ActualCostTotal.HasValue ? data.ActualCostTotal.ToString() : "";
                        this.txtPublicationStatus.Text = data.PublicationStatus;
                        this.txtImplementationStatus.Text = data.ImplementationStatus;
                        this.txtHSEManageSituation.Text = data.HSEManageSituation;
                        this.txtNextMonthMilestone.Text = data.NextMonthMilestone;
                        this.txtNextMonthCompleteWork.Text = data.NextMonthCompleteWork;
                        this.txtNextMonthSchedule.Text = data.NextMonthSchedule;
                        this.txtNextMonthDrivingCost.Text = data.NextMonthDrivingCost;
                        this.txtProblemsMeasures.Text = data.ProblemsMeasures;
                        this.txtSolvedProblems.Text = data.SolvedProblems;
                    }
                    var items = BLL.TestRun_MonthReportItemService.GetMonthReportItemByReportId(this.hdId.Text);
                    if (items.Count > 0)
                    {
                        this.Grid1.DataSource = items;
                        this.Grid1.DataBind();
                    }
                    var items2 = BLL.TestRun_MonthReportItem2Service.GetMonthReportItem2ByReportId(this.hdId.Text, "1");
                    if (items2.Count > 0)
                    {
                        this.Grid2.DataSource = items2;
                        this.Grid2.DataBind();
                    }
                    var items3 = BLL.TestRun_MonthReportItem2Service.GetMonthReportItem2ByReportId(this.hdId.Text, "2");
                    if (items3.Count > 0)
                    {
                        this.Grid3.DataSource = items3;
                        this.Grid3.DataBind();
                    }
                    WindowAtt_Close(null, null);
                }
                else
                {
                    this.txtMonthReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    //新增月报时填充上一条月报的项目概况内容
                    var report = BLL.TestRun_MonthReportService.GetReportByMaxDate(this.CurrUser.LoginProjectId);
                    if (report != null)
                    {
                        this.txtProjectOverview.Text = report.ProjectOverview;
                        this.txtProjectBaseInfo.Text = report.ProjectBaseInfo;
                        this.txtEngineeringPlant.Text = report.EngineeringPlant;
                        this.txtProjectContract.Text = report.ProjectContract;
                    }
                    #region 开车绩效测量数据
                    monthReportItems2.Clear();
                    Model.TestRun_MonthReportItem2 newItem = new Model.TestRun_MonthReportItem2();
                    newItem.MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2));
                    newItem.TypeName = "设计";
                    monthReportItems2.Add(newItem);
                    Model.TestRun_MonthReportItem2 newItem2 = new Model.TestRun_MonthReportItem2();
                    newItem2.MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2));
                    newItem2.TypeName = "采购";
                    monthReportItems2.Add(newItem2);
                    Model.TestRun_MonthReportItem2 newItem3 = new Model.TestRun_MonthReportItem2();
                    newItem3.MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2));
                    newItem3.TypeName = "施工";
                    monthReportItems2.Add(newItem3);
                    Model.TestRun_MonthReportItem2 newItem4 = new Model.TestRun_MonthReportItem2();
                    newItem4.MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2));
                    newItem4.TypeName = "开车";
                    monthReportItems2.Add(newItem4);
                    Model.TestRun_MonthReportItem2 newItem5 = new Model.TestRun_MonthReportItem2();
                    newItem5.MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2));
                    newItem5.TypeName = "项目";
                    monthReportItems2.Add(newItem5);
                    this.Grid2.DataSource = monthReportItems2;
                    this.Grid2.DataBind();
                    this.Grid3.DataSource = monthReportItems2;
                    this.Grid3.DataBind();
                    #endregion
                }
                GroupPanel8.Title = "下月开车计划要点(" + Convert.ToDateTime(this.txtMonthReportDate.Text).AddMonths(1).Month + "月份)";
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReport));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverReport/MonthReport&menuId={1}", this.hdId.Text, BLL.Const.MonthReportMenuId)));
        }
        protected void WindowAtt_Close(object sender, WindowCloseEventArgs e)
        {
            this.lbtnAttach.Text = string.Empty;
            string url = string.Empty;
            var attLists = BLL.AttachFileService.Getfiles(this.hdId.Text, BLL.Const.MonthReportMenuId);
            if (attLists != null)
            {
                List lists = Funs.GetStrListByStr(attLists.AttachUrl, ',');
                if (lists.Count > 0)
                {
                    foreach (var item in lists)
                    {
                        url += item.Substring(item.IndexOf('_') + 1) + "\r\n";
                    }
                }
                if (!string.IsNullOrEmpty(url))
                {
                    this.lbtnAttach.Text = url;
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Params["id"];
            Model.TestRun_MonthReport newData = new Model.TestRun_MonthReport();            
            newData.MonthReportCode = this.txtMonthReportCode.Text.Trim();
            newData.MonthReportDate = Funs.GetNewDateTime(this.txtMonthReportDate.Text.Trim());           
            newData.ProjectId = this.CurrUser.LoginProjectId;
            newData.ProjectOverview = this.txtProjectOverview.Text.Trim();
            newData.ProjectBaseInfo = this.txtProjectBaseInfo.Text.Trim();
            newData.EngineeringPlant = this.txtEngineeringPlant.Text.Trim();
            newData.ProjectContract = this.txtProjectContract.Text.Trim();
            newData.Milestone = this.txtMilestone.Text.Trim();
            newData.CompleteWork = this.txtCompleteWork.Text.Trim();
            newData.ProblemSituation = this.txtProblemSituation.Text.Trim();
            newData.Budget = Funs.GetNewDecimal(this.txtBudget.Text.Trim());
            newData.BudgetTotal = Funs.GetNewDecimal(this.txtBudgetTotal.Text.Trim());
            newData.ActualCost = Funs.GetNewDecimal(this.txtActualCost.Text.Trim());
            newData.ActualCostTotal = Funs.GetNewDecimal(this.txtActualCostTotal.Text.Trim());
            newData.PublicationStatus = this.txtPublicationStatus.Text.Trim();
            newData.ImplementationStatus = this.txtImplementationStatus.Text.Trim();
            newData.HSEManageSituation = this.txtHSEManageSituation.Text.Trim();
            newData.NextMonthMilestone = this.txtNextMonthMilestone.Text.Trim();
            newData.NextMonthCompleteWork = this.txtNextMonthCompleteWork.Text.Trim();
            newData.NextMonthSchedule = this.txtNextMonthSchedule.Text.Trim();
            newData.NextMonthDrivingCost = this.txtNextMonthDrivingCost.Text.Trim();
            newData.ProblemsMeasures = this.txtProblemsMeasures.Text.Trim();
            newData.SolvedProblems = this.txtSolvedProblems.Text.Trim();
            if (!string.IsNullOrEmpty(id))
            {
                newData.MonthReportId = id;
                BLL.TestRun_MonthReportService.UpdateMonthReport(newData);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newData.MonthReportId = this.hdId.Text.Trim();
                }
                else
                {
                    newData.MonthReportId = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReport));
                    this.hdId.Text = newData.MonthReportId;
                }
                BLL.TestRun_MonthReportService.AddMonthReport(newData);
            }
            #region 工作包完成情况统计
            BLL.TestRun_MonthReportItemService.DeleteMonthReportItemByMonthReportId(this.hdId.Text);
            jerqueSaveList();
            foreach (var item in monthReportItems)
            {
                item.MonthReportId = this.hdId.Text;
                BLL.TestRun_MonthReportItemService.AddMonthReportItem(item);
            }
            #endregion
            #region 开车绩效测量数据
            BLL.TestRun_MonthReportItem2Service.DeleteMonthReportItem2ByReportId(this.hdId.Text);
            jerqueSaveList2();
            foreach (var item in monthReportItems2)
            {
                item.MonthReportId = this.hdId.Text;
                BLL.TestRun_MonthReportItem2Service.AddMonthReportItem2(item);
            }
            #endregion
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存工作包完成情况统计
        /// 
        private void jerqueSaveList()
        {
            monthReportItems.Clear();
            JArray mergedData = Grid1.GetMergedData();
            foreach (JObject mergedRow in mergedData)
            {
                string status = mergedRow.Value("status");
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.TestRun_MonthReportItem item = new Model.TestRun_MonthReportItem
                {
                    MonthReportItemId = this.Grid1.Rows[i].DataKeys[0].ToString(),
                    SortIndex = values.Value("SortIndex").ToString(),
                    WorkPackageType = values.Value("WorkPackageType").ToString(),
                    Unit = values.Value("Unit").ToString(),
                    DesignCount = Funs.GetNewInt(values.Value("DesignCount").ToString()),
                    MonthPlan = Funs.GetNewInt(values.Value("MonthPlan").ToString()),
                    MonthActual = values.Value("MonthActual").ToString(),
                    TotalPlan = Funs.GetNewInt(values.Value("TotalPlan").ToString()),
                    TatalActual = Funs.GetNewInt(values.Value("TatalActual").ToString()),
                    CompletionRate = values.Value("CompletionRate").ToString(),
                };
                monthReportItems.Add(item);
            }
        }
        /// 
        /// 保存开车绩效测量数据
        /// 
        private void jerqueSaveList2()
        {
            monthReportItems2.Clear();
            int index = 1;
            JArray mergedData = Grid2.GetMergedData();
            foreach (JObject mergedRow in mergedData)
            {
                string status = mergedRow.Value("status");
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.TestRun_MonthReportItem2 item = new Model.TestRun_MonthReportItem2
                {
                    MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2)),
                    ReportType = "1",
                    TypeName = values.Value("TypeName").ToString(),
                    PV = values.Value("PV").ToString(),
                    EV = values.Value("EV").ToString(),
                    AC = values.Value("AC").ToString(),
                    CV = values.Value("CV").ToString(),
                    SV = values.Value("SV").ToString(),
                    CPI = values.Value("CPI").ToString(),
                    SPI = values.Value("SPI").ToString(),
                    SortIndex = index++,
                };
                monthReportItems2.Add(item);
            }
            int index2 = 1;
            JArray mergedData2 = Grid3.GetMergedData();
            foreach (JObject mergedRow in mergedData2)
            {
                string status = mergedRow.Value("status");
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.TestRun_MonthReportItem2 item = new Model.TestRun_MonthReportItem2
                {
                    MonthReportItem2Id = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem2)),
                    ReportType = "2",
                    TypeName = values.Value("TypeName").ToString(),
                    PV = values.Value("PV").ToString(),
                    EV = values.Value("EV").ToString(),
                    AC = values.Value("AC").ToString(),
                    CV = values.Value("CV").ToString(),
                    SV = values.Value("SV").ToString(),
                    CPI = values.Value("CPI").ToString(),
                    SPI = values.Value("SPI").ToString(),
                    SortIndex = index2++,
                };
                monthReportItems2.Add(item);
            }
        }
        #endregion
        #region 增行按钮
        /// 
        /// 新增
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            jerqueSaveList();
            Model.TestRun_MonthReportItem newItem = new Model.TestRun_MonthReportItem
            {
                MonthReportItemId = SQLHelper.GetNewID(typeof(Model.TestRun_MonthReportItem))
            };
            monthReportItems.Add(newItem);
            this.Grid1.DataSource = monthReportItems;
            this.Grid1.DataBind();
        }
        #endregion
        #region 行事件
        /// 
        /// 删除
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            jerqueSaveList();
            string rowID = this.Grid1.DataKeys[e.RowIndex][0].ToString();
            if (e.CommandName == "Delete")
            {
                foreach (var item in monthReportItems)
                {
                    if (item.MonthReportItemId == rowID)
                    {
                        monthReportItems.Remove(item);
                        break;
                    }
                }
                Grid1.DataSource = monthReportItems;
                Grid1.DataBind();
                ShowNotify("删除数据成功!", MessageBoxIcon.Success);
            }
        }
        #endregion       
    }
}