<?php
// includes/flash.php — mensajes de sesión tipo toast (multi-mensaje)
function flashSet(string $tipo, string $msg): void {
    startSession();
    if (!isset($_SESSION['flash'])) $_SESSION['flash'] = [];
    $_SESSION['flash'][] = ['tipo' => $tipo, 'msg' => $msg];
}
function flashGet(): array {
    startSession();
    $f = $_SESSION['flash'] ?? [];
    unset($_SESSION['flash']);
    return $f;
}
function flashHtml(): string {
    $flashes = flashGet();
    if (!$flashes) return '';
    $html = '';
    foreach ($flashes as $idx => $f) {
        $id  = 'flash-msg-' . $idx;
        $cls = match($f['tipo']) {
            'ok'   => 'alert-dark-success',
            'warn' => 'alert-dark-warn',
            'err'  => 'alert-dark-danger',
            default=> 'alert-dark-info',
        };
        $ico = match($f['tipo']) {
            'ok'  => 'circle-check',
            'err' => 'circle-exclamation',
            'warn'=> 'triangle-exclamation',
            default=> 'info-circle'
        };
        // Permitir HTML controlado en el mensaje (ya viene sanitizado desde PHP)
        $msg = $f['msg'];
        $html .= "<div class=\"{$cls} p-3 mb-2\" id=\"{$id}\">
                    <i class=\"fa fa-{$ico} me-2\"></i>{$msg}
                    <button onclick=\"document.getElementById(\'{$id}\').remove()\"
                            style=\"float:right;background:none;border:none;color:inherit;cursor:pointer;\">✕</button>
                  </div>";
    }
    return $html;
}