using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Hotel_Course_Project
{
    /// <summary>
    /// Логика взаимодействия для HostelsPage.xaml
    /// </summary>
    public partial class HostelsPage : Page
    {
        public Staff _hostels;
        public HostelsPage(Staff hostels)
        {
            InitializeComponent();
            _hostels = hostels;

        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            OrderList.ItemsSource = DataBase.db.Order.ToList().OrderByDescending(item => item.DateEnd);
            ClientList.ItemsSource = DataBase.db.Client.Where(item => item.Id_PersStatus == 1).ToList();
        }

        private void AddOrderBtn_Click(object sender, RoutedEventArgs e)
        {
            PChanger.MainFrame.Navigate(new OrderChangeOrAddPage(null, _hostels));
            Page_Loaded(sender, e);
        }
        private void SelectOrderBtn_Click(object sender, RoutedEventArgs e)
        {
            Order order = (sender as Button).DataContext as Order;
            PChanger.MainFrame.Navigate(new OrderChangeOrAddPage(order, _hostels));
        }

        private void OrderSeacrhTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (OrderSeacrhTB.Text == "")
            {
                Page_Loaded(sender, e);
            }
        }

        private void OrderSrchBtn_Click(object sender, RoutedEventArgs e)
        {
            var seacrhed = DataBase.db.Order.Where(item => (item.Client.LName + " " + item.Client.FName + " " + item.Client.MName + " " + item.DateStart + " " + item.Pay.PayType.Name + " " + item.Pay.EndSum + " " + item.DateEnd).Contains(OrderSeacrhTB.Text)).ToList();
            OrderList.ItemsSource = seacrhed;
        }

        private void AddClientBtn_Click(object sender, RoutedEventArgs e)
        {
            PChanger.MainFrame.Navigate(new ClientChangeOrAddPage(null));
            Page_Loaded(sender, e);
        }

        private void ClientSeacrhTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (ClientSeacrhTB.Text == "")
            {
                Page_Loaded(sender, e);
            }
        }

        private void ClientSrchBtn_Click(object sender, RoutedEventArgs e)
        {
            var seacrhed = DataBase.db.Client.Where(item => (item.LName + " " + item.FName + " " + item.MName).Contains(ClientSeacrhTB.Text)).ToList();
            ClientList.ItemsSource = seacrhed.Where(item => item.Id_PersStatus == 1).ToList();
        }

        private void ClientSelectBtn_Click(object sender, RoutedEventArgs e)
        {
            Client client = (sender as Button).DataContext as Client;
            PChanger.MainFrame.Navigate(new ClientChangeOrAddPage(client));
            Page_Loaded(sender, e);
        }
    }
}
