using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using HighWayIot.Winform.UserControlPages.LogPages;
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;

        private List<ZxDailyReportEntity> dailyEntities = new List<ZxDailyReportEntity>();

        /// <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()
        {
            dailyEntities.Clear();
            this.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)
                && x.IsDone == 1);

            _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.ToString(),
                    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),
                });
            }

            List<string> ds1 = new List<string>() { "" };
            ds1.AddRange(_monitorDataSources.Select(x => x.VulcanizationNo).ToList());
            VulcanizationNoCombobox.DataSource = ds1;
            VulcanizationNoCombobox.SelectedIndex = 0;

            List<string> ds2 = new List<string>() { "" };
            ds2.AddRange(_monitorDataSources.Select(x => x.DeviceNo).ToList());
            DeviceNoCombobox.DataSource = ds2;
            DeviceNoCombobox.SelectedIndex = 0;

            List<string> ds3 = new List<string>() { "" };
            ds3.AddRange(_monitorDataSources.Select(x => x.RecipeCode).ToList());
            RecipeCodeCombobox.DataSource = ds3;
            RecipeCodeCombobox.SelectedIndex = 0;

            DataCountLabel.Text = _monitorDataSources.Count.ToString();
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExportTableButton_Click(object sender, EventArgs e)
        {
            ExportPreviewForm exportPreviewForm = new ExportPreviewForm(dailyEntities);
            if (exportPreviewForm.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show("导出成功");
            }
        }

        private void ButtonPanel_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
