using MahApps.Metro.Controls;
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.Shapes;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für CheckParkausweis.xaml
    /// </summary>
    public partial class CheckParkausweis : MetroWindow
    {
        public CheckParkausweis()
        {
            InitializeComponent();

            var list = SQL.ReadSQL($"SELECT Kennzeichen, MA_Vorname AS Vorname, MA_Name AS Nachname, Telefon, Abteilung, Vorgesetzter, isKrad FROM {MainWindow.table}.parkausweise WHERE Genehmigt = '1'").Result;
            dgCheck.DataContext = list;
        }


        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var list = SQL.ReadSQL($"SELECT Kennzeichen, MA_Vorname AS Vorname, MA_Name AS Nachname, Telefon, Abteilung, Vorgesetzter, isKrad FROM {MainWindow.table}.parkausweise WHERE Genehmigt = '1' AND Kennzeichen LIKE '%{tbSearch.Text}%'").Result;
            dgCheck.DataContext = list;
        }

        private void tbSearch_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (tbSearch.Text == "")
                lblSearch.Visibility = Visibility.Visible;
        }

        private void tbSearch_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
                lblSearch.Visibility = Visibility.Hidden;
        }

        private void lblSearch_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                var list = SQL.ReadSQL($"SELECT Kennzeichen, MA_Vorname AS Vorname, MA_Name AS Nachname, Telefon, Abteilung, Vorgesetzter FROM {MainWindow.table}.parkausweise WHERE Genehmigt = '1' AND Kennzeichen LIKE '%{tbSearch.Text}%'").Result;
                dgCheck.DataContext = list;
            }
        }

        private async void btnExport_Click(object sender, RoutedEventArgs e)
        {
            gridSpinner.Visibility = Visibility.Visible;

            await Task.Run(() =>
            {
                var res = SQL.ReadSQL($"SELECT Kennzeichen, MA_Vorname AS Vorname, MA_Name AS Nachname, Telefon, Abteilung, Vorgesetzter FROM {MainWindow.table}.parkausweise WHERE Gedruckt = '1'").Result;
                var ex = new ExcelExporter();
                ex.Export(res);
            });

            gridSpinner.Visibility = Visibility.Collapsed;
        }



        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }
    }
}
