using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HighWayIot.Winform.UserControlPages
{
    public partial class DailyReportPage : UserControl
    {
        /// <summary>
        /// 报表服务类
        /// </summary>
        private ZxDailyReportService _zxDailyReportService = ZxDailyReportService.Instance;

        /// <summary>
        /// datagridview 数据源
        /// 必须使用BindingList， 如果使用LIST，无法实现更改，添加、删除数据源自动更新datagridview
        /// </summary>
        private BindingList<MonitorDataSource> _monitorDataSources = new BindingList<MonitorDataSource>();

        public DailyReportPage()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            ReportDataGridView.AutoGenerateColumns = false;

            List<string> recipeCodes = new List<string>()
            {
                ""
            };
            recipeCodes.AddRange(ZxRecipeService.Instance.GetRecipeInfos().Select(x => x.RecipeCode).ToList());

            List<string> workStationNos = new List<string>()
            {
                ""
            };
            workStationNos.AddRange(ZxReaderSettingService.Instance.GetReaderInfos().Select(x => x.WorkstationNo).ToList());

            List<string> deviceNos = new List<string>()
            {
                ""
            };
            deviceNos.AddRange(ZxTagSettingService.Instance.GetTagInfos().Select(x => x.DeviceNo).ToList());

            RecipeCodeCombobox.DataSource = recipeCodes;
            VulcanizationNoCombobox.DataSource = workStationNos;
            DeviceNoCombobox.DataSource = deviceNos;

            SelectStartTime.Value = DateTime.Now.AddDays(-1);
            SelectEndTime.Value = DateTime.Now;

            ReportDataGridView.DataSource = null;
            ReportDataGridView.DataSource = _monitorDataSources;
            DataRefresh();
        }

        private void SelectReport_Click(object sender, EventArgs e)
        {
            DataRefresh();
        }

        private void DataRefresh()
        {
            List<ZxDailyReportEntity> dailyEntities =
                _zxDailyReportService.GetDailyReportInfos(x =>
                x.StartTime >= SelectStartTime.Value
                && x.StartTime <= SelectEndTime.Value
                && (string.IsNullOrEmpty(RecipeCodeCombobox.Text) || x.RecipeCode == RecipeCodeCombobox.Text)
                && (string.IsNullOrEmpty(VulcanizationNoCombobox.Text) || x.VulcanizationNo == VulcanizationNoCombobox.Text)
                && (string.IsNullOrEmpty(DeviceNoCombobox.Text) || x.DeviceNo.ToString() == DeviceNoCombobox.Text));

            _monitorDataSources.Clear();

            for (int i = 0; i < dailyEntities.Count; i++)
            {
                _monitorDataSources.Add(new MonitorDataSource()
                {
                    No = i + 1,
                    VulcanizationNo = dailyEntities[i].VulcanizationNo,
                    StartTime = dailyEntities[i].StartTime.ToString("MM月dd日 HH:mm:ss"),
                    RecipeName = dailyEntities[i].RecipeName,
                    RecipeCode = dailyEntities[i].RecipeCode,
                    SpecCode = dailyEntities[i].SpecCode,
                    DeviceNo = dailyEntities[i].DeviceNo,
                    RawTireWeight = dailyEntities[i].RawTireWeight ?? 0,
                    BaseRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntities[i].StartTime, dailyEntities[i].BaseEndTime),
                    MidRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntities[i].StartTime, dailyEntities[i].MidEndTime),
                    FaceRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntities[i].StartTime, dailyEntities[i].FaceEndTime),
                });
            }
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExportTableButton_Click(object sender, EventArgs e)
        {

        }
    }
}
