using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Model.domain;
using SlnMesnac.WPF.MessageTips;
using SlnMesnac.WPF.ViewModel;
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;

namespace SlnMesnac.WPF.Page
{
    /// <summary>
    /// SystemAlarmWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BagsAmountSetWindow : Window
    {

        private readonly ConfigInfoBusiness _configInfoBusiness;


        public BagsAmountSetWindow()
        {
            InitializeComponent();
            _configInfoBusiness = App.ServiceProvider.GetService<ConfigInfoBusiness>();
            Init();
        }

        private void Init()
        {
            List<BaseConfigInfo> configInfos = _configInfoBusiness.GetConfigInfos();
            BagsAmountTxt.Text = configInfos.Where(x => x.ConfigKey == "包装袋余量").FirstOrDefault().ConfigValue;
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null)
            {
                // 从按钮的 Tag 属性中获取参数
                string tag = button.Tag.ToString();
                int amount;

                // 尝试将 tag 转换为整数
                if (int.TryParse(tag, out amount))
                {
                    UpdateBagsAmount(amount);
                }
               
            }
        }

        private void UpdateBagsAmount(int amount)
        {
            int total = int.Parse(BagsAmountTxt.Text);
            total += amount;
            if(total < 0)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Msg.MsgShow("数量不能小于0", 1, 5);

                });

              
                return;
            }
            BagsAmountTxt.Text = total.ToString();
            BaseConfigInfo configInfos = _configInfoBusiness.GetConfigInfos().FirstOrDefault(x => x.ConfigKey == "包装袋余量");
            configInfos.ConfigValue = total.ToString();
            _configInfoBusiness.UpdateConfigInfo(configInfos);
        }


    }
}
