<?php
/**
 * File: includes/flash_message.php
 * Menangani tampilan flash message/session notification
 */

// Periksa apakah ada pesan flash yang disimpan di session
$flash_types = ['success', 'error', 'warning', 'info'];
?>

<?php foreach ($flash_types as $type): ?>
    <?php if (isset($_SESSION[$type]) && !empty($_SESSION[$type])): ?>
        <div class="alert alert-<?= $type === 'error' ? 'danger' : $type ?> alert-dismissible fade show mb-4" role="alert">
            <div class="d-flex align-items-center">
                <?php if ($type === 'success'): ?>
                    <i class="bi bi-check-circle-fill me-2"></i>
                <?php elseif ($type === 'error' || $type === 'danger'): ?>
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                <?php elseif ($type === 'warning'): ?>
                    <i class="bi bi-exclamation-circle-fill me-2"></i>
                <?php elseif ($type === 'info'): ?>
                    <i class="bi bi-info-circle-fill me-2"></i>
                <?php endif; ?>
                
                <div>
                    <?php 
                    // Tampilkan pesan - escape output untuk keamanan
                    echo is_array($_SESSION[$type]) ? implode('<br>', array_map('htmlspecialchars', $_SESSION[$type])) : htmlspecialchars($_SESSION[$type]); 
                    ?>
                </div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        
        <?php 
        // Hapus pesan dari session setelah ditampilkan
        unset($_SESSION[$type]); 
        ?>
    <?php endif; ?>
<?php endforeach; ?>

<script>
// Auto close alert setelah 5 detik
document.addEventListener('DOMContentLoaded', function() {
    const alerts = document.querySelectorAll('.alert');
    
    alerts.forEach(alert => {
        setTimeout(() => {
            const bsAlert = new bootstrap.Alert(alert);
            bsAlert.close();
        }, 5000);
    });
});
</script>