import Vue from 'vue';
import formMixin from "@mixins/formMixin";
import initVue from '@assets/js/initVue';

const vm = new Vue({
  data() {
    return {
      configurations: {},
      language: {},
      error: {},
      back: '',
      action: '',
      loading: true,
    }
  },
  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.configurations = configurations;
      this.language = language;
      this.error = error;
      this.back = back;
      this.action = action;
      this.validateMessage();
    },
    validateMessage() {
      let hasAnyError = false;

      Object.keys(this.error).forEach((item, key) => {
        if (this.error[item][0]) {
          hasAnyError = true;
        }
      });

      if (hasAnyError) {
        notificationMsg('Alguns campos estão incorretos. Corrija os campos em vermelho.', 'error');
      }
    },
    onInput(fieldName) {
      if (this.configurations.lastname != "" && this.configurations.lastname.length < 32) {
        this.error.error_address_lastname[0] = "";
      }

      if (this.configurations.firstname != "" && this.configurations.firstname.length < 32) {
        this.error.error_address_firstname[0] = "";
      }
    },
    dataReturn(selectedValues, moduleIndex) {
      if (this.error['error_address_postcode'][moduleIndex] && selectedValues.postcode != "") {
        this.error['error_address_postcode'][moduleIndex] = "";
      }

      if (this.error['error_address_address_1'][moduleIndex] && selectedValues.address_1 != "") {
        this.error['error_address_address_1'][moduleIndex] = "";
      }

      if (this.error['error_address_address_2'][moduleIndex] && selectedValues.address_2 != "") {
        this.error['error_address_address_2'][moduleIndex] = "";
      }

      if (this.error['error_address_apelido'][moduleIndex] && selectedValues.apelido != "") {
        this.error['error_address_apelido'][moduleIndex] = "";
      }

      if (this.error['error_address_city'][moduleIndex] && selectedValues.city != "") {
        this.error['error_address_city'][moduleIndex] = "";
      }

      if (this.error['error_address_complemento'][moduleIndex] && selectedValues.complemento != "") {
        this.error['error_address_complemento'][moduleIndex] = "";
      }

      if (this.error['error_address_numero'][moduleIndex] && selectedValues.numero != "") {
        this.error['error_address_numero'][moduleIndex] = "";
      }

      if (this.error['error_address_zone'][moduleIndex] && selectedValues.zone_id != "") {
        this.error['error_address_zone'][moduleIndex] = "";
      }
    },
  },
  mounted() {
    this.loading = false;
  }
});

initVue(vm);

