using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
using Database.Models;
using Database.Repository;

namespace Pos.Setting
{
    /// <summary>
    /// Interaction logic for EmployeeWindow.xaml
    /// </summary>
    public partial class EmployeeWindow : Window
    {
        private int _selectedEmployee = -1;
        private string _employeeName = string.Empty;
        public EmployeeWindow()
        {
            InitializeComponent();
            LoadData();
        }


        private void Item_Selected(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = (ListBoxItem)sender;
            _selectedEmployee = Convert.ToInt32(item.Tag);
            _employeeName = item.Content.ToString();
            btnDelete.IsEnabled = true;
            btnEdit.IsEnabled = true;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            EditEmployeeWindow editEmployeeWindow = new EditEmployeeWindow(_selectedEmployee, _employeeName);
            editEmployeeWindow.Closed += WindowClosed;
            editEmployeeWindow.Show();
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            AddEmployeeWindow addEmployeeWindow = new AddEmployeeWindow();
            addEmployeeWindow.Closed += WindowClosed;
            addEmployeeWindow.Show();
        }


        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteEmployeeWindow deleteEmployeeWindow = new DeleteEmployeeWindow(_selectedEmployee,_employeeName);
            deleteEmployeeWindow.Closed += WindowClosed;
            deleteEmployeeWindow.Show();
        }

        private void WindowClosed(object? sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            using EmployeeRepository employeeRepository = new EmployeeRepository();
            List<EmployeeEntity> allStaff = employeeRepository.GetAll();
            LstStaff.Items.Clear();
            foreach (EmployeeEntity staff in allStaff)
            {
                ListBoxItem item = new ListBoxItem();
                item.Content = staff.Name;
                item.Tag = staff.Id;
                item.Selected += Item_Selected;
                LstStaff.Items.Add(item);
            }
        }

    }
}
