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

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

const vm = new AdmVue({
  data() {
    return {
      showModal: false,
      btnAddList: false,
      id_list: '',
      newLink: 'index.php?route=listapresente/listapresente/new',
    }
  },
  components: {
    BaseModal: () => import(/* webpackChunkName: "BaseModal" */ '@components/BaseModal/index.vue'),
    BaseButton: () => import(/* webpackChunkName: "Basebutton" */ '@elements/BaseButton/index.vue'),
    BaseListing: () => import(/* webpackChunkName: "BaseListing" */ '@elements/BaseListing/index.vue'),
  },
  methods: {
    listStatus(listStatus) {
      if (listStatus === 'Ativa') return 'success';
      if (listStatus === 'Inativa') return 'error';
    },
    async onRemoveList() {
      await axiosApi({
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        url: '/index.php?route=listapresente/listapresente/onRemoveList' + `&id=${this.id_list}`,
        method: 'GET',
      })
        .then(({ data }) => {
          
          const { success, warning } = data;
          
          if(success) {
            const newList = this.listas.filter( (list) => {            
              if (list.id !== this.id_list) return list;
            });
            this.listas = newList;
            notificationMsg(success, 'success');
          }  

          if (warning) notificationMsg(warning, 'warning'); 

          this.onClose();
        })
        .catch(err => console.log(err, err.response));
    },
    async onAddList() {
      this.btnAddList = true;
      await axiosApi({
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        url: '/index.php?route=listapresente/listapresente/validateLimitList',
        method: 'GET',
      })
        .then(({ data }) => {

          if (data.message) return notificationMsg(data.message, 'warning');
          
          window.location.href = BASE_URL + this.newLink; 
        })
        .catch(err => console.log(err, err.response))
        .finally(() => { this.btnAddList = false; });
    },
    onOpen(id) {
      this.id_list = id;
      this.showModal = true;
    },
    onClose() {
      this.showModal = false;
    },
    onUpdateList() {
      if (this.successNew) return notificationMsg(this.successNew ,'success');
      if (this.successEdit) return notificationMsg(this.successEdit ,'success');
    },
  },
  mounted() {
    document.getElementById('gift-lists').classList.remove('hidden'); 
    this.onUpdateList();
  },
});

AdmVue.init(vm);