import Vue from 'vue';
import initVue from '@assets/js/initVue';
import formMixin from "@mixins/formMixin";
import axiosApi, {param} from "@assets/js/axios-api";

const vm = new Vue({
  data() {
    return {
      back: '',
      action: '',
      error: {},
      language: {},
      configurations: {},
    }
  },
  mixins: [formMixin],
  components: {
    BaseContent: () => import(/* webpackChunkName: "BaseContent" */ '@elements/BaseContent/index.vue'),
    BaseInput: () => import(/* webpackChunkName: "BaseInput" */ '@elements/BaseInput/index.vue'),
    BaseCep: () => import(/* webpackChunkName: "BaseCep" */ '@elements/BaseCep/index.vue'),
  },
  methods: {
    setData({ configurations, language, error, back, action }) {
      this.back = back;
      this.error = error;
      this.action = action;
      this.language = language;
      this.configurations = configurations;
    },
    dataReturn(selectedValues, moduleIndex) {},
    async onSubmit() {
      await axiosApi({
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        method: 'POST',
        url: this.action,
        data: param(this.configurations)
      })
        .then(({ data }) => {

          if (data.errors) {
            this.$refs.validationObserver.setErrors(data.errors);
            this.alert = {type: 'error', message: data.errors};
          }

          if (data.success) {
            setTimeout(() => {
              return window.location.href = data.success;
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err);
          this.alert = {type: 'warning', message: 'Error on address form'};
        })
    }    
  },
});

initVue(vm);