import AdmVue from '@assets/js/adm-vue';
import modelPrice from '../../../model/marketplace/marketplace/marketplacePrice.js';

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: {
        galeriapage_status: false,
        galeriapage_email: '',
        galeriapage_password: '',
        galeriapage_initial_description: '',
        galeriapage_final_description: '',
        galeriapage_environment: '',
        gp_order_status_waiting_payment: '',
        gp_order_status_approved: '',
        gp_order_status_processing: '',
        gp_order_status_invoiced: '',
        gp_order_status_send: '',
        gp_order_status_delivered: '',
        gp_order_status_canceled: '',
      },
      disableForm: false,
      loadingBtn: false,
      loadingBtnStatus: false,
      loadingBtnLogin: false,
      marketPriceObject: {},
      mktPrice: [],
      mktPriceDelete: [],
      textUrlCallCopy: '',
      textUrlNotyCopy: '',
    };
  },
  created() {},
  beforeMount() {
    this.marketPriceObject = JSON.parse(this.marketprice);

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

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

      const status = [
        'gp_order_status_waiting_payment',
        'gp_order_status_approved',
        'gp_order_status_processing',
        'gp_order_status_invoiced',
        'gp_order_status_send',
        'gp_order_status_delivered',
        'gp_order_status_canceled',
      ];

      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;
    },
    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));
    },
    validateStatusApi: async function() {
      try {
        this.loadingBtnStatus = true;

        const params = {
          galeriapage_environment: this.configurations.galeriapage_environment,
        };

        const { data } = await this.axiosApi({
          url: this.url.statusApi,
          method: 'GET',
          params,
        });

        const { error, sucess, message } = data;

        if (error) {
          this.alert = {
            type: 'error',
            message: message,
          };
        }

        if (sucess) {
          this.alert = {
            type: 'success',
            message: message,
          };
        }
      } catch (e) {
        console.log(e);
      } finally {
        this.configurations = { ...this.configurations };
        this.loadingBtnStatus = false;
      }
    },
    validateLogin: async function() {
      try {
        this.loadingBtnLogin = true;

        const params = {
          galeriapage_email: this.configurations.galeriapage_email,
          galeriapage_password: this.configurations.galeriapage_password,
          galeriapage_environment: this.configurations.galeriapage_environment,
        };

        const { data } = await this.axiosApi({
          url: this.url.authApi,
          method: 'GET',
          params,
        });

        const { error, status, message } = data;

        if (error) {
          this.alert = {
            type: 'error',
            message: message,
          };
        }

        if (status) {
          this.alert = {
            type: 'success',
            message: message,
          };
        } else {
          this.alert = {
            type: 'error',
            message: message,
          };
        }
      } catch (e) {
        console.log(e);
      } finally {
        this.configurations = { ...this.configurations };
        this.loadingBtnLogin = false;
      }
    },
  },
});

AdmVue.init(vm);
