using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Library.Mapper;
using Khd.Core.Plc;
using Khd.Core.Plc.S7;
using Khd.Core.Wpf.Form;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Khd.Core.Wpf.TaskForm
{
    /// <summary>
    /// AddTask.xaml 的交互逻辑
    /// </summary>
    public partial class UpdateAmountWindow : Window
    {

        public delegate void RefreshDelegate(string containerCode);
        public static event RefreshDelegate? RefreshDelegateEvent;


        private string barcodeInfo;
        private string locationCode;
        private string containerCode;
        private readonly IHost _host;
        public UpdateAmountWindow(IHost host,string barcodeInfo,string locationCode,string containerCode)
        {
            InitializeComponent();
            _host = host;
            this.barcodeInfo = barcodeInfo;
            this.locationCode = locationCode;
            this.containerCode = containerCode;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           

        }

        private void UpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            string input = this.StockAmount.Text.Trim(); // 去除前后空白
            if (string.IsNullOrEmpty(input))
            {
                MessageBox.Show("数量不能为空，请输入有效数值。");
                return;
            }

            decimal stockAmountValue;
            if (!decimal.TryParse(input, out stockAmountValue))
            {
                MessageBox.Show("输入的数值格式不正确，请输入有效的十进制数。");
                return;
            }

            //
             if (stockAmountValue <0)
            {
                MessageBox.Show("数值必须大于0");
                return;
            }
            var scope = _host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            var wmsRawStocks = dbContext.WmsRawStock.Where(x => x.locationCode == locationCode && x.instockBatch == barcodeInfo).ToList();
            WmsRawStock wmsRawStock = wmsRawStocks.FirstOrDefault();
            if (wmsRawStock != null)
            {
                wmsRawStock.totalAmount = stockAmountValue;
                if (wmsRawStock.totalAmount == 0)
                {
                    dbContext.Remove(wmsRawStock);
                }
                else
                {
                    dbContext.Update(wmsRawStock);
                }
                // 修改盘库明细
                WmsInventoryCheckDetail wmsInventoryDetail = dbContext.WmsInventoryCheckDetail.Where(x => x.LocationCode == locationCode && x.MaterialBatch == barcodeInfo && x.CheckStatus=="1").OrderByDescending(X=>X.CreateTime).FirstOrDefault();
                if(wmsInventoryDetail != null)
                {
                    wmsInventoryDetail.StockAmount = stockAmountValue;
                    wmsInventoryDetail.RealAmount = stockAmountValue;
                    wmsInventoryDetail.UpdateTime = DateTime.Now;
                    dbContext.Update(wmsInventoryDetail);
                }
                dbContext.SaveChanges();
                this.Close();
                RefreshDelegateEvent?.Invoke(containerCode);
            }


        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
