using BBWY.Client.APIServices;
using BBWY.Client.Models.APIModel;
using BBWY.Client.Models.APIModel.Response.PackTask;
using BBWY.Client.ViewModels;
using BBWY.Client.ViewModels.PackTask;
using BBWY.Client.Views.PackTask;
using BBWY.Client.Views.QualityTask;
using GalaSoft.MvvmLight.Command;
using NPOI.Util;
using System;
using System.Collections.Generic;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WebSocketSharp;

namespace BBWY.Client.Models
{
    /// <summary>
    /// 打包任务  数据绑定
    /// </summary>
    public class PackTaskModel : BaseVM
    {

        private bool isShowFees;
        public bool IsShowFees { get => isShowFees; set { Set(ref isShowFees, value); } }
        private long id;
        public long Id { get => id; set { Set(ref id, value); } }
        public ICommand PrintBarcodeCommand { get; set; }
        public ICommand PrintCerCommand { get; set; }
        public ICommand LookCerCommand { get; set; }
        public ICommand LookBarCommand { get; set; }
        public ICommand SetServiceCommand { get; set; }
        /// <summary>
        /// 修改任务
        /// </summary>
        public ICommand UpdateTaskCommand { get; set; }
        public ICommand QualityTaskCommand { get; set; }
        public Action ReflashTask { get; set; }
        private void UpdateTask()
        {

            try
            {
                if (!this.SkuPurchaseSchemeId.IsNullOrEmpty())//B端有采购方案
                {
                    UpdatePurchaseTaskWindow updatePurchaseWindow = new UpdatePurchaseTaskWindow(this, ReflashTask);
                    updatePurchaseWindow.ShowDialog();
                    return;
                }

                if (!OrderId.IsNullOrEmpty() && SkuPurchaseSchemeId.IsNullOrEmpty())//B端无采购方案 ,手动添加合格证
                {


                    PublishTaskWindow publishB = new PublishTaskWindow(ReflashTask, this, SaveType.B端);
                    publishB.ShowDialog();
                    return;
                }

                PublishTaskWindow publish = new PublishTaskWindow(ReflashTask, this, SaveType.C端);
                publish.ShowDialog();
            }
            catch (Exception)
            {

                
            }
            
        }



        public PackTaskModel()
        {
            PrintBarcodeCommand = new RelayCommand(PrintBarcode);
            PrintCerCommand = new RelayCommand(PrintCer);
            LookBarCommand = new RelayCommand(LookBar);
            LookCerCommand = new RelayCommand(LookCer);
            SetServiceCommand = new RelayCommand(SetService);
            UpdateTaskCommand = new RelayCommand(UpdateTask);
            QualityTaskCommand = new RelayCommand(QualityTask);
        }

        private void QualityTask()
        {
            //加载数据
            QualityWindow service = new QualityWindow(this, ReflashTask);
            service.Show();
        }

        private void SetService()
        {

            if (this.PackProcessType==null&&this.FeesMoney>0)
            {
                ServiceWindow window = new ServiceWindow(this, ReflashTask);
                window.ShowDialog();
                return;//旧版本

            }
            PackDetailWindow service = new PackDetailWindow(this, ReflashTask);
            service.ShowDialog();
        }

        /// <summary>
        /// 查看合格证
        /// </summary>
        private void LookCer()
        {
            if (CertificateModel == null)
            {
                new TipsWindow("该任务无设置合格证信息,无法查看!").ShowDialog();
                return;
            }
            LookCerWindow lookCerWindow = new LookCerWindow(CertificateModel);
            lookCerWindow.ShowDialog();
        }



        /// <summary>
        /// 查看条形码
        /// </summary>
        private void LookBar()
        {
            if (BarCodeModel == null)
            {
                new TipsWindow("该任务无设置条形码信息,无法查看!").ShowDialog();
                return;
            }
            LookBarCodeWindow look = new LookBarCodeWindow();
            look.SetData(BarCodeModel.Copy());
            look.Show();
        }

        private void PrintCer()
        {
            if (CertificateModel == null)
            {
                new TipsWindow("该任务无设置合格证信息,无法打印!").Show();
                return;
            }
            PrintWindow printWindow = new PrintWindow();

            printWindow.CertificateModel = certificateModel;
            printWindow.LoadData();
            //printWindow.Datas = LoadCerLabelModels();
            printWindow.ShowDialog();
        }

        /// <summary>
        /// 打印条形码
        /// </summary>
        private void PrintBarcode()
        {
            if (BarCodeModel == null)
            {
                new TipsWindow("该任务无设置条形码信息,无法打印!").Show();
                return;
            }

            PrintWindow printWindow = new PrintWindow();
            printWindow.BarCodeModel = new BarCodeModel
            {
                Brand = BarCodeModel.Brand,
                BrandName = BarCodeModel.BrandName,
                LabelModel = BarCodeModel.LabelModel,
                ProductNo = BarCodeModel.ProductNo,
                SkuId = BarCodeModel.SkuId,
                SkuName = BarCodeModel.SkuName,
                ShopName = BarCodeModel.ShopName
            };
            printWindow.LoadData();
            //printWindow.Datas = LoadBarLabelModels();
            printWindow.ShowDialog();
        }



