<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 — Modal akceptacji regulaminu bramki płatności.
 *
 * Obowiązkowa akceptacja regulaminu przed przejściem do płatności
 * (wymóg przy przerzuceniu prowizji na klienta).
 *
 * Wymaga w zakresie zmiennej $enabled_gateways (lista bramek).
 * Dołączać RAZ na stronie z przyciskami płatności (faktury, dashboard).
 * Przechwytuje submit każdego formularza z klasą .payment-form.
 */
if (empty($enabled_gateways)) {
    return;
}

$gw_terms = [];
foreach ($enabled_gateways as $gw) {
    $gcfg = payment_load_config($gw);
    $gw_terms[$gw] = [
        'label' => payment_gateway_label($gw),
        'url'   => payment_terms_url($gw, $gcfg),
    ];
}
?>
<div class="modal fade" id="paymentTermsModal" tabindex="-1" aria-labelledby="paymentTermsModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="paymentTermsModalLabel">
                    <i class="bi bi-shield-check me-2"></i><?= __('payment.terms_title') ?>
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?= __('common.close') ?>"></button>
            </div>
            <div class="modal-body">
                <p class="mb-3"><?= __('payment.terms_intro') ?></p>
                <p class="mb-3">
                    <a href="#" id="paymentTermsLink" target="_blank" rel="noopener noreferrer">
                        <i class="bi bi-box-arrow-up-right me-1"></i><?= __('payment.terms_link') ?>:
                        <span class="payment-terms-gw fw-bold"></span>
                    </a>
                </p>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="paymentTermsCheck">
                    <label class="form-check-label" for="paymentTermsCheck"><?= __('payment.terms_checkbox') ?></label>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= __('common.cancel') ?></button>
                <button type="button" class="btn btn-success" id="paymentTermsProceed" disabled>
                    <i class="bi bi-credit-card me-1"></i><?= __('payment.terms_proceed') ?>
                </button>
            </div>
        </div>
    </div>
</div>
<script nonce="<?= csp_nonce() ?>">
(function () {
    var TERMS = <?= json_encode($gw_terms, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>;
    var modalEl = document.getElementById('paymentTermsModal');
    if (!modalEl || typeof bootstrap === 'undefined') return;

    var modal = new bootstrap.Modal(modalEl);
    var chk = document.getElementById('paymentTermsCheck');
    var proceed = document.getElementById('paymentTermsProceed');
    var link = document.getElementById('paymentTermsLink');
    var gwEls = modalEl.querySelectorAll('.payment-terms-gw');
    var pending = null;

    // Przechwyć submit każdego formularza płatności i wymuś akceptację regulaminu
    document.addEventListener('submit', function (e) {
        var form = e.target;
        if (!form || !form.classList || !form.classList.contains('payment-form')) return;
        e.preventDefault();
        pending = form;
        var gwInput = form.querySelector('input[name="gateway"]');
        var gw = gwInput ? gwInput.value : '';
        var info = TERMS[gw] || { label: gw, url: '#' };
        link.setAttribute('href', info.url);
        gwEls.forEach(function (el) { el.textContent = info.label; });
        chk.checked = false;
        proceed.disabled = true;
        modal.show();
    });

    chk.addEventListener('change', function () { proceed.disabled = !chk.checked; });

    proceed.addEventListener('click', function () {
        if (!pending || !chk.checked) return;
        var form = pending;
        pending = null;
        modal.hide();
        form.submit(); // submit programowy NIE wywołuje listenera 'submit' — brak pętli
    });
})();
</script>
