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;

namespace Pos.Setting.Product
{
    /// <summary>
    /// Interaction logic for EditProductWindow.xaml
    /// </summary>
    public partial class EditProductWindow : Window
    {
        private int _productId;
        private int _selectedProductGroupId;
        private Database.Models.ProductEntity _product;

        public EditProductWindow(int productId, int selectedProductGroupId)
        {
            InitializeComponent();
            _productId = productId;
            _selectedProductGroupId = selectedProductGroupId;
            Init();
        }

        private void Init()
        {
            ProductGroupRepository categoryRepository = new ProductGroupRepository();
            List<Database.Models.ProductGroupEntity> productGroups = categoryRepository.GetAll();
            foreach (Database.Models.ProductGroupEntity productGroup in productGroups)
            {

                ComboBoxItem comboBoxItem = new ComboBoxItem();
                comboBoxItem.Tag = productGroup.Id;
                comboBoxItem.Content = productGroup.Name;
                cmbProductGroup.Items.Add(comboBoxItem);
            }
            cmbProductGroup.SelectedIndex = _selectedProductGroupId;

            ProductRepository productRepository = new ProductRepository();
            _product = productRepository.GetById(_productId);
            txtProduct.Text = _product.Name;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem item = (ComboBoxItem)cmbProductGroup.SelectedItem;
            ProductRepository productRepository = new ProductRepository();
            productRepository.Update(_product.Id,(int)item.Tag,txtProduct.Text);
            this.Close();
        }
    }
}
