using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

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

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            #region условия для выводва работников
            if (SortStaffCB.SelectedItem == activeS)
            {
                StaffList.ItemsSource = DataBase.db.Staff.Where(item => item.Id_PersStatus == 1).ToList();
            }
            else
            {
                StaffList.ItemsSource = DataBase.db.Staff.Where(item => item.Id_PersStatus == 2).ToList();
            }
            #endregion
            #region Условия для выводва клиентов
            if (SortClientCB.SelectedItem == activeC)
            {
                ClientList.ItemsSource = DataBase.db.Client.Where(item => item.Id_PersStatus == 1).ToList();
            }
            else
            {
                ClientList.ItemsSource = DataBase.db.Client.Where(item => item.Id_PersStatus == 2).ToList();
            }
            #endregion

            RoomList.ItemsSource = DataBase.db.Room.ToList();
            PayList.ItemsSource = DataBase.db.Order.ToList();
            RoleList.ItemsSource = DataBase.db.StaffRole.ToList();
            StatusRoomList.ItemsSource = DataBase.db.RoomStatus.ToList();
            PayTypeList.ItemsSource = DataBase.db.PayType.ToList();
        }

        #region Обработчики кнопок добавления
        private void AddStaffBtn_Click(object sender, RoutedEventArgs e)
        {
            PChanger.MainFrame.Navigate(new StaffChangeOrAddPage(null));
            Page_Loaded(sender, e);
        }
        private void AddClientBtn_Click(object sender, RoutedEventArgs e)
        {
            PChanger.MainFrame.Navigate(new ClientChangeOrAddPage(null));
            Page_Loaded(sender, e);
        }
        private void AddRoomBtn_Click(object sender, RoutedEventArgs e)
        {
            RoomAddOrChangePage roomAddOrChange = new RoomAddOrChangePage(null);
            PChanger.MainFrame.Navigate(new RoomAddOrChangePage(null));
            Page_Loaded(sender, e);
        }
        private void RoleAddBtn_Click(object sender, RoutedEventArgs e)
        {
            RoleAddWindow roleAdd = new RoleAddWindow();
            roleAdd.ShowDialog();
            Page_Loaded(sender, e);
        }
        private void StatusRoomAddBtn_Click(object sender, RoutedEventArgs e)
        {
            StatusRoomAddWindow statusRoomAdd = new StatusRoomAddWindow();
            statusRoomAdd.ShowDialog();
            Page_Loaded(sender, e);
        }
        private void PayTypeAddBtn_Click(object sender, RoutedEventArgs e)
        {
            PayTypeAddWindow payTypeAdd = new PayTypeAddWindow();
            payTypeAdd.ShowDialog();
            Page_Loaded(sender, e);
        }

        #endregion

        #region Обработчики  кнопок выбора
        private void StaffSelectBtn_Click(object sender, RoutedEventArgs e)
        {
            Staff staff = (sender as Button).DataContext as Staff;
            PChanger.MainFrame.Navigate(new StaffChangeOrAddPage(staff));
            Page_Loaded(sender, e);
        }
        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);
        }
        private void SelectRoomBtn_Click(object sender, RoutedEventArgs e)
        {
            Room room = (sender as Button).DataContext as Room;
            PChanger.MainFrame.Navigate(new RoomAddOrChangePage(room));
            Page_Loaded(sender, e);
        }
        #endregion

        #region Обработчики выбора в сортировке

        private void SortStaffCB_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (StaffList != null)
            {
                Page_Loaded(sender, e);
            }
        }

        private void SortClientCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ClientList != null)
            {
                Page_Loaded(sender, e);
            }
        }

        #endregion

        #region Обработчики кнопок поиска
        private void StaffSrchBtn_Click(object sender, RoutedEventArgs e)
        {
            string searchText = StaffSeacrhTB.Text;
            //var seacrhed = DataBase.db.Staff.Where(item => (item.LName + " " + item.FName + " " + item.MName + " " + item.Login + " " + item.Password).Contains(StaffSeacrhTB.Text)).ToList();
            var searched = SearchStaff(DataBase.db.Staff.ToList(), searchText);
            if (SortStaffCB.SelectedItem == activeS)
            {
                StaffList.ItemsSource = searched.Where(item => item.Id_PersStatus == 1).ToList();
            }
            else
            {
                StaffList.ItemsSource = searched.Where(item => item.Id_PersStatus == 2).ToList();
            }
        }

        public List<Staff> SearchStaff(List<Staff> listStaff, string searchText)
        {
            List<Staff> staffSearchedList = new List<Staff>();
            staffSearchedList = listStaff.Where(item => (item.LName + " " + item.FName + " " + item.MName + " " + item.Login + " " + item.Password).Contains(searchText)).ToList();
            return staffSearchedList;
        }

        private void ClientSrchBtn_Click(object sender, RoutedEventArgs e)
        {
            string searchText = ClientSeacrhTB.Text;
            //var seacrhed = DataBase.db.Client.Where(item => (item.LName + " " + item.FName + " " + item.MName).Contains(ClientSeacrhTB.Text)).ToList();
            var searched = SearchClient(DataBase.db.Client.ToList(), searchText);
            if (SortClientCB.SelectedItem == activeC)
            {
                ClientList.ItemsSource = searched.Where(item => item.Id_PersStatus == 1).ToList();
            }
            else
            {
                ClientList.ItemsSource = searched.Where(item => item.Id_PersStatus == 2).ToList();
            }
        }
        public List<Client> SearchClient(List<Client> listClient, string searchText)
        {
            List<Client> clientSearchedList = new List<Client>();
            clientSearchedList = listClient.Where(item => (item.LName + " " + item.FName + " " + item.MName).Contains(searchText)).ToList();
            return clientSearchedList;
        }

        private void PaySrchBtn_Click(object sender, RoutedEventArgs e)
        {
            var searched = DataBase.db.Order.Where(item => (item.Pay.PayDate + " " + item.Pay.EndSum + " " + item.Staff.LName + " " + item.Staff.FName + " " + item.Staff.MName + " " + item.Client.LName + " " + item.Client.FName + " " + item.Room.NumRoom).Contains(PaySeacrhTB.Text)).ToList();
            PayList.ItemsSource = searched;
        }
        #endregion

        #region Обработичики для строк поиска
        private void StaffSeacrhTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (StaffSeacrhTB.Text == "")
            {
                Page_Loaded(sender, e);
            }
        }

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

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

        #endregion

        #region обработчики кнопок удаления
        private void RoleDeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            StaffRole role = (sender as Button).DataContext as StaffRole;
            var ishave = DataBase.db.Staff.Where(item => item.Id_StaffRole == role.Id).ToList();
            if (ishave.Count > 0)
            {
                MessageBox.Show("Удаление невозможно, т.к. \nимеются работники занимающие \nэту должность");
            }
            else
            {
                DataBase.db.StaffRole.Remove(role);
                DataBase.db.SaveChanges();
                Page_Loaded(sender, e);
            }
        }
        private void StatusRoleDeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            RoomStatus status = (sender as Button).DataContext as RoomStatus;
            var ishave = DataBase.db.Order.Where(item => item.Id_Room == status.Id).ToList();
            if (ishave.Count > 0)
            {
                MessageBox.Show("Удаление невозможно, т.к. эта комната учавствует в заказах");
            }
            else
            {
                DataBase.db.RoomStatus.Remove(status);
                DataBase.db.SaveChanges();
                Page_Loaded(sender, e);
            }
        }
        private void PayTypeDeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            PayType type = (sender as Button).DataContext as PayType;
            var ishave = DataBase.db.Pay.Where(item => item.Id_PayType == type.Id).ToList();
            if (ishave.Count > 0)
            {
                MessageBox.Show("Удаление невозможно, т.к. эта этот тип платежей учавствует в платежах");
            }
            else
            {
                DataBase.db.PayType.Remove(type);
                DataBase.db.SaveChanges();
                Page_Loaded(sender, e);
            }
        }
        #endregion
    }
}
