using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
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 Khd.Core.Wpf.WindowPage
{
    /// <summary>
    /// StockWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StockWindow : Window
    {
        private IHost host = null;
        private long LocationId = 0;
        public StockWindow(IHost _host, long locationId)
        {
            host = _host;
            InitializeComponent();
            LocationId = locationId;
            Init(locationId);
        }

        public async Task Init(long locationId)
        {
            using var scope = host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            var location = await dbContext.WmsBaseLocation.Where(x => x.locationId == locationId).FirstOrDefaultAsync();
            positionRowTxt.Text = location.locRow.ToString();
            positionColumnTxt.Text = location.locColumn.ToString();
            positionLayerTxt.Text = location.layerNum.ToString();
            locationTxt.Text = location.locationCode;
            palletTxt.Text = location.containerCode;
            
            if (location.warehouseId == 512 || location.warehouseId == 311 || location.warehouseId == 511)
            {


                var stockList = await dbContext.WmsRawStock.Where(x => x.palletInfoCode == location.containerCode && x.palletInfoCode != null).ToListAsync();
                if (stockList != null && stockList.Count > 0)
                {
                  
                    txtStatus.Text = getStatus(location.locationStatus) + ",有库存";
                    // 获取所有库存物料 ID
                    var materialIds = stockList.Select(stock => stock.materialId).Distinct().ToList();
                    var materialInfos = await dbContext.MesBaseMaterialInfo
              .Where(material => materialIds.Contains(material.MaterialId))
              .ToListAsync();

                    // 将库存物料信息和物料详细信息合并
                    var stockDetails = from stock in stockList
                                       from material in materialInfos
                                       where stock.materialId == material.MaterialId
                                       select new
                                       {
                                           materialCode = material.MaterialCode,
                                           materialName = material.MaterialName,
                                           materialSpec = material.MaterialSpec,
                                           totalAmount = stock.totalAmount,
                                           frozenAmount = stock.frozenAmount

                                       };


                    StockDataGrid.ItemsSource = stockDetails;
                }
                else
                {
                    txtStatus.Text = getStatus(location.locationStatus) + ",无库存";
                }
              
            }
            else
            {
                var stockList = await dbContext.WmsProductStock.Where(x => x.palletInfoCode == location.containerCode && x.palletInfoCode != null).ToListAsync();
                if (stockList != null && stockList.Count > 0)
                {
                    txtStatus.Text = getStatus(location.locationStatus) + ",有库存";
                    // 获取所有库存物料 ID
                    var materialIds = stockList.Select(stock => stock.productId).Distinct().ToList();
                    var materialInfos = await dbContext.MesBaseMaterialInfo
              .Where(material => materialIds.Contains(material.MaterialId))
              .ToListAsync();

                    // 将库存物料信息和物料详细信息合并
                    var stockDetails = from stock in stockList
                                       from material in materialInfos
                                       where stock.productId == material.MaterialId
                                       select new
                                       {
                                           materialCode = material.MaterialCode,
                                           materialName = material.MaterialName,
                                           materialSpec = material.MaterialSpec,
                                           totalAmount = stock.totalAmount,
                                           frozenAmount = stock.frozenAmount

                                       };


                    StockDataGrid.ItemsSource = stockDetails;
                }
                else
                {
                    txtStatus.Text = getStatus(location.locationStatus) + ",无库存";
                }
              

            }
        }
        
        public string getStatus(string status)
        {
            switch (status)
            {
                case "1": return "正常";
                case "2": return "自动锁定";
                case "3": return "人工锁定";
                case "4": return "移库锁定";
                case "5": return "合库锁定";
                case "6": return "出库锁定";
                case "8": return "因外侧库位异常导致内侧库位无法使用";
                case "9": return "异常库位";
                default : return "未知";
            }
        }

        private void LockLocation_Click(object sender, RoutedEventArgs e)
        {
            using var scope = host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            var record =  dbContext.WmsBaseLocation.Where(x => x.locationId == LocationId).First();
            record.locationStatus = "3";
            dbContext.WmsBaseLocation.Update(record);
            dbContext.SaveChanges();
            Init(LocationId);
        }

        private void UnLockLocation_Click(object sender, RoutedEventArgs e)
        {
            using var scope = host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            var record = dbContext.WmsBaseLocation.Where(x => x.locationId == LocationId).First();
            record.locationStatus = "1";
            dbContext.WmsBaseLocation.Update(record);
            dbContext.SaveChanges();
            Init(LocationId);
        }
    }
}
