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;
using Database.Repository;
using Pos.Setting.Category;

namespace Pos.Category
{
    /// <summary>
    /// Interaction logic for ProductGroupWindow.xaml
    /// </summary>
    public partial class ProductGroupWindow : Window
    {
        private int _id = -1;
        private string _name = string.Empty;

        public ProductGroupWindow()
        {
            InitializeComponent();
            LoadData();
        }

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

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

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

        private void LoadData()
        {
            ProductGroupRepository categoryRepository = new ProductGroupRepository();
            List<Database.Models.ProductGroupEntity> categories = categoryRepository.GetAll();
            LstCategory.Items.Clear();
            foreach (Database.Models.ProductGroupEntity category in categories)
            {
                ListBoxItem item = new ListBoxItem();
                item.Tag = category.Id;
                item.Content = category.Name;
                item.Selected += ItemOnSelected;
                LstCategory.Items.Add(item);
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            EditProductGroupWindow editProductGroupWindow = new EditProductGroupWindow(_name,_id);
            editProductGroupWindow.Closed += WindowClosed;
            editProductGroupWindow.Show();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ArchiveProductGroupWindow archiveProductGroupWindow = new ArchiveProductGroupWindow(_name, _id);
            archiveProductGroupWindow.Closed += WindowClosed;
            archiveProductGroupWindow.Show();
        }


        private void btnOrder_Click(object sender, RoutedEventArgs e)
        {
            ChangeProductGroupOrderWindow changeProductGroupOrderWindow = new ChangeProductGroupOrderWindow();
            changeProductGroupOrderWindow.Closed += WindowClosed;
            changeProductGroupOrderWindow.Show();
        }

    }
}
