// Common reusable JS for all modules
// Show a generic message modal
function showMessageModal(message, type, onClose) {
    var modal = new bootstrap.Modal(document.getElementById('messageModal'));
    var body = document.getElementById('messageModalBody');
    var title = document.getElementById('messageModalLabel');
    // Set color and icon by type
    let icon = '', color = '#0d6efd', titleText = 'Message';
    if (type === 'success') { icon = '✔️'; color = '#198754'; titleText = 'Success'; }
    else if (type === 'danger') { icon = '❌'; color = '#dc3545'; titleText = 'Error'; }
    else if (type === 'warning') { icon = '⚠️'; color = '#ffc107'; titleText = 'Warning'; }
    else if (type === 'info') { icon = 'ℹ️'; color = '#0dcaf0'; titleText = 'Info'; }
    title.innerHTML = icon + ' <span style="color:' + color + '">' + titleText + '</span>';
    body.innerHTML = message;
    modal.show();
    // Optional callback after close
    var closeBtn = document.querySelector('#messageModal .btn-close');
    var footerCloseBtn = document.querySelector('#messageModal .modal-footer .btn');
    function handler() {
        if (onClose) onClose();
        closeBtn.removeEventListener('click', handler);
        footerCloseBtn.removeEventListener('click', handler);
    }
    closeBtn.addEventListener('click', handler);
    footerCloseBtn.addEventListener('click', handler);
}

// Show a generic confirm modal
function showConfirmModal(message, callback) {
    var modalEl = document.getElementById('confirmModal');
    var modal = new bootstrap.Modal(modalEl);
    document.getElementById('confirmModalBody').innerHTML = message;
    var okBtn = document.getElementById('confirmModalOk');
    var cancelBtn = document.getElementById('confirmModalCancel');
    function cleanup() {
        okBtn.removeEventListener('click', okHandler);
        cancelBtn.removeEventListener('click', cancelHandler);
        modalEl.removeEventListener('hidden.bs.modal', cancelHandler);
    }
    function okHandler() {
        cleanup();
        modal.hide();
        callback(true);
    }
    function cancelHandler() {
        cleanup();
        modal.hide();
        callback(false);
    }
    okBtn.addEventListener('click', okHandler);
    cancelBtn.addEventListener('click', cancelHandler);
    modalEl.addEventListener('hidden.bs.modal', cancelHandler);
    modal.show();
}
