using HandyControl.Controls;
using HandyControl.Data;
using MiniExcelLibs;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TouchSocket.Core;
using WCS.Model;
using WCS.Model.ApiModel.OutStore;
using WCS.Model.ApiModel.Stocktaking;
using 智能仓储WCS管理系统.Api;
using 智能仓储WCS管理系统.ViewModel;

namespace 智能仓储WCS管理系统.ViewModel
{
    public class OutInventoryViewModel : BindableBase
    {
        public OutInventoryViewModel()
        {
            RefreshOutOrderList();
        }
        #region Property
        private OutOrderModel selectedOutOrder;
        public OutOrderModel SelectedOutOrder
        {
            get { return selectedOutOrder; }
            set
            {
                SetProperty(ref selectedOutOrder, value);
                if (selectedOutOrder != null)
                {
                    SelectedOutOrderNumber = selectedOutOrder.OrderNumber;
                    RefreshDataGridItemSource();
                }
                else
                {
                    DataGridItemSource?.Clear();
                }
            }
        }

        private string selectedOutOrderNumber;
        public string SelectedOutOrderNumber
        {
            get => selectedOutOrderNumber;
            set
            {
                SetProperty(ref selectedOutOrderNumber, value);
            }
        }


        private ObservableCollection<OutOrderModel> outOrderList;
        public ObservableCollection<OutOrderModel> OutOrderList
        {
            get => outOrderList;
            set
            {
                SetProperty(ref outOrderList, value);
            }
        }

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

        //单据总盘数
        private int totalPan;
        public int TotalPan
        {
            get => totalPan; set
            {
                SetProperty(ref totalPan, value);
            }
        }

        private int sendedPan;
        public int SendedPan
        {
            get => sendedPan; set
            {
                SetProperty(ref sendedPan, value);
            }
        }

        private string orderStatus;
        public string OrderStatus
        {
            get { return orderStatus; }
            set
            {
                SetProperty(ref orderStatus, value);
            }
        }

        public void RefreshCount()
        {
            Task.Run(() =>
            {
                SendedPan = dataGridItemSource.Where(t => t.IsSended).Count();
                TotalPan = dataGridItemSource.Count();
            });
        }
        #endregion
        #region Command
        public ICommand BtnOutOrderCommand { get => new DelegateCommand(BtnOutOrder); }
        public void BtnOutOrder()
        {
            var window = new OutInventoryAddDucumentView();
            window.Owner = Application.Current.MainWindow;
            window.ShowDialog();
        }


        public ICommand BtnStartCommand { get => new DelegateCommand(BtnStart); }
        public void BtnStart()
        {
            try
            {
                //判断是否选择单据
                if (SelectedOutOrder == null)
                {
                    Growl.Warning("未选择单据！");
                    return;
                }

                #region 调用接口开始出库
                var body = new GetOutOrderDetailRequest()
                {
                    OrderId = selectedOutOrder.Id,
                    OrderNumber = selectedOutOrder.OrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "outStore/goInOutstore", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Warning("已成功开始出库！");
                    RefreshDataGridItemSource();
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }

        public ICommand BtnPauseCommand { get => new DelegateCommand(BtnPause); }
        public void BtnPause()
        {
            try
            {
                //判断是否选择单据
                if (SelectedOutOrder == null)
                {
                    Growl.Warning("未选择单据！");
                    return;
                }
                #region 调用接口结束出库
                var body = new GetOutOrderDetailRequest()
                {
                    OrderId = selectedOutOrder.Id,
                    OrderNumber = selectedOutOrder.OrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "outStore/goOutOutstore", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Warning("已成功结束出库！");
                    RefreshDataGridItemSource();
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }

        public void RefreshOutOrderList(string OrderNumber = "")
        {
            #region 调用接口获取发料单
            try
            {
                var body = new GetOutOrderListByStatusRequest()
                {
                    OrderExeStatus = new List<OutOrderExeStatus>() { OutOrderExeStatus.开始发料, OutOrderExeStatus.发料完成 }
                };
                var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<OutOrderModel>>(LocalFile.Config.ApiIpHost + "outstore/getOutOrderListByStatus", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    OutOrderList = new ObservableCollection<OutOrderModel>(Result.Data.Lists);
                    if (!string.IsNullOrEmpty(OrderNumber))
                    {
                        SelectedOutOrder = OutOrderList.Where(t => t.OrderNumber == OrderNumber).FirstOrDefault();
                    }
                }
                else if (Result != null && !string.IsNullOrEmpty(Result.Message))
                {
                    Growl.Warning(Result.Message);
                }
            }
            catch (Exception ex)
            {
                Growl.Warning(ex.Message);
            }
            #endregion
        }

        public void RefreshDataGridItemSource()
        {
            if (SelectedOutOrder == null)
            {
                //选择的单据为空无法进行查询
                return;
            }
            #region 调用接口获取出库单物料明细
            Task.Run(() =>
            {
                var body = new GetOutOrderDetailRequest()
                {
                    OrderId = selectedOutOrder.Id,
                    OrderNumber = selectedOutOrder.OrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,

                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<List<OutOrderMatDetailModel>>>(LocalFile.Config.ApiIpHost + "outStore/getOutOrderMatDetail", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    if (Result.Data.Count > 0)
                    {
                        var list = Result.Data.OrderBy(t => t.IsSended).ToList();
                        DataGridItemSource = new ObservableCollection<OutOrderMatDetailModel>(list);
                        OrderStatus = Result.Message;
                    }
                    else
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            DataGridItemSource?.Clear();
                        });
                        Growl.Warning("该单据未查询到发料明细！");
                    }
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
            });
            #endregion
        }
        #endregion
    }
}