        private DateTime endTime;
        /// <summary>
        /// 提交时间
        /// </summary>
        public DateTime EndTime { get => endTime; set { Set(ref endTime, value); } }


        private long taskId;
        /// <summary>
        /// 任务id
        /// </summary>
        public long TaskId { get => taskId; set { Set(ref taskId, value); } }


        private string departmentName;
        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { get => departmentName; set { Set(ref departmentName, value); } }


        private string acceptName;
        /// <summary>
        /// 对接人
        /// </summary>
        public string AcceptName { get => acceptName; set { Set(ref acceptName, value); } }


        private int skuCount;
        /// <summary>
        /// sku数量
        /// </summary>
        public int SkuCount { get => skuCount; set { Set(ref skuCount, value); } }


        private Availability availability;
        /// <summary>
        /// 到货情况(待收货=0,部分收货=1,已到货=2)
        /// </summary>
        public Availability Availability { get => availability; set { Set(ref availability, value); } }






        private TaskState taskState;
        /// <summary>
        /// 打包仓库任务状态
        /// </summary>
        public TaskState TaskState { get => taskState; set { Set(ref taskState, value); } }


        private PackType packType;
        /// <summary>
        /// 打包类型(单件=0,多件=1)
        /// </summary>
        public PackType PackType { get => packType; set { Set(ref packType, value); } }


        private int goodsNumber;
        /// <summary>
        /// 配件数
        /// </summary>
        public int GoodsNumber { get => goodsNumber; set { Set(ref goodsNumber, value); } }


        private string skuTitle;
        /// <summary>
        /// sku配件商品名称
        /// </summary>
        public string SkuTitle { get => skuTitle; set { Set(ref skuTitle, value); } }


        private BasicPack basicPack;
        /// <summary>
        /// 基础包装(快递袋=0,纸箱=1,麻袋=2)
        /// </summary>
        public BasicPack BasicPack { get => basicPack; set { Set(ref basicPack, value); } }



        private string increment1;
        /// <summary>
        /// 增量1()
        /// </summary>
        public string Increment1 { get => increment1; set { Set(ref increment1, value); } }

        private string positionType;
        /// <summary>
        /// 落仓(商家仓=0,云仓=1,京仓=2,聚水潭=3)
        /// </summary>
        public string PositionType { get => positionType; set { Set(ref positionType, value); } }




        #region 条形码数据
        /// <summary>
        /// 条形码id
        /// </summary>
        public long BarCodeId { get; set; }

        /// <summary>
        /// 标签模板
        /// </summary>
        public BarcodeLabelModel LabelModel { get; set; }

        private string brand;
        /// <summary>
        /// 品牌
        /// </summary>
        public string Brand { get => brand; set { Set(ref brand, value); } }

        private string brandName;
        /// <summary>
        /// 品名
        /// </summary>
        public string BrandName { get => brandName; set { Set(ref brandName, value); } }


        private string producNo;
        /// <summary>
        /// 货号
        /// </summary>
        public string ProductNo { get => producNo; set { Set(ref producNo, value); } }

        private string skuName;
        /// <summary>
        /// sku名称
        /// </summary>
        public string SkuName { get => skuName; set { Set(ref skuName, value); } }


        private string skuId;
        /// <summary>
        /// skuId(条形码号=POP+SkuId)
        /// </summary>
        public string SkuId { get => skuId; set { Set(ref skuId, value); } }


        //private string modelNo;
        ///// <summary>
        ///// 型号
        ///// </summary>
        //public string ModelNo { get => modelNo; set { Set(ref modelNo, value); } }
        #endregion


        private BarCodeModel barCodeModel;
        /// <summary>
        /// 合格证
        /// </summary>
        public BarCodeModel BarCodeModel { get => barCodeModel; set { Set(ref barCodeModel, value); } }

        private CertificateModel[] certificateModel;
        /// <summary>
        /// 合格证
        /// </summary>
        public CertificateModel[] CertificateModel { get => certificateModel; set { Set(ref certificateModel, value); } }

        /// <summary>
        /// 合格证位置(外部包装=0,产品包装=1)
        /// </summary>
        private CertificatePosition certificatePosition;
        /// <summary>
        /// 合格证位置(外部包装=0,产品包装=1)
        /// </summary>
        public CertificatePosition CertificatePosition { get => certificatePosition; set { Set(ref certificatePosition, value); } }


        /// <summary>
        /// 服务收费  (单个任务id 消耗的总费用)
        /// </summary>
        private decimal feesMoney;
        /// <summary>
        /// 服务收费  (单个任务id 消耗的总费用)
        /// </summary>
        public decimal FeesMoney { get => feesMoney; set { Set(ref feesMoney, value); } }

        /// <summary>
        /// 打包员
        /// </summary>
        private string packUser;
        /// <summary>
        /// 打包员
        /// </summary>
        public string PackUser { get => packUser; set { Set(ref packUser, value); } }


