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.Media.Media3D;
using System.Windows.Shapes;

namespace Khd.Core.Wpf.WindowPage
{
    /// <summary>
    /// StockWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InventoryMaterialWindow : Window
    {
        private IHost host = null;
        public InventoryMaterialWindow(IHost _host)
        {
            host = _host;
            InitializeComponent();
            Init();
        }

        public async Task Init()
        {
            using var scope = host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            //var location = await dbContext.WmsBaseLocation.Where(x => x.locationId == locationId).FirstOrDefaultAsync();
    
            var stockList = await dbContext.WmsRawStock.Where(x => x.warehouseId ==512).ToListAsync();


            if(stockList != null && stockList.Count > 0)
            {
                // 获取所有库存物料 ID
                var materialIds = stockList.Select(stock => stock.materialId).Distinct().ToList();
                var materialInfos = await dbContext.MesBaseMaterialInfo
          .Where(material => materialIds.Contains(material.MaterialId))
          .ToListAsync();
                // 将库存物料信息和物料详细信息合并
                var stockDetails = materialInfos.Select(material => new StockItem
                {
                    materialId   = material.MaterialId.ToString(),
                    materialCode = material.MaterialCode,
                    materialName = material.MaterialName,
                    materialSpec = material.MaterialSpec,
                }).ToList();


                StockDataGrid.ItemsSource = stockDetails;
            }
          
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            string searchStr = SearchTxt.Text;
            if (string.IsNullOrEmpty(searchStr))
            {
                Init();
            }
            else
            {
                var target = StockDataGrid.ItemsSource.Cast<dynamic>().Where(x => x.materialCode.Contains(searchStr) || x.materialName.Contains(searchStr) || x.materialSpec.Contains(searchStr)).ToList();
                StockDataGrid.ItemsSource = target;
            }
           
        }



        private void StockDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 获取选中的项（假设它是某种类型，如StockItem）
            var selectedItem = StockDataGrid.SelectedItem as StockItem; // StockItem是你的数据项类型
            if (selectedItem != null)
            {
                // 更新TextBox的内容为选中项的属性值
                SelectMaterialId.Text = selectedItem.materialId.ToString(); // 假设MaterialId是一个属性
                SelectMaterialName.Text = selectedItem.materialName; // 假设MaterialName是一个属性
            }
            else
            {
                // 如果没有选中项，则清空TextBox的内容
                SelectMaterialId.Text = string.Empty;
                SelectMaterialName.Text = string.Empty;
            }
        }



        public class StockItem
        {
            public string materialId { get; set; }
            public string  materialCode { get; set; }
            public string  materialName { get; set; }
            public string  materialSpec { get; set; }
        }
    }
}
