using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Wpf.TaskForm.Inventory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace Khd.Core.Wpf.TaskForm
{
    /// <summary>
    /// InventoryTaskForm.xaml 的交互逻辑
    /// </summary>
    public partial class InventoryTaskForm : Window
    {
        private readonly IHost _host;

        public InventoryTaskForm(IHost host)
        {
            InitializeComponent();
            _host = host;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                using var scope = _host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var wcsTasks = dbContext.WcsTask.Where(t => t.taskType == 100 && t.taskStatus == 6).ToList();
                if (wcsTasks.Count > 0)
                {
                    this.ContainerCodeBox.ItemsSource = wcsTasks.Distinct();
                }
                else
                {
                    HandyControl.Controls.MessageBox.Show("未找到盘库已完成的小车任务！");
                    this.Close();
                }
            }
            catch
            {

            }
        }

        private void GetData()
        {
            if (this.ContainerCodeBox.SelectedItem is WcsTask task)
            {
                using var scope = _host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var wcsTask = dbContext.WcsTask
                    .Where(t => t.taskType == 100)
                    .Where(t => t.containerNo == task.containerNo).FirstOrDefault();
                if (wcsTask != null)
                {
                    var wmsInventoryCheckDetails = dbContext.WmsInventoryCheckDetail
                        .Where(t => t.InventoryCheckId == wcsTask.orderId)
                        .Where(t => t.LocationCode == wcsTask.currPointNo)
                        .Where(t => t.CheckStatus == "1")
                        .ToList();
                    List<long?> list = wmsInventoryCheckDetails.Select(t => t.MaterialId).Distinct().ToList();
                    List<MesBaseMaterialInfo> mesBaseMaterialInfos = dbContext.MesBaseMaterialInfo.Where(t => list.Contains(t.MaterialId)).ToList();
                    this.InventoryDataGrid.ItemsSource = wmsInventoryCheckDetails.GroupBy(t => t.MaterialId).Select(t =>
                        new
                        {
                            ContainerNo = wcsTask.containerNo,
                            MaterialName = mesBaseMaterialInfos.FirstOrDefault(m => m.MaterialId == t.Key)?.MaterialSpec,
                            StockAmount = t.Sum(n => n.StockAmount),
                            LocationCode = wcsTask.currPointNo,
                            MaterialId = t.Key,
                            RealAmount = t.Sum(n => n.RealAmount),
                        });
                    this.InventoryDataGrid.Items.Refresh();
                    if (wmsInventoryCheckDetails.Count == 0)
                    {
                        HandyControl.Controls.MessageBox.Show("该料箱为空料箱");
                    }
                }
            }
            else
            {
                HandyControl.Controls.MessageBox.Show("请先选择料箱号！");
            }
        }

        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void EditBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.InventoryDataGrid.SelectedItem != null)
            {
                dynamic item = this.InventoryDataGrid.SelectedItem;
                long materialId = item.MaterialId;
                string containerNo = item.ContainerNo;
                string materialName = item.MaterialName;
                string locationCode = item.LocationCode;
                InventoryTaskEditForm editForm = new InventoryTaskEditForm(_host, materialId, containerNo, materialName, locationCode, this);
                this.Hide();
                editForm.ShowDialog();
                GetData();
            }
            else
            {
                if (this.ContainerCodeBox.SelectedItem is WcsTask task)
                {
                    InventoryTaskEditForm editForm = new InventoryTaskEditForm(_host, 0, task.containerNo, "", task.currPointNo, this);
                    this.Hide();
                    editForm.ShowDialog();
                    GetData();
                }
                else
                {
                    HandyControl.Controls.MessageBox.Show("请先选择料箱号！");
                }
            }
        }
    }
}
