using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.Common.DataExporting;
using MWS.Domain.View.History;
using MWS.Library.Common;
using MWS.Library.Common.Module;
using MWS.Library.Enums;
using MWS.Model;
using MWS.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MWS.Domain.View.Alert
{
    /// <summary>
    /// HistoryAlertPage.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryAlertPage : Page
    {
        private readonly IDevice deviceManager;
        private readonly ITag tagManager;
        private readonly IAlert alertManager;
        public List<T_DeviceTag> deviceTags;
        private int mecType = 0;
        private int alertType = 0;
        private int deviceId = 0;
        //private DateTime startTime;
        //private long startTime;
        //private long endTime;
        private DateTime beginTime;
        private DateTime endTime;
        public HistoryAlertPage()
        {
            InitializeComponent();
            tagManager = new TagManager();
            deviceManager = new DeviceManager();
            //alertManager = new AlertManager();
            BindMecTypeComboBox();
            BindAlertComboBox();

        
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            beginTime = DateTime.Now.AddHours(-2);
            endTime = DateTime.Now;
            //startTime = DateTime.Now.AddHours(-24);
            //dpBeginTime.SelectedDate = DateTime.Now.AddHours(-24);
            ////endTime = DateTime.Now;
            //dpEndTime.SelectedDate = DateTime.Now;
            pagination.CurrentPageNumberChanging += Pagination_CurrentPageNumberChanging;
            btDataSelect.Content = beginTime.ToString() + "-" + endTime.ToString();
            //pagination.TotalDataCount = alertManager.GetAllCount(mecType, alertType, startTime, endTime);
            GetListData();
        }
        private void GetListData(int pageIndex = 1)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            beginTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            mecType = Convert.ToInt32(cbMecType.SelectedValue);
            alertType = Convert.ToInt32(cbAlertType.SelectedValue);
            //teamId = Convert.ToInt32(team.SelectedValue);
            PageData<RealTimeAlertDto> pa = alertManager.GetAllRecords(mecType, alertType , beginTime, endTime, pageIndex, pagination.PageDataCount);
            foreach (var item in pa.Data)
            {

                item.MecType = Tool.GetEnumDesc(typeof(MecType), item.MecTypeId);
                item.AlertType = Tool.GetEnumDesc(typeof(AlertType), item.AlertTypeId);
            }
            lstAlert.ItemsSource = pa.Data;

            pagination.TotalDataCount = pa.TotalCount;
            pagination.CurrentPageNumber = pageIndex;
        }
        private void Pagination_CurrentPageNumberChanging(object sender, RoutedEventArgs e)
        {
            GetListData(pagination.CurrentPageNumber);
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetListData();
        }
        /// <summary>
        /// 绑定机构类型
        /// </summary>
        private void BindMecTypeComboBox()
        {
            try
            {
                var query = typeof(MecType).ToArrayList();
                //var mecTypes =
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

                list = (from KeyValuePair<Enum, string> type in query
                        select new KeyValuePair<string, string>(((int)(MecType)type.Key).ToString(), type.Value)).ToList();
                //list.Insert(0, new KeyValuePair<string, string>("0", "全部"));
                //new List<KeyValuePair<string, string>>
                //{
                //    new KeyValuePair<string, string>("0", "全部"),
                //    new KeyValuePair<string, string>("1", "1号料口"),
                //    new KeyValuePair<string, string>("2", "2号料口"),
                //    new KeyValuePair<string, string>("3", "3号料口")
                // };
                cbMecType.ItemsSource = list;
                cbMecType.SelectedValuePath = "Key";
                cbMecType.DisplayMemberPath = "Value";
                cbMecType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        /// <summary>
        /// 绑定机构类型
        /// </summary>
        private void BindAlertComboBox()
        {
            try
            {
                var query = typeof(AlertType).ToArrayList();
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

                list = (from KeyValuePair<Enum, string> type in query
                        select new KeyValuePair<string, string>(((int)(AlertType)type.Key).ToString(), type.Value)).ToList();
                //list.Insert(0, new KeyValuePair<string, string>("0", "全部"));
                //new List<KeyValuePair<string, string>>
                //{
                //    new KeyValuePair<string, string>("0", "全部"),
                //    new KeyValuePair<string, string>("1", "1号料口"),
                //    new KeyValuePair<string, string>("2", "2号料口"),
                //    new KeyValuePair<string, string>("3", "3号料口")
                // };
                cbAlertType.ItemsSource = list;
                cbAlertType.SelectedValuePath = "Key";
                cbAlertType.DisplayMemberPath = "Value";
                cbAlertType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExport_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog
            {
                Title = "Export Excel",
                Filter = "Excel(*.xlsx)|*.xlsx|Excel 97-2003(*.xls)|*.xls",
                ValidateNames = true,
                AddExtension = true,
                FileName = "报警历史" + DateTime.Now.ToString("yyyyMMdd")
            };
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var source = alertManager.GetAllExportRecords(mecType, alertType, beginTime, endTime);
                foreach (var item in source)
                {
                    item.MecType = Tool.GetEnumDesc(typeof(MecType), item.MecTypeId);
                    item.AlertType = Tool.GetEnumDesc(typeof(AlertType), item.AlertTypeId);
                }
               
                if (source != null)
                {
                    new HistoryAlertReportExcelExporter().ExportToFile(fileDialog.FileName, source);
                }
            }

        }
        private void btDataSelect_Click(object sender, RoutedEventArgs e)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            beginTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(beginTime, endTime);
            dateTimeSelectWindow.ShowDialog();
            if ((bool)dateTimeSelectWindow.DialogResult)
            {
                this.beginTime = dateTimeSelectWindow.BeginTime;
                this.endTime = dateTimeSelectWindow.EndTime;
                btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
            };
        }
    }
}
