using BBWY.Client.APIServices;
using BBWY.Client.Models.FallWare;
using BBWY.Client.Models;
using BBWY.Controls;
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
using WebSocketSharp;

namespace BBWY.Client.Views.FallWare
{
    /// <summary>
    /// SetCloudWareBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetCloudWareBoxWindow : BWindow
    {
        public SetCloudWareBoxWindow(JDWareBoxModel model, SealBoxService sealBoxService, WareType wareType, Action reflashWindow, bool isEnabled = true)
        {
            if (SealBoxService == null) this.SealBoxService = sealBoxService;
            WareType = wareType;
            ReflashWindow = reflashWindow;

            JDWareBoxModel = model;
            InitializeComponent();
            this.DataContext = JDWareBoxModel;
            if (!isEnabled)
            {
                this.jd_box.IsEnabled = isEnabled;
                this.btn_save.Visibility = Visibility.Collapsed;
            }
        }
        public JDWareBoxModel JDWareBoxModel { get; set; }

        private SealBoxService SealBoxService { get; set; }

        private Action ReflashWindow { get; set; }

        private WareType WareType { get; set; }
        private void BButton_Click(object sender, RoutedEventArgs e)
        {

            if (JDWareBoxModel.PurchaseOrder.IsNullOrEmpty())
            {
                MessageBox.Show("采购单号不能为空");
                return;
            }
            if (JDWareBoxModel.PrewOrder.IsNullOrEmpty() && WareType == WareType.京仓)
            {
                MessageBox.Show("预约单号不能为空");
                return;
            }
            var res = SealBoxService.SetFallWareConfigure(JDWareBoxModel.SealBoxId, JDWareBoxModel.ProductTitle, JDWareBoxModel.PurchaseOrder, JDWareBoxModel.PrewOrder, JDWareBoxModel.WaybillNo);

            if (res != null && res.Success)
            {
                if (ReflashWindow != null) ReflashWindow();
                this.Close();

                return;
            }
            if (res != null) MessageBox.Show(res.Msg);
        }
    }
}