import AdmVue from '@assets/js/adm-vue';
import copyContent from '@assets/js/copyContent';
import accountB2w from '../../../model/marketplace/skyhub/accountB2w';
import modelPrice from '../../../model/marketplace/marketplace/marketplacePrice.js';
import formMixin from "@mixins/formMixin";

const vm = new AdmVue({
  components: {
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    BaseInputFile: () => import(/* webpackChunkName: "BaseInputFile" */ '@elements/BaseInputFile/index.vue'),
    BaseCheckbox: () => import(/* webpackChunkName: "BaseInputFile" */ '@elements/BaseCheckbox/index.vue'),
    BaseTabs: () => import(/* webpackChunkName: "BaseTabs" */ '@components/BaseTabs/index.vue'),
    AsideContent: () => import(/* webpackChunkName: "AsideContent" */ '@components/AsideContent/index.vue'),
    BaseTextarea: () => import(/* webpackChunkName: "BaseTextarea" */ '@elements/BaseTextarea/index.vue'),
    GroupInput: () => import(/* webpackChunkName: "BaseTabs" */ '@components/GroupInput/index.vue'),
    MarketPrice: () => import(/* webpackChunkName: "MarketPrice" */ '@components/MarketPrice/index.vue'),
    BaseModal: () => import(/* webpackChunkName: "BaseModal" */ '@components/BaseModal/index.vue'),
  },
  data() {
    return {
      configurations: {
        skyhub_status: false,
        skyhub_email: '',
        skyhub_api_key: '',
        skyhub_initial_description: '',
        skyhub_final_description: '',
        skyhub_shipping_method: [],
        skyhub_order_status_new: '',
        skyhub_order_status_aproved: '',
        skyhub_order_status_canceled: '',
        skyhub_order_status_shipped: '',
        skyhub_order_status_nfe: '',
        skyhub_order_status_delivered: '',
        skyhub_order_status_shipment_exception: '',
        skyhub_order_status_confirm_stock: '',
        skyhub_order_status_confirmed_stock: '',
        skyhub_order_status_canceled_stock: '',
        skyhub_order_status_processing_store: '',
        skyhub_comission_status: false,
        skyhub_comission: [],
        skyhub_order_status_new_type: '',
        skyhub_order_status_aproved_type: '',
        skyhub_order_status_canceled_type: '',
        skyhub_order_status_shipped_type: '',
        skyhub_order_status_nfe_type: '',
        skyhub_order_status_delivered_type: '',
        skyhub_order_status_shipment_exception_type: '',
        skyhub_order_status_confirm_stock_type: '',
        skyhub_order_status_confirmed_stock_type: '',
        skyhub_order_status_canceled_stock_type: '',
        skyhub_order_status_processing_store_type: ''
      },
      models:{
        accountB2w
      },
      accountB2w: {},
      disableForm: false,
      loadingBtn: false,
      marketPriceObject: {},
      mktPrice : [],
      mktPriceDelete : [],
      textUrlCallCopy: '',
      textUrlNotyCopy: '',
      showModal: false,
    }
  },
  created() {},
  beforeMount() {

    this.marketPriceObject = JSON.parse(this.marketprice);

    if (this.marketPriceObject.mktpriceadjustments.length) {
      this.marketPriceObject.mktpriceadjustments = this.marketPriceObject.mktpriceadjustments;
    }

    this.tabs = this.skySettingsTabs;
    this.textUrlCallCopy = this.text_url_copy;
    this.textUrlNotyCopy = this.text_url_copy;

    this.skyhub_order_status_new_type = this.typeStatusAction.Tp_Recebe;
    this.skyhub_order_status_aproved_type = this.typeStatusAction.Tp_Recebe;
    this.skyhub_order_status_canceled_type = this.typeStatusAction.Tp_EnviaRecebe;
    this.skyhub_order_status_shipped_type = this.typeStatusAction.Tp_EnviaRecebe;
    this.skyhub_order_status_nfe_type = this.typeStatusAction.Tp_Envia;
    this.skyhub_order_status_delivered_type = this.typeStatusAction.Tp_EnviaRecebe;
    this.skyhub_order_status_shipment_exception_type = this.typeStatusAction.Tp_Envia;
    this.skyhub_order_status_confirm_stock_type = this.typeStatusAction.Tp_Recebe;
    this.skyhub_order_status_confirmed_stock_type = this.typeStatusAction.Tp_Envia;
    this.skyhub_order_status_canceled_stock_type = this.typeStatusAction.Tp_Envia;
    this.skyhub_order_status_processing_store_type = this.typeStatusAction.Tp_Envia;

  },
  mounted() {},
  watch: {},
  computed: {
    validateDuplicateStatus() {
      const result = { valid: {}, message: 'Existem status de pedido repetidos' };

      const status = [
        'skyhub_order_status_new',
        'skyhub_order_status_aproved',
        'skyhub_order_status_canceled',
        'skyhub_order_status_shipped',
        'skyhub_order_status_nfe',
        'skyhub_order_status_delivered',
        'skyhub_order_status_shipment_exception',
        'skyhub_order_status_confirm_stock',
        'skyhub_order_status_confirm_stock',
        'skyhub_order_status_confirmed_stock',
        'skyhub_order_status_canceled_stock',
        'skyhub_order_status_processing_store',
      ];

      const valid = status.reduce((acc, value, _, self) => {
          acc[value] = true;
          for (const key in self) {
            if (self[key] != value ) {
               if (this.configurations[value] == this.configurations[self[key]]) {
                 acc[value] = false;
                 return acc;
               }
            }
          }

          return acc;

      }, {});

      result.valid = valid;
      return result;
    },
    teste(){},
    sendData() {
      const mktPriceAdjustments = this.mktPrice.map( item => {
        item.marketplace_price_adjustments = item.marketplace_price_adjustments.filter(value => value != null);
        item = modelPrice.format(item);
        return item;
      });
      return{
        ...this.configurations,
        mktpriceadjustments: {data: mktPriceAdjustments, delete: this.mktPriceDelete}
      }
    },
  },
  methods: {
    setErrors(errors){
      this.$refs.validationObserver.setErrors(errors);
    },
    formError(data, validationObserver) {

      validationObserver = validationObserver || this.$refs.validationObserver;

      const { errors } = data ;
      validationObserver.setErrors(errors);
      if (errors.warning) {
        this.alert = {
          type: 'warning',
          message: errors.warning,
        };
      }
    },
    formSuccess({mktpriceadjustments}) {
      const marketPriceObject = this.cloneObject(this.marketPriceObject);
      marketPriceObject.mktpriceadjustments = mktpriceadjustments;
      this.marketPriceObject = marketPriceObject;
    },
    clickTab(tab, value) {
      this[tab] = value;
    },
    parseBoolean(value) {
      return Boolean(Number(value));
    },
    onCopyUrl(url) {
      copyContent(url);

      if (url == this.url.store) {
        this.textUrlCallCopy = this.text_url_coped;
        setTimeout(() => {
          this.textUrlCallCopy = this.text_url_copy;
        }, 2000);
      } else {
        this.textUrlNotyCopy = this.text_url_coped;
        setTimeout(() => {
          this.textUrlNotyCopy = this.text_url_copy;
        }, 2000);
      }
    },
    closeModal() {
      this.showModal = false;
    },
    handleModal() {
      this.showModal = true;
    },
    onSubmit(form = {}, sendData, validationObserver) {
      //essa funcao eh assim pra fazer um override por conta de uma mensagem que deveria aparecer, como eh reativo, foi feito assim
      this.configurations.skyhub_invalid_credential = 0;
      formMixin.methods.onSubmit.call(this, [form, sendData, validationObserver])
    }
  },
});

AdmVue.init(vm);