<?php
$errorMessage = $errorMessage ?? session('errorMessage');
$warningMessage = session('warningMessage');

if (session()->has('message')) {
    $successMessage = session('message');
}
if (session()->has('error')) {
    $errorMessage = is_array(session('error')) ? implode(session('error')) : session('error');
} /* // Uncomment this block if you want the errors listed line by line in the alert
    elseif (session()->has('errors')) {
        $errorMessage = '<ul class="text-start">';
        foreach (session('errors') as $error) :
            $errorMessage .= '<li>' . $error . '</li>';
        endforeach;
        $errorMessage .= '</ul>';
    }
    */
?>

<div id="sk-alert">
</div>


<?= $this->section('additionalInlineJs') ?>

function popAlert(message, alertClass, alertIcon){
    var htmlString = `
        <div class="alert ${alertClass} d-flex align-items-baseline" role="alert">
            <span class="alert-icon alert-icon-lg text-primary me-2">
                <i class="ti ${alertIcon} ti-sm"></i>
            </span>
            <div class="d-flex flex-column ps-1">
                <h5 class="alert-heading mb-2">${message}</h5>
            </div>
        </div>`;
    $(window).scrollTop(0);
    $('#sk-alert').hide().empty().html(htmlString).fadeIn("slow", function(){
        setTimeout(function(){
            $('#sk-alert').fadeOut("slow");
        }, 5000);
    });
}

function popSuccessAlert(successMsg){
    popAlert(successMsg, "alert-success", "ti-check");
}

function popWarningAlert(warningMsg){
    popAlert(warningMsg, "alert-warning", "ti-bell");
}

function popErrorAlert(errorMsg){
    popAlert(errorMsg, "alert-danger", "ti-ban");
}

<?php if (isset($successMessage) && $successMessage){ ?>
    popSuccessAlert(`<?= $successMessage ?>`);
<?php } ?>

<?php if (isset($warningMessage) && $warningMessage){ ?>
    popWarningAlert(`<?= $warningMessage ?>`);
<?php } ?>

<?php if (isset($errorMessage) && $errorMessage){ ?>
    popErrorAlert(`<?= $errorMessage ?>`);
<?php } ?>

<?= $this->endSection() ?>


