<script>
    document.addEventListener("DOMContentLoaded", function() {
        document.querySelectorAll('.accordion-body').forEach(accordionBody => {
            accordionBody.addEventListener('shown.bs.collapse', function() {
                document.getElementById('popup-iframe').height = document.body.clientHeight;
            });
        });

        document.getElementById('show-promo-field')?.addEventListener('click', function() {
            document.getElementById('apply-promo').style.display = 'block';
            this.style.display = 'none';
            document.getElementById('promocode')?.focus();
        });

        document.querySelectorAll('.register-login a').forEach(link => {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                const tabId = this.getAttribute('href');
                const tabElement = document.querySelector(tabId);
                const bsTab = new bootstrap.Tab(this);
                bsTab.show();
            });
        });

        document.querySelectorAll('.remove-cart-item').forEach(function(button) {
            button.addEventListener('click', function(event) {
                event.preventDefault();

                if (confirm("{{ 'Are you sure you want to remove this item from cart?'|trans }}")) {
                    var item_id = this.getAttribute('data-cart-item-id');

                    FOSSBilling.api.guest.post("cart/remove_item", { id: item_id },
                        () => {
                            FOSSBilling.message("{{ 'Item was removed from cart'|trans }}");
                            window.location.reload();
                        });
                }
            });
        });
    });

    function onOrderCheckout(result){
        if (result.invoice_hash) {
            FOSSBilling.api.guest.post('invoice/payment', { hash: result.invoice_hash, gateway_id: result.gateway_id, auto_redirect: true },
                (r) => {
                    let checkoutEl = document.getElementById('checkout');
                    let paymentHtmlEl = document.getElementById('payment-html');
                    if (r.iframe) {
                        document.getElementById('payment-html-inner').innerHTML = r.result;
                        bootstrap.Collapse.getOrCreateInstance(checkoutEl).hide();
                        document.getElementById('checkout').remove();
                        bootstrap.Collapse.getOrCreateInstance(paymentHtmlEl).show();
                    } else {
                        const link = '{{ "invoice/banklink"|url }}' + '/' + result.invoice_hash + '/' + result.gateway_id;
                        document.getElementById('payment-html-inner').innerHTML = '<a href="' + link + '" target="_parent" id="redirect-to-gateway">Redirect to Payment Gateway</a>';
                        bootstrap.Collapse.getOrCreateInstance(checkoutEl).hide();
                        document.getElementById('checkout-inner').remove();
                        bootstrap.Collapse.getOrCreateInstance(paymentHtmlEl).show();
                        document.getElementById('redirect-to-gateway').click();
                    }
                });
        } else {
            window.top.location.href = ('{{ "order/service/manage"|url }}' + '/' + result.order_id);
        }
    }

    function onLogin(result){
        FOSSBilling.message("{{ 'You logged in successfully'|trans }}");
        let registerEl = document.getElementById('register');
        new bootstrap.Collapse('#register').hide();
        document.querySelector("#checkoutButton").hidden = false;
        document.querySelector("#loginMessage").hidden = true;
        setTimeout(() => {
            registerEl.parentElement.remove();
            new bootstrap.Collapse('#checkout').show();
        }, 500);
    }

    function onAccountCreate(result){
        const login_details = {
            email: document.getElementById('reg-email').value,
            password: document.getElementById('reg-password').value
        };
        FOSSBilling.api.guest.post('client/login', login_details,
            () => {
                FOSSBilling.message("{{ 'You logged in successfully'|trans }}");
                let registerEl = document.getElementById('register');
                new bootstrap.Collapse('#register').hide();
                document.querySelector("#checkoutButton").hidden = false;
                document.querySelector("#loginMessage").hidden = true;
                setTimeout(() => {
                    registerEl.parentElement.remove();
                    new bootstrap.Collapse('#checkout').show();
                    }, 500);
            });
    }
</script>
