import Vue from 'vue';
import initVue from '@assets/js/initVue';
import axios from 'axios';

import formMixin from '@mixins/formMixin';

const vm = new Vue({
  components: {
    BaseAlert: () => import(/* webpackChunkName: "BaseAlert" */ '@elements/BaseAlert/index.vue'),
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    TheHeader: () => import(/* webpackChunkName: "TheHeader" */ '@components/TheHeader/index.vue'),
  },
  mixins: [formMixin],
  data() {
    return {
      isLoading: true,
      btnSubmitLoading: false,
      disableForm: false,
      alert: {},
      back: '',
      configurations: {},
      customerGroups: [],
    };
  },
  mounted() {
    this.isLoading = false;
  },
  methods: {
    setData({ configurations, customerGroups, back }) {
      this.configurations = configurations;
      this.customerGroups = customerGroups;
      this.back = back;
    },
    onAddDiscount() {
      this.configurations.desconto_tipo_pagamento = [
        ...this.configurations.desconto_tipo_pagamento,
        {
          modo_desconto: '',
          pagamento_desconto: '',
          prioridade_desconto: '',
          valor_minimo: '',
          status_desconto: '',
          text_desconto: '',
          tabela_preco: [],
          val_desconto: '',
        }
      ];
    },
    onRemoveDiscount(discountIndex) {
      this.configurations.desconto_tipo_pagamento.splice(discountIndex, 1);
    },
    onSubmit() {
      const paymentTypeDiscount = this.configurations.desconto_tipo_pagamento;
      const tablePriceNotExists = paymentTypeDiscount.some(item => item.tabela_preco.length == 0);

      if (tablePriceNotExists) {
        this.alert = {
          type: 'error',
          message: 'Algum dos módulo está sem uma tabela de preço selecionada.'
        }
        return;
      }

      const form = this.$refs.form;
      const url = form.action;
      const data = new FormData(form);

      this.btnSubmitLoading = true;
      this.disableForm = true;

      axios({
        method: 'post',
        url,
        data,
      }).then(({ data }) => {
        if (data.warning) {
          this.alert = {
            type: 'error',
            message: data.errors.warning
          }
        }
        if (data.errors) {
          this.$refs.validationObserver.setErrors(data.errors);
        }
        if (data.success) {
          window.location.href = this.back;
        }
      }).catch(() => {
        this.alert = {
          type: 'warning',
          message: 'Aconteceu algo de inesperado, por favor tente novamente!'
        }
      }).then(() => {
        this.disableForm = false;
        this.btnSubmitLoading = false;
      });
    },
    onChangeTablePrice(discountIndex) {
      if (!event.target.checked) {
        return;
      }

      if (event.target.value == 0) {
        this.configurations.desconto_tipo_pagamento[discountIndex].tabela_preco = [0];
      } else {
        const tablePrices = this.configurations.desconto_tipo_pagamento[discountIndex].tabela_preco;
        const tablePriceAllIndex = tablePrices.findIndex(tablePrice => tablePrice == '0');

        if (tablePriceAllIndex !== -1) {
          this.configurations.desconto_tipo_pagamento[discountIndex].tabela_preco.splice(tablePriceAllIndex, 1);
        }
      }
    }
  },
});

initVue(vm);