{{-- Shared unblock modal — uses Bootstrap's native data-toggle + the
     show.bs.modal event with relatedTarget. This avoids any custom
     click binding (which kept failing in earlier iterations because
     the click handler bound before relevant elements existed, or the
     button was wrapped in something that swallowed the click).

     Every .vc-unblock-btn on the page MUST carry:
        data-toggle="modal"
        data-target="#vcUnblockModal"
        data-form-action="..."
        data-owner-name="..."
        data-has-backup="1|0"
        data-backup-value="..."
--}}
<div class="modal fade" id="vcUnblockModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <form id="vc_unblock_form" method="POST" action="">
            @csrf
            <div class="modal-content">
                <div class="modal-header" style="background:#16a34a;color:#fff;">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">
                        <i class="fa fa-lock-open"></i>
                        Unblock <span id="vc_unblock_name"></span>
                    </h4>
                </div>
                <div class="modal-body">
                    <p>Confirm unblocking. The owner regains booking and invoicing.</p>
                    <div id="vc_unblock_credit_section"
                         style="background:#fef3c7;border:1px solid #fde68a;border-radius:6px;padding:10px 14px;">
                        <p style="margin:0 0 8px;font-weight:700;color:#92400e;">
                            <i class="fa fa-coins"></i> Credit limit
                        </p>
                        <p style="margin:0 0 8px;font-size:12.5px;color:#78350f;">
                            The credit limit was set to <strong>0</strong> when the owner was blocked.
                            What should happen on unblock?
                        </p>
                        <label style="font-weight:normal;display:block;margin-bottom:4px;">
                            <input type="radio" name="restore_credit_limit" value="1" checked>
                            <strong>Restore</strong> the previous credit limit
                            (was <span id="vc_unblock_backup_value">—</span>).
                        </label>
                        <label style="font-weight:normal;display:block;">
                            <input type="radio" name="restore_credit_limit" value="0">
                            <strong>Keep at 0</strong> — unblock but require manual limit reset.
                        </label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success">
                        <i class="fa fa-lock-open"></i> Unblock
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
// Bootstrap-native: the show.bs.modal event fires REGARDLESS of how the
// modal was triggered (data-toggle click, programmatic .modal('show'),
// etc). relatedTarget is the element that triggered the modal. No
// custom click delegation, no race with jQuery ready, no event swallow.
(function () {
    if (window.__vcUnblockModalWired) return;
    window.__vcUnblockModalWired = true;

    function wire() {
        if (typeof $ !== 'function' || !$('#vcUnblockModal').length) {
            return setTimeout(wire, 100);
        }
        $('#vcUnblockModal').on('show.bs.modal', function (e) {
            var btn = e.relatedTarget;
            if (!btn) { console.warn('vcUnblockModal: no relatedTarget'); return; }
            var $b = $(btn);
            var action = $b.data('form-action');
            if (!action) { console.warn('vcUnblockModal: button missing data-form-action', btn); return; }
            $('#vc_unblock_form').attr('action', action);
            $('#vc_unblock_name').text($b.data('owner-name') || '');
            var hasBackup = String($b.data('has-backup')) === '1';
            if (hasBackup) {
                $('#vc_unblock_credit_section').show();
                $('#vc_unblock_backup_value').text($b.data('backup-value') || '0');
            } else {
                $('#vc_unblock_credit_section').hide();
            }
        });
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', wire);
    } else {
        wire();
    }
})();
</script>
