<?php
$msg = $_SESSION['login_msg'] ?? $_SESSION['msg'] ?? '';
$alert_type = $_SESSION['alert_type'] ?? $_SESSION['type'] ?? 'info';
$redirect_url = $_SESSION['redirect_url'] ?? '';

if (!empty($msg)):
?>
<!-- SweetAlert Trigger -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    var message = <?= json_encode($msg) ?>;
    var alertType = <?= json_encode($alert_type) ?>;
    var redirect = <?= json_encode($redirect_url) ?>;

    if (typeof Swal !== 'undefined' && typeof Swal.fire === 'function') {
        Swal.fire({
            position: 'center',
            icon: alertType,
            html: message,
            showConfirmButton: true,
            confirmButtonText: 'Okay',
            confirmButtonColor: '#1a5c2a'
        }).then(function() {
            if (redirect) {
                window.location.href = redirect;
            }
        });
    } else if (typeof swal === 'function') {
        swal({
            title: alertType.toUpperCase(),
            text: message.replace(/<[^>]*>?/gm, ''),
            type: alertType,
            confirmButtonText: 'Okay'
        }, function() {
            if (redirect) {
                window.location.href = redirect;
            }
        });
    } else {
        alert(message.replace(/<[^>]*>?/gm, ''));
        if (redirect) {
            window.location.href = redirect;
        }
    }
});
</script>
<?php
unset($_SESSION['login_msg'], $_SESSION['msg'], $_SESSION['alert_type'], $_SESSION['type'], $_SESSION['redirect_url']);
endif;
?>
