import Vue from "vue";

const vm = new Vue({
    components: {
        BaseManual: () => import(/* webpackChunkName: "BaseManual" */ '@elements/BaseManual/index.vue'),
    },
    data() {
        return {
            configurations: {},
        };
    },
    methods: {
        setData(configurations) {
            this.configurations = configurations
        }
    }
})

if (document.readyState === 'complete') {
    init();
  } else {
    document.addEventListener('DOMContentLoaded', function() {
      init();
    });
}

function init() {
    vm.setData(DATA);
    vm.$mount('#root');

    enableTooltips();
    handleDeleteButton();
}

function enableTooltips() {
    const icons = document.querySelectorAll('.icon-info');

    icons.forEach(icon => {
        const index = icon.dataset.index; // pega o data-index

        icon.addEventListener('mouseenter', () => {
            document.querySelectorAll('.show-modal-coupon').forEach(el => {
                if (el.dataset.index == index) {
                    el.classList.remove('hidden');
                }
            });
        });

        icon.addEventListener('mouseleave', () => {
            document.querySelectorAll('.show-modal-coupon').forEach(el => {
                if (el.dataset.index == index) {
                    el.classList.add('hidden');
                }
            });
        });
    });
}

function handleDeleteButton() {
    const deleteButton = document.querySelector('.js-delete-coupon');

    // seleciona todos os forms existentes nessa tela
    const forms = document.querySelectorAll('.js-main-form');

    const form = forms[1];

    if (deleteButton && form) {
        deleteButton.addEventListener('click', function(e) {
            e.preventDefault();

            // checkboxes marcados
            const checkboxes = Array.from(
                form.querySelectorAll('input[name="selected[]"]')
            ).filter(chk => chk.checked);

            if (checkboxes.length === 0) {
                alert('Por favor selecione um ou mais cupons antes de excluir.');
                return;
            }

            if (confirm('Tem certeza que deseja excluir os cupons selecionados?')) {
                form.submit();
            }
        });
    }
}