import AdmVue from '@assets/js/adm-vue';
import axiosApi from '@assets/js/axios-api';
import param from '@assets/js/param';

const BASE_URL = document.getElementById('base-urls').dataset.baseUrlLoja;

const vm = new AdmVue({
  components: {
    BaseTabs: () => import(/* webpackChunkName: "BaseTabs" */ '@components/BaseTabs/index.vue'),
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    BaseInput: () => import(/* webpackChunkName: "BaseInput" */ '@elements/BaseInput/index.vue'),
    BaseButton: () => import(/* webpackChunkName: "Basebutton" */ '@elements/BaseButton/index.vue'),
    BaseModal: () => import(/* webpackChunkName: "BaseModal" */ '@components/BaseModal/index.vue'),
  },
  data() {
    return {
      tabs: [
        { text: 'Geral', active: true },
        { text: 'Status do Pagamento', active: false },
        { text: 'Configurações Avançadas', active: false },
      ],
      configurations: {},
      showTermsModal: false,
      matera_account_info: false,
      matera_billing_address: false,
      loading: false,
      txFixedValue: 0,
      txPercentValue: 1,
      txRangeValue: 2,
  }

  },
  methods: {
    //requests
    async getAccountInfo(errors) {
      this.$refs.provider.validate();
      if(errors[0] || !this.configurations.matera_account_info.cnpj) return;
      this.loading = true;
      await axiosApi({
        method: 'POST',
        url: BASE_URL + 'admin/index.php?route=payment/matera/getAccountInfo&token=' + this.token,
        data: param({cnpj: this.configurations.matera_account_info.cnpj}),
      })
        .then(({ data }) => {
          if (data && !data.erros) {
            notificationMsg('Verificação realizada com sucesso!', 'success');
            this.responseAccountInfo(data);  
            this.taxFormatter(data);     
          }else{
            notificationMsg(data.erros, 'warning');
            this.loading = false;
          }
        })
        .catch(err => console.log(err, err.response));
    },
    //responses
    responseAccountInfo(data){
      this.configurations.matera_account_info = data.conta;
     
      const accountFounded = data.transacao.find(item => item.mensagem === "CONTA CADASTRADA");

      if (accountFounded) {
        const parsedJson = JSON.parse(accountFounded.json_envio);
        this.configurations.matera_billing_address = parsedJson.billingAddress;
      } else {
        this.configurations.matera_billing_address = [];
      };
      this.loading = false;
    },
    taxFormatter(data){
      const taxAccount = data.conta;

      if(taxAccount.ec_tx_tipo === this.txFixedValue){
        return this.configurations.txName = 'Fixa';
      }
      if(taxAccount.ec_tx_tipo === this.txPercentValue){
        return this.configurations.txName = 'Percentual';
      }
      if(taxAccount.ec_tx_tipo === this.txRangeValue){
        return this.configurations.txName = 'Faixa de valor';
      } 
    },
    displayValue(type) {
      switch (type) {
        case this.txFixedValue:
          return 'Fixa';
        case this.txPercentValue:
          return 'Percentual';
        case this.txRangeValue:
          return 'Faixa de valor';
        default:
          return null;
      }
    }
  },
  mounted(){
  }
});

AdmVue.init(vm);