using BBWY.Client.APIServices;
using BBWY.Client.Extensions;
using BBWY.Client.Models;
using BBWY.Client.Models.FallWare;
using BBWY.Controls;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using WebSocketSharp;

namespace BBWY.Client.Views.FallWare
{
    /// <summary>
    /// SetJDWareBoxWindow2.xaml 的交互逻辑
    /// </summary>
    public partial class SetJDWareBoxWindow2 : BWindow
    {
        public JDWareBoxModel JDWareBoxModel { get; set; }

        private SealBoxService sealBoxService;

        private Action reflashWindow;

        public WareType wareType { get; set; }

        /// <summary>
        /// 预约时间(日期)
        /// </summary>
        public  DateTime PrewDate { get; set; }

        /// <summary>
        /// 预约时间(小时:分钟)
        /// </summary>
        public  string PrewTime { get; set; }

        public bool IsEnabled { get; set; }
        public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }

        private bool isLoading;

        public SetJDWareBoxWindow2(JDWareBoxModel model, SealBoxService sealBoxService, WareType wareType, Action reflashWindow,DateTime? TransportOverTime=null)
        {
            InitializeComponent();

            this.sealBoxService = sealBoxService;
            this.wareType = wareType;
            this.reflashWindow = reflashWindow;
            JDWareBoxModel = model;

            this.DataContext = this;

            switch (wareType)
            {
                case WareType.京仓:
                    btn_yuncang.IsEnabled = false;
                    btn_jushuitang.IsEnabled = false;
                     panel_jingcang.Visibility= Visibility.Visible;
                    panel_yuncang.Visibility= Visibility.Collapsed; 
                    panel_jst.Visibility= Visibility.Collapsed;

                    PrewDate = DateTime.Now;
                    PrewTime = "17:30";
                    if (TransportOverTime != null)
                    {
                        PrewDate = TransportOverTime.Value.Date;
                        PrewTime = TransportOverTime?.ToString("HH:mm");
                    }
                    break;
                case WareType.云仓:
                    btn_jingcang.IsEnabled = false;
                    btn_jushuitang.IsEnabled = false;
                    panel_jingcang.Visibility = Visibility.Collapsed;
                    panel_yuncang.Visibility = Visibility.Visible;
                    panel_jst.Visibility = Visibility.Collapsed;
                    break;
                case WareType.商家仓:
                    break;
                case WareType.聚水潭:
                    btn_yuncang.IsEnabled = false;
                    btn_jingcang.IsEnabled = false;
                    panel_jingcang.Visibility = Visibility.Collapsed;
                    panel_yuncang.Visibility = Visibility.Collapsed;
                    panel_jst.Visibility = Visibility.Visible;
                    break;
                default:
                    break;
            }


        }

        private void btn_save_Click(object sender, System.Windows.RoutedEventArgs e)
        {
          
        
            if (wareType == WareType.京仓)
            {
                if (string.IsNullOrEmpty(JDWareBoxModel.ProductTitle) ||
               string.IsNullOrEmpty(JDWareBoxModel.PrewOrder) ||
               string.IsNullOrEmpty(JDWareBoxModel.PurchaseOrder))
                {
                    MessageBox.Show("信息不完整", "提示");
                    return;
                }

                DateTime? PrewDateTime = null;

                try
                {
                    PrewDateTime = DateTime.Parse((PrewDate.ToString("yyyy-MM-dd ") + PrewTime));


                }
                catch 
                {
                    MessageBox.Show("输入的时间有误", "提示");
                    return;
                }



                IsLoading = true;
                Task.Factory.StartNew(() =>
                {
                    var getDetailResponse = sealBoxService.GetJDSupplierNameAndStoreName(JDWareBoxModel.PurchaseOrder);
                    if (!getDetailResponse.Success)
                    {
                        IsLoading = false;
                        this.Dispatcher.Invoke(() => MessageBox.Show(getDetailResponse.Msg, "提示"));
                        return;
                    }

                    if (getDetailResponse.Data.StoreName!= JDWareBoxModel.WareName)
                    {
                        IsLoading = false;
                        this.Dispatcher.Invoke(() => MessageBox.Show($"采购单对应的仓库名:{getDetailResponse.Data.StoreName}与当前仓库:{JDWareBoxModel.WareName}不符", "提示"));
                        return;
                    }

                    var res = sealBoxService.SetFallWareConfigure(JDWareBoxModel.SealBoxId, JDWareBoxModel.ProductTitle, JDWareBoxModel.PurchaseOrder, JDWareBoxModel.PrewOrder, JDWareBoxModel.WaybillNo, getDetailResponse.Data.SupplierName, getDetailResponse.Data.StoreName, getDetailResponse.Data.City, PrewDateTime);
                    if (!res.Success)
                    {
                        IsLoading = false;
                        this.Dispatcher.Invoke(() => MessageBox.Show(res.Msg, "提示"));
                        return;
                    }
                    IsLoading = false;
                    this.Dispatcher.Invoke(() =>
                    {
                        reflashWindow?.Invoke();
                        this.Close();
                    });
                });
                return;
            }
            if (wareType == WareType.云仓|| wareType == WareType.聚水潭)
            {

                if (JDWareBoxModel.PurchaseOrder.IsNullOrEmpty())
                {
                    MessageBox.Show("采购单号不能为空");
                    return;
                }

                if (wareType == WareType.聚水潭)
                {
                    if (JDWareBoxModel.PurchaseOrder.IsNullOrEmpty())
                    {
                        MessageBox.Show("采购单不能为空!");
                        return;
                    }

                    if (JDWareBoxModel.PurchaseOrder.IsSpecialChar())
                    {
                        MessageBox.Show("采购单不能含特殊符号!");
                        return;
                    }


                }



                if (wareType == WareType.云仓 )
                {
                    if (!JDWareBoxModel.PurchaseOrder.ToUpper().StartsWith("CPL"))
                    {
                        MessageBox.Show("CLPS采购单号必须以CPL开头");
                        return;
                    }
                    var number = JDWareBoxModel.PurchaseOrder.ToUpper().Substring(3);

                    var result = long.TryParse(number, out long res);

                    if (!result||number.Length!=13)
                    {
                        MessageBox.Show("CLPS采购单号必须以CPL开头,且后面为13位数字");
                        return;

                    }

                }


                Task.Factory.StartNew(() =>
                {

                    var res = sealBoxService.SetFallWareConfigure(JDWareBoxModel.SealBoxId, JDWareBoxModel.ProductTitle, JDWareBoxModel.PurchaseOrder.ToUpper(), JDWareBoxModel.PrewOrder, JDWareBoxModel.WaybillNo, null, null);
                    if (!res.Success)
                    {
                        IsLoading = false;
                        this.Dispatcher.Invoke(() => MessageBox.Show(res.Msg, "提示"));
                        return;
                    }
                    IsLoading = false;
                    this.Dispatcher.Invoke(() =>
                    {
                        
                        reflashWindow?.Invoke();
                        this.Close();
                    });
                });
            }
            


        }
    }
}