using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Data;
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 ArrivalsOverview.xaml
    /// </summary>
    public partial class ArrivalsOverview : MetroWindow
    {
        Roles LoggedInRole = Roles.None;

        DataTable overv = new DataTable("Overview");
        DataTable todayFirm = new DataTable("TodayFirm");
        DataTable todayBesuch = new DataTable("TodayBesuch");



        public ArrivalsOverview(Roles role)
        {            
            InitializeComponent();

            LoggedInRole = role;

            dPOverviewVon.SelectedDate = DateTime.Now.Date;
            dPOverviewBis.SelectedDate = DateTime.Now.Date.AddDays(14);
            FillDataGrid(DateTime.Now.Date, DateTime.Now.Date.AddDays(14));            
        }


        private async void FillDataGrid(DateTime von, DateTime bis)
        {
            if (LoggedInRole == Roles.Admin || LoggedInRole == Roles.Pförtner || LoggedInRole == Roles.M2_AM)
            {
                todayFirm = await SQL.ReadSQL($"Select * From {MainWindow.table}.firmen where (Arbeitsbeginn >= '{von.ToString("yyyy-MM-dd")}' AND Arbeitsende <= '{bis.ToString("yyyy-MM-dd")}') OR '{von.ToString("yyyy-MM-dd")}' BETWEEN Arbeitsbeginn AND Arbeitsende", todayFirm);
                todayBesuch = await SQL.ReadSQL($"SELECT idbesucher, concat('Besucher: ', Name) AS Name, Verantwortlicher_MA AS Verantwortlicher_MA_Firma,Tel_Nr_Besucher AS Tel_Nr_Verantwortlicher_Firma,Besuchstag AS Arbeitsbeginn,Grund_des_Besuchs AS Beauftragte_Leistung,Ansprechpartner_Intern from {MainWindow.table}.besucher WHERE (Besuchstag >= '{von.ToString("yyyy-MM-dd")}' AND Besuchstag <= '{bis.ToString("yyyy-MM-dd")}')", todayBesuch);
            }
            else
            {
                todayFirm = await SQL.ReadSQL($"Select * From {MainWindow.table}.firmen where ((Arbeitsbeginn >= '{von.ToString("yyyy-MM-dd")}' AND Arbeitsende <= '{bis.ToString("yyyy-MM-dd")}') OR '{von.ToString("yyyy-MM-dd")}' BETWEEN Arbeitsbeginn AND Arbeitsende) AND (Ersteller = '{Environment.UserName}' " +
                    $"OR Ersteller = '{Environment.UserDomainName}{Environment.UserName}' " +
                    $"OR Ersteller = '{SQL.ReadSingleValue($"SELECT Vertreter FROM {MainWindow.table}.ansprechpartner WHERE Name = '{Helper.InsertSpaceBeforeUpperCase(Environment.UserName)}'")}' " +
                    $"OR Ansprechpartner_Intern = '{Helper.InsertSpaceBeforeUpperCase(Environment.UserName)}')", todayFirm);
                todayBesuch = await SQL.ReadSQL($"SELECT idbesucher, concat('Besucher: ', Name) AS Name, Verantwortlicher_MA AS Verantwortlicher_MA_Firma,Tel_Nr_Besucher AS Tel_Nr_Verantwortlicher_Firma,Besuchstag AS Arbeitsbeginn,Grund_des_Besuchs AS Beauftragte_Leistung,Ansprechpartner_Intern from {MainWindow.table}.besucher WHERE (Besuchstag >= '{von.ToString("yyyy-MM-dd")}' AND Besuchstag <= '{bis.ToString("yyyy-MM-dd")}') AND (Ersteller = '{Environment.UserName}' " +
                    $"OR Ersteller = '{Environment.UserDomainName}{Environment.UserName}' " +
                    $"OR Ersteller = '{SQL.ReadSingleValue($"SELECT Vertreter FROM {MainWindow.table}.ansprechpartner WHERE Name = '{Helper.InsertSpaceBeforeUpperCase(Environment.UserName)}'")}' " +
                    $"OR Ansprechpartner_Intern = '{Helper.InsertSpaceBeforeUpperCase(Environment.UserName)}')", todayBesuch);
            }

            var firmview = todayFirm.DefaultView;
            firmview.Sort = "Name";
            todayFirm = firmview.ToTable();

            var besuchview = todayBesuch.DefaultView;
            besuchview.Sort = "Name";
            todayBesuch = besuchview.ToTable();

            todayFirm.Merge(todayBesuch);
            dgArrivalsOverview.DataContext = todayFirm;
        }


        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            FillDataGrid(dPOverviewVon.SelectedDate.Value, dPOverviewBis.SelectedDate.Value);
        }

        private void dPOverviewVon_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dPOverviewVon.SelectedDate.HasValue && dPOverviewBis.SelectedDate.HasValue && dPOverviewBis.SelectedDate.Value >= dPOverviewVon.SelectedDate.Value)
            {
                //btnOK.IsEnabled = true;
                FillDataGrid(dPOverviewVon.SelectedDate.Value, dPOverviewBis.SelectedDate.Value);
                lblAnwesend.Text = $"Ihre anwesenden Firmen/Besucher zwischen dem {dPOverviewVon.SelectedDate.Value.ToString("dd.MM.yy")} und {dPOverviewBis.SelectedDate.Value.ToString("dd.MM.yy")}:";
                dPOverviewVon.ClearValue(DatePicker.BackgroundProperty);
                dPOverviewBis.ClearValue(DatePicker.BackgroundProperty);
            }
            else
                dPOverviewVon.Background = new SolidColorBrush(Colors.Red);                
            
        }

        private void dPOverviewBis_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dPOverviewVon.SelectedDate.HasValue && dPOverviewBis.SelectedDate.HasValue && dPOverviewBis.SelectedDate.Value >= dPOverviewVon.SelectedDate.Value)
            {
                //btnOK.IsEnabled = true;
                FillDataGrid(dPOverviewVon.SelectedDate.Value, dPOverviewBis.SelectedDate.Value);
                lblAnwesend.Text = $"Ihre anwesenden Firmen/Besucher zwischen dem {dPOverviewVon.SelectedDate.Value.ToString("dd.MM.yy")} und {dPOverviewBis.SelectedDate.Value.ToString("dd.MM.yy")}:";
                dPOverviewBis.ClearValue(DatePicker.BackgroundProperty);
                dPOverviewVon.ClearValue(DatePicker.BackgroundProperty);
            }
            else
                dPOverviewBis.Background = new SolidColorBrush(Colors.Red);
        }

        private void dPOverviewVon_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
        {
            MessageBox.Show("Kein gültiges Datumsformat in Feld 'von'", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        private void dPOverviewBis_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
        {
            MessageBox.Show("Kein gültiges Datumsformat in Feld 'bis'", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning);
        }


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

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