using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
using WSDDBBrowser.Chart;
using WSDDBBrowser.Core;

namespace WSDDBBrowser
{
    /// <summary>
    /// PageOutput.xaml 的交互逻辑
    /// </summary>
    public partial class PgOutput : Page
    {
        PgOutputViewModel viewModel;
        public PgOutput()
        {
            InitializeComponent();
        }
        List<FrameworkElement> charts;
        public void Init()
        {
            if (DBViewerModel.Instance.CurrProcess.HasTGData)
            {
                charts = new List<FrameworkElement>();
                foreach (var scanDataItem in DBViewerModel.Instance.ScanDataItems)
                {
                    var myChart = new TrendGraphExcel()
                    {
                        Width = 1280,
                        Height = 768 * 2 / 3,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        VerticalAlignment = VerticalAlignment.Top
                    };
                    myChart.Init(scanDataItem);

                    Border border = new Border()
                    {
                        Margin = new Thickness(5),
                        Child = myChart
                    };

                    sp.Children.Add(border);
                    charts.Add(myChart);
                }
            }
        }

        private async void ButtonOutput_Click(object sender, RoutedEventArgs e)
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            string filename = $"威仕达现场监控报表_{DBViewerModel.Instance.CurrProcess.OrderNo}_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "xlsx files (*.xlsx)|*.xlsx";
            sfd.InitialDirectory = strDesktopPath;
            sfd.FileName = filename;
            if (sfd.ShowDialog() == true)
            {
                ExcelOuput ouput = new ExcelOuput();
                ouput.charts = charts;

                ouput.SaveToXlsx(sfd.FileName);


            }

        }

        private void ButtonBack_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
            this.NavigationService.RemoveBackEntry();
        }
    }
    public class PgOutputViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
