using BBWY.Client.Models.APIModel.Request;
using BBWY.Client.Models;
using BBWY.Client.Views.PackTask;
using GalaSoft.MvvmLight.Command;
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 BBWY.Client.Extensions;
using BBWY.Client.APIServices;
using BBWY.Controls;
using System.DirectoryServices.ActiveDirectory;
using System.Threading;
using System.Threading.Tasks;

namespace BBWY.Client.Views.QualityTask
{
    /// <summary>
    /// QualitySetCerWindow.xaml 的交互逻辑
    /// </summary>
    public partial class QualitySetCerWindow : BWindow
    {

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

        private bool isLoading;

        private bool isShowProductAdress=false;
        public bool IsShowProductAdress { get => isShowProductAdress; set {Set(ref isShowProductAdress,value); } }

        private bool isShowShader = false;
        public bool IsShowShader { get => isShowShader; set { Set(ref isShowShader, value); } }
        private bool isShowProductShop = false;
        public bool IsShowProductShop { get => isShowProductShop; set { Set(ref isShowProductShop, value); } }

        private bool isShowExcuteStander = false;
        public bool IsShowExcuteStander { get => isShowExcuteStander; set { Set(ref isShowExcuteStander, value); } }




        string orderId { get; set; }

        public QualitySetCerWindow( bool IsQualityTask, string orderId)
        {
            InitializeComponent();
            SetPackCerStateCommand = new RelayCommand<PackCerState>(SetPackCerState);
            ShowDataCommand = new RelayCommand<string>(ShowData);
            //if (IsQualityTask)
            //{
            //    IsShowShader = true;
            //    IsShowExcuteStander = true;
            //    IsShowProductShop = true;
            //    IsShowProductAdress = true;
            //}

            this.orderId = orderId;
        }

        private void ShowData(string obj)
        {
            if (IsLoading)
            {
                return;
            }
            if (obj== "材质")
            {
                IsShowShader = false;
            }
            if (obj == "生产商")
            {
                IsShowProductShop = false;
            }
            if (obj == "执行标准")
            {
                IsShowExcuteStander = false;
            }
            if (obj == "地址")
            {
                IsShowProductAdress = false;
            }
            IsLoading = true;

            Task.Factory.StartNew(() => {
                Thread.Sleep(1000);
                IsLoading = false;

            });
           


            
           
        }

        private void SetPackCerState(PackCerState obj)
        {
            PackCerState = obj;
        }

        public void LoadData(bool isNeedCer, CertificateModel CertificateModel, PackPurchaseTaskService packTaskService, CertificateModel SpuCertificateModel, bool IsSetSpuCertificate,SaveType? saveType)
        {
            this.CertificateModel = CertificateModel.Copy();
            this.packTaskService = packTaskService;
            PackCerState = isNeedCer ? PackCerState.合格证信息 : PackCerState.无需合格证;
            SaveType = saveType;
            this.DataContext = this;
        }

        public SaveType? SaveType { get; set; }
        public ICommand SetPackCerStateCommand { get; set; }

        public ICommand ShowDataCommand { get; set; }



        public PackCerState PackCerState
        {
            get
            {
                return (PackCerState)GetValue(PackCerStateProperty);

            }
            set
            {
                SetValue(PackCerStateProperty, value);

            }
        }


        public static readonly DependencyProperty PackCerStateProperty =
            DependencyProperty.Register("PackCerState", typeof(PackCerState), typeof(QualitySetCerWindow));

        public bool IsSetSpuCertificate { get; set; }
        public PackPurchaseTaskService packTaskService { get; set; }
        public CertificateModel CertificateModel { get; set; }

        public CertificateModel SpuCertificateModel { get; set; }

        public Action<CertificateModel, PackCerState> SaveResult { get; set; }


        private void save_btn_Click(object sender, RoutedEventArgs e)
        {


            if (PackCerState== PackCerState.无需合格证)
            {
                if (SaveResult != null) SaveResult(CertificateModel, PackCerState);
                this.Close();
                return;
            }







            if (string.IsNullOrEmpty(CertificateModel.ExcuteStander)
                || string.IsNullOrEmpty(CertificateModel.Shader) || string.IsNullOrEmpty(CertificateModel.BrandName)
                || string.IsNullOrEmpty(CertificateModel.Brand) || string.IsNullOrEmpty(CertificateModel.ProductShop)
                || string.IsNullOrEmpty(CertificateModel.ProductAdress))
            {
                //new TipsWindow("参数出错!请重新填写!").Show();
                MessageBox.Show("有未填写的参数");
                return;
            }
            if((CertificateModel.LabelModel== CertificateLabelModel.标准无3c|| CertificateModel.LabelModel == CertificateLabelModel.标准有3c) && string.IsNullOrEmpty( CertificateModel.ProductNo))
            {
                MessageBox.Show("型号不能为空");
                return;
            }
            if ( CertificateModel.LabelModel == CertificateLabelModel.标准有3c && string.IsNullOrEmpty(CertificateModel.FactoryNumber))
            {
                MessageBox.Show("工厂编号不能为空");
                return;
            }
            if (CertificateModel.LabelModel == CertificateLabelModel.适用年龄 && string.IsNullOrEmpty(CertificateModel.ApplyAge))
            {
                MessageBox.Show("适用年龄不能为空");
                return;
            }


            if (IsShowShader)
            {
                MessageBox.Show("请先确认材质");
                return;
            }

            if (IsShowProductShop)
            {
                MessageBox.Show("请先确认生产商");
                return;
            }
            if (IsShowExcuteStander)
            {
                MessageBox.Show("请先确认执行标准");
                return;
            }

            if (IsShowProductAdress)
            {
                MessageBox.Show("请先确认地址");
                return;
            }



            var standers = CertificateModel.ExcuteStander.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var resData = packTaskService.SaveCer(new CerRequest
            {
                Brand = CertificateModel.Brand,
                BrandName = CertificateModel.BrandName,
                ProductNo = CertificateModel.ProductNo,
                SkuId = CertificateModel.SkuId,
                ExcuteStander = CertificateModel.ExcuteStander,
                LabelModel = (int)CertificateModel.LabelModel,
                FactoryNumber = CertificateModel.FactoryNumber,
                IsLogo = CertificateModel.IsLogo,
                ProductAdress = CertificateModel.ProductAdress,
                ProductShop = CertificateModel.ProductShop,
                Shader = CertificateModel.Shader,
                ApplyAge = CertificateModel.ApplyAge,
                GoodsNumber = CertificateModel.GoodsNumber,
                ProduceDate = CertificateModel.ProduceDate,
                PurchaseSkuId = CertificateModel.PurchaseSkuId,
                 SaveType = SaveType,
                OrderId = orderId
            });
            if (resData == null || !resData.Success)
            {
                return;
            }

            CertificateModel.Id = resData.Data;
            if (SaveResult != null) SaveResult(CertificateModel, PackCerState);
            this.Close();
        }
    }
}