        private FeesItemResponse feesItemResponse;

        public FeesItemResponse FeesItemResponse { get => feesItemResponse; set { Set(ref feesItemResponse, value); } }

        public IList<SkuMessage> ItemList { get; set; }

        public string OrderId { get; set; }
        private int? floorDragNumber;
        public int? FloorDragNumber { get => floorDragNumber; set { Set(ref floorDragNumber, value); } }

        private string qualityRemainTime;
        /// <summary>
        /// 质检剩余时间
        /// </summary>
        public string QualityRemainTime { get => qualityRemainTime; set { Set(ref qualityRemainTime, value); } }

        private bool isQualityOverTime;
        public bool IsQualityOverTime { get => isQualityOverTime; set { Set(ref isQualityOverTime, value); } }

        /// <summary>
        /// 质检超时时间
        /// </summary>
        public DateTime? QualityCompletionOverTime { get; set; }




        private string packRemainTime;
        /// <summary>
        /// 打包剩余时间
        /// </summary>
        public string PackRemainTime { get => packRemainTime; set { Set(ref packRemainTime, value); } }

        /// <summary>
        /// 打包完成时间(超时时间)
        /// </summary>
        public DateTime? PackCompletionOverTime { get; set; }

        private bool isPackOverTime;
        public bool IsPackOverTime { get => isPackOverTime; set { Set(ref isPackOverTime, value); } }


        private bool isScheduleOverTime;
        public bool IsScheduleOverTime { get => isScheduleOverTime; set { Set(ref isScheduleOverTime, value); } }

        private string scheduleRemainTime;
        /// <summary>
        /// 打包剩余时间
        /// </summary>
        public string ScheduleRemainTime { get => scheduleRemainTime; set { Set(ref scheduleRemainTime, value); } }

        /// <summary>
        /// 排单超时备注信息
        /// </summary>
        public string ScheduleOverTimeMarkMsg { get; set; }


        /// <summary>
        /// 物流信息
        /// </summary>
        public List<ExpressOrderResponse> ExpressOrderList { get; set; }

        /// <summary>
        /// 需求方店铺id
        /// </summary>
        private string shopName;
        /// <summary>
        /// 需求方店铺id
        /// </summary>
        public string ShopName { get => shopName; set { Set(ref shopName, value); } }


        private string qualityOverTimeMarkMsg;
        /// <summary>
        /// 质检超时备注信息
        /// </summary>
        public string QualityOverTimeMarkMsg { get => qualityOverTimeMarkMsg; set { Set(ref qualityOverTimeMarkMsg, value); } }

        private string packOverTimeMarkMsg;
        /// <summary>
        /// 打包超时备注信息
        /// </summary>
        public string PackOverTimeMarkMsg { get => packOverTimeMarkMsg; set { Set(ref packOverTimeMarkMsg, value); } }
        /// <summary>
        /// 
        /// </summary>
        private string showMarkMessage;
        /// <summary>
        /// 备注消息列表
        /// </summary>
        public string ShowMarkMessage { get => showMarkMessage; set { Set(ref showMarkMessage, value); } }

        /// <summary>
        /// 采购方案
        /// </summary>
        public string SkuPurchaseSchemeId { get; set; }


        /// <summary>
        /// 排单超时时间
        /// </summary>
        public DateTime? ScheduleOverTime { get; set; }

        private PackProcessType? packProcessType;
        /// <summary>
        /// 打包工序类型(0=推荐工序,1=自定义工序)   NULL 金额不为0则为旧版本结算
        /// </summary>
        public PackProcessType? PackProcessType { get => packProcessType; set { Set(ref packProcessType, value); } }


        public string  ShopId { get; set; }

        /// <summary>
        /// 发送信息不全时间
        /// </summary>
        public DateTime? SendToSetSkuConfigureTime { get; set; }
    }

    public class SkuMessage : NotifyObject
    {
        private string skuId;

        public string SkuId { get => skuId; set { Set(ref skuId, value); } }

        /// <summary>
        /// 店铺Sku图链接
        /// </summary>
        private string logo;
        /// <summary>
        /// 店铺Sku图链接
        /// </summary>
        public string Logo { get => logo; set { Set(ref logo, value); } }

        /// <summary>
        /// 采购Sku名称
        /// </summary>
        private string skuName;
        /// <summary>
        /// 采购Sku名称
        /// </summary>
        public string SkuName { get => skuName; set { Set(ref skuName, value); } }

        /// <summary>
        /// 货号品名(手写上传)
        /// </summary>
        private string brandName;
        /// <summary>
        /// 货号品名(手写上传)
        /// </summary>
        public string BrandName { get => brandName; set { Set(ref brandName, value); } }

        /// <summary>
        /// 货号
        /// </summary>
        private string goodsNo;
        /// <summary>
        /// 货号
        /// </summary>
        public string GoodsNo { get => goodsNo; set { Set(ref goodsNo, value); } }



        


    }
}