using HandyControl.Controls;
using HandyControl.Data;
using MiniExcelLibs;
using Newtonsoft.Json;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using TouchSocket.Core;
using System.Windows.Markup;
using System.Security.Policy;
using WCS.Model.ApiModel.MXBackgroundThread;
using 智能仓储WCS管理系统;
using WCS.Model.ApiModel.MatInventoryDetail;
using 智能仓储WCS管理系统.Api;
using System.Runtime.CompilerServices;
using WCS.Model;

namespace 智能仓储WCS管理系统.ViewModel
{
    public class MXOutOrderDetailViewViewModel : BindableBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="billNumber"></param>
        /// <param name="warehouseCode"></param>
        /// <param name="orderProdNumbers"></param>
        /// <param name="orderWorkNumbers"></param>
        /// <param name="isFirstSend">是否是首盘发料</param>
        public MXOutOrderDetailViewViewModel(
            string billNumber, string warehouseCode
            , string orderProdNumbers, string orderWorkNumbers
            , int isFirstSend = 0, string reportSide = null
            )
        {
            BillNumber = billNumber;
            OrderProdNumbers = orderProdNumbers;
            OrderWorkNumbers = orderWorkNumbers;
            WarehouseCode = warehouseCode;

            Task.Run(() =>
            {
                var request = new RecommendedBarCodeRequest()
                {
                    pickBillNumber = billNumber,
                    warehouseCode = warehouseCode,
                    warehouseType = 20
                };
                var requeststr = JsonConvert.SerializeObject(request);
                var url = string.Empty;
                if (isFirstSend == 0)
                {
                    url = LocalFile.Config.RecommendedBarCode;
                }
                else if (isFirstSend == 1)
                {
                    request = new FistPushRecommendedBarCodeRequest()
                    {
                        pickBillNumber = billNumber,
                        warehouseCode = warehouseCode,
                        warehouseType = 20,
                        reportSide = reportSide,
                    };
                    requeststr = JsonConvert.SerializeObject(request);
                    url = LocalFile.Config.FirstSendRecommendedBarCode;
                }
                //后续料
                else
                {
                    url = LocalFile.Config.ElectronicSiloPush;
                }

                var result = ApiHelp.MXGetDataFromHttp<List<PickOrderDetailGetReturnList>>(requeststr, url, "POST", true);
                if (result != null && result.code == 200 && result.data != null && result.data.Count() > 0)
                {
                    //获取推荐的条码
                    var matSns = result.data.Select(t => t.materialBar).ToList();

                    //调用 比对库存数据 获取库存中有的数据
                    #region 调用接口 比对库存数据 获取库存中有的数据
                    try
                    {
                        var body = new CompareMatInventoryDetailRequest()
                        {
                            MatSns = matSns,
                        };
                        var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<MatInventoryDetailModel>>(LocalFile.Config.ApiIpHost + "matInventoryDetail/compareMatInventoryDetail", body, "POST", true);

                        //查询到物料信息
                        if (Result != null && Result.Data != null && Result.Data.Lists != null && Result.Data.Lists.Count != 0)
                        {
                            DataGridItemSource = Result.Data.Lists;
                        }
                        else if (Result != null && Result.Code == 200 && (Result.Data == null || Result.Data.Lists == null || Result.Data.Lists.Count == 0))
                        {
                            MessageBox.Show($"未查询到领料明细！所推荐物料在此货架上不存在，请确认是否在其他货架上！\r\n{string.Join(",\r\n", matSns)}");
                            DataGridItemSource = null;
                        }
                        else
                        {
                            MessageBox.Show($"未查询到领料明细！请重试！");
                            DataGridItemSource = null;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show($"未查询到领料明细！请重试！");
                        DataGridItemSource = null;
                    }
                    #endregion
                }
                else if (result != null && result.code == 200 && (result.data == null || result.data.Count == 0))
                {
                    Growl.Warning("未查询到物料明细！MES推荐物料明细为空！");
                    DataGridItemSource = null;
                }
                else
                {
                    Growl.Warning("未查询到领料明细！请求MES接口失败！");
                    DataGridItemSource = null;
                }
            });
        }


        #region Property
        private int selectedCount;
        public int SelectedCount
        {
            get { return selectedCount; }
            set { SetProperty(ref selectedCount, value); }
        }

        private int selectedPan;
        public int SelectedPan
        {
            get { return selectedPan; }
            set { SetProperty(ref selectedPan, value); }
        }
        //发料单号
        private string billNumber;
        public string BillNumber
        {
            get { return billNumber; }
            set { SetProperty(ref billNumber, value); }
        }

        //订单号
        private string orderProdNumbers;
        public string OrderProdNumbers
        {
            get { return orderProdNumbers; }
            set { SetProperty(ref orderProdNumbers, value); }
        }

        //工单号
        private string orderWorkNumbers;
        public string OrderWorkNumbers
        {
            get { return orderWorkNumbers; }
            set { SetProperty(ref orderWorkNumbers, value); }
        }

        //输入的仓库代码
        private string warehouseCode;
        public string WarehouseCode
        {
            get { return warehouseCode; }
            set
            {
                SetProperty(ref warehouseCode, value);
            }
        }

        private List<MatInventoryDetailModel> dataGridItemSource;
        public List<MatInventoryDetailModel> DataGridItemSource
        {
            get { return dataGridItemSource; }
            set
            {
                SetProperty(ref dataGridItemSource, value);
                RefreshCount();
            }
        }


        public void RefreshCount()
        {
            Task.Run(() =>
            {
                try
                {
                    SelectedCount = dataGridItemSource.Where(t => t.IsSelected == true).Sum(t => t.MatQty);
                    SelectedPan = dataGridItemSource.Where(t => t.IsSelected == true).Count();
                }
                catch { }

            });
        }

        #endregion

        #region Command

        #endregion
    }
}
