<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-tools me-2"></i>Maintenance
        </h1>
        <p class="text-muted mb-0">
            Outils d’administration, sauvegarde et contrôle du système.
        </p>
    </div>

    <a href="index.php?page=dashboard" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i> Retour tableau de bord
    </a>
</div>
<div class="darlink-card overflow-hidden mb-4">
    <div
        class="p-4 text-white"
        style="background:linear-gradient(135deg, var(--darlink-primary), #1565A8);"
    >
        <div class="row align-items-center g-4">
            <div class="col-lg-7">
                <div class="small text-uppercase opacity-75 mb-2">
                    Centre d’administration
                </div>

                <h2 class="h3 fw-bold mb-2">
                    <?= htmlspecialchars(\App\Core\Version::fullLabel()) ?>
                </h2>

                <div class="d-flex align-items-center gap-3 flex-wrap">
                    <span class="badge bg-light text-dark px-3 py-2">
                        <?= htmlspecialchars($summary['health_label']) ?>
                    </span>

                    <span>
                        <?= (int)$summary['total'] ?> contrôle(s) exécuté(s)
                    </span>
                </div>
            </div>

            <div class="col-lg-5 text-lg-end">
                <div class="display-4 fw-bold">
                    <?= (int)$summary['score'] ?> %
                </div>

                <div class="opacity-75">
                    Score de santé
                </div>

                <div class="mt-2 small">
                    Dernière sauvegarde :
                    <strong>
                        <?= !empty($databaseInformation['latest_backup'])
                            ? htmlspecialchars($databaseInformation['latest_backup'])
                            : 'Aucune' ?>
                    </strong>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row g-3 mb-4">
    <div class="col-md-6 col-xl">
        <div class="darlink-card h-100 p-4">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                Total
            </div>
            <div class="d-flex align-items-center justify-content-between">
                <strong class="h3 mb-0"><?= (int)($backupMetrics['total_count'] ?? 0) ?></strong>
                <i class="bi bi-archive fs-2 text-primary"></i>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl">
        <div class="darlink-card h-100 p-4">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                Sauvegardes SQL
            </div>
            <div class="d-flex align-items-center justify-content-between">
                <strong class="h3 mb-0"><?= (int)($backupMetrics['sql_count'] ?? 0) ?></strong>
                <i class="bi bi-database fs-2 text-primary"></i>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl">
        <div class="darlink-card h-100 p-4">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                Sauvegardes complètes
            </div>
            <div class="d-flex align-items-center justify-content-between">
                <strong class="h3 mb-0"><?= (int)($backupMetrics['complete_count'] ?? 0) ?></strong>
                <i class="bi bi-file-earmark-zip fs-2 text-success"></i>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl">
        <div class="darlink-card h-100 p-4">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                Espace utilisé
            </div>
            <div class="d-flex align-items-center justify-content-between">
                <strong class="h5 mb-0">
                    <?= number_format(
                        (float)($backupMetrics['total_size'] ?? 0) / 1024 / 1024,
                        2,
                        ',',
                        ' '
                    ) ?> Mo
                </strong>
                <i class="bi bi-hdd fs-2 text-secondary"></i>
            </div>
        </div>
    </div>

    <div class="col-md-12 col-xl">
        <div class="darlink-card h-100 p-4">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                État général
            </div>
            <div class="d-flex align-items-center justify-content-between gap-3">
                <div>
                    <span class="darlink-badge darlink-badge-<?= htmlspecialchars(
                        (string)($backupStatus['status'] ?? 'danger')
                    ) ?>">
                        <?= htmlspecialchars(
                            (string)($backupStatus['label'] ?? 'Indisponible')
                        ) ?>
                    </span>
                    <div class="small text-muted mt-2">
                        <?= htmlspecialchars(
                            (string)($backupStatus['message'] ?? '')
                        ) ?>
                    </div>
                </div>
                <i class="bi bi-shield-check fs-2 text-<?= htmlspecialchars(
                    (string)($backupStatus['status'] ?? 'danger')
                ) ?>"></i>
            </div>
        </div>
    </div>
</div>

<?php $latestValidBackup = $backupStatus['latest'] ?? null; ?>
<div class="darlink-card overflow-hidden mb-4">
    <div class="darlink-admin-block-header">
        <i class="bi bi-clock-history me-2"></i>Dernière sauvegarde valide
    </div>
    <div class="p-4 d-flex flex-wrap align-items-center justify-content-between gap-3">
        <div>
            <div class="h5 mb-1">
                <?= $latestValidBackup
                    ? htmlspecialchars((string)($latestValidBackup['created_at'] ?? '-'))
                    : 'Aucune sauvegarde valide' ?>
            </div>
            <div class="text-muted">
                <?= $latestValidBackup
                    ? htmlspecialchars((string)($latestValidBackup['file_name'] ?? ''))
                    : 'Créez une sauvegarde complète pour sécuriser les données.' ?>
            </div>
        </div>

        <?php if ($latestValidBackup): ?>
            <span class="darlink-badge darlink-badge-success">
                <i class="bi bi-check-circle me-1"></i>
                Conforme
            </span>
        <?php endif; ?>
    </div>
</div>

<div class="darlink-card overflow-hidden mb-4">
    <div class="darlink-admin-block-header">
        <i class="bi bi-wrench-adjustable-circle me-2"></i>Outils de maintenance
    </div>

    <div class="p-4">
        <div class="row g-3">
            <?php foreach ($cards as $card): ?>
                <?php
                $isDisabled = ($card['url'] ?? '#') === '#';
                ?>

                <div class="col-md-6 col-xl-4">
                    <?php if ($isDisabled): ?>
                        <div class="darlink-settings-tile disabled h-100">
                            <i class="bi <?= htmlspecialchars($card['icon']) ?>"></i>

                            <span>
                                <strong><?= htmlspecialchars($card['title']) ?></strong>
                                <small><?= htmlspecialchars($card['text']) ?></small>
                            </span>

                            <i class="bi bi-lock darlink-settings-arrow"></i>
                        </div>
<?php else: ?>
    <?php if (($card['method'] ?? 'get') === 'post'): ?>
        <form
            method="post"
            action="<?= htmlspecialchars($card['url']) ?>"
            class="h-100"
            onsubmit="return confirm('<?= htmlspecialchars($card['confirmation'] ?? 'Confirmer cette opération ?', ENT_QUOTES, 'UTF-8') ?>');"
        >
            <button
                type="submit"
                class="darlink-settings-tile h-100 w-100 text-start"
                style="border:1px solid var(--darlink-border);"
            >
                <i class="bi <?= htmlspecialchars($card['icon']) ?>"></i>

                <span>
                    <strong><?= htmlspecialchars($card['title']) ?></strong>
                    <small><?= htmlspecialchars($card['text']) ?></small>
                </span>

                <i class="bi bi-chevron-right darlink-settings-arrow"></i>
            </button>
        </form>
    <?php else: ?>
        <a
            href="<?= htmlspecialchars($card['url']) ?>"
            class="darlink-settings-tile h-100"
        >
            <i class="bi <?= htmlspecialchars($card['icon']) ?>"></i>

            <span>
                <strong><?= htmlspecialchars($card['title']) ?></strong>
                <small><?= htmlspecialchars($card['text']) ?></small>
            </span>

            <i class="bi bi-chevron-right darlink-settings-arrow"></i>
        </a>
    <?php endif; ?>
<?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<div class="darlink-card overflow-hidden mb-4" id="gestionnaire-sauvegardes">
    <div class="darlink-admin-block-header">
        <i class="bi bi-database-check me-2"></i>Gestionnaire de sauvegardes
    </div>

    <div class="p-4">
        <?php if (empty($backups)): ?>
            <div class="text-center text-muted py-4">
                <i class="bi bi-database-x fs-2 d-block mb-2"></i>
                Aucune sauvegarde disponible.
            </div>
        <?php else: ?>
            <?php
            $latestBackup = $backups[0];
            $totalBackupSize = array_sum(
                array_column($backups, 'file_size')
            );
            ?>

            <div class="row g-3 mb-4">
                <div class="col-md-4">
                    <div class="darlink-stat h-100">
                        <span>Nombre de sauvegardes</span>
                        <strong><?= count($backups) ?></strong>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="darlink-stat h-100">
                        <span>Dernière sauvegarde</span>
                        <strong class="fs-6">
                            <?= htmlspecialchars(
                                $latestBackup['created_at'] ?? '-'
                            ) ?>
                        </strong>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="darlink-stat h-100">
                        <span>Espace utilisé</span>
                        <strong>
                            <?= number_format(
                                $totalBackupSize / 1024 / 1024,
                                2,
                                ',',
                                ' '
                            ) ?> Mo
                        </strong>
                    </div>
                </div>
            </div>

            <div class="table-responsive">
                <table class="table align-middle darlink-table darlink-table-modern">
                    <thead>
						<tr>
							<th>Type</th>
							<th>Fichier</th>
							<th>Version</th>
							<th>Utilisateur</th>
							<th>Date</th>
                            <th>État</th>
							<th class="text-end">Taille</th>
							<th class="text-end">Actions</th>
						</tr>
                    </thead>

                    <tbody>
                    <?php foreach ($backups as $backup): ?>
                        <?php
                        $backupType = (string)($backup['type'] ?? 'database');
                        $isCompleteBackup = $backupType === 'complete';
                        ?>

                        <tr>
                            <td>
                                <?php if ($isCompleteBackup): ?>
                                    <span class="darlink-badge darlink-badge-success">
                                        <i class="bi bi-file-earmark-zip me-1"></i>
                                        Complète
                                    </span>
                                <?php else: ?>
                                    <span class="darlink-badge darlink-badge-primary">
                                        <i class="bi bi-database me-1"></i>
                                        SQL
                                    </span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <i class="bi <?= $isCompleteBackup
                                    ? 'bi-file-earmark-zip text-success'
                                    : 'bi-filetype-sql text-primary' ?> me-2"></i>

                                <?= htmlspecialchars(
                                    (string)($backup['file_name'] ?? '')
                                ) ?>
                                <div class="small mt-1 <?= ($backup['integrity'] ?? '') === 'valid' ? 'text-success' : 'text-danger' ?>">
                                    <i class="bi <?= ($backup['integrity'] ?? '') === 'valid' ? 'bi-check-circle' : 'bi-exclamation-triangle' ?> me-1"></i>
                                    <?= htmlspecialchars((string)($backup['integrity_message'] ?? '')) ?>
                                </div>
                            </td>

                            <td>
                                <span class="darlink-badge <?= ($backup['version_state'] ?? '') === 'current'
                                    ? 'darlink-badge-primary'
                                    : 'darlink-badge-light' ?>">
                                    <?= htmlspecialchars(
                                        (string)($backup['version'] ?? 'Historique')
                                    ) ?>
                                </span>
                            </td>

                            <td>
                                <?= htmlspecialchars(
                                    (string)($backup['created_by'] ?? '')
                                ) ?: '—' ?>
                            </td>

                            <td>
                                <?= htmlspecialchars(
                                    (string)($backup['created_at'] ?? '-')
                                ) ?>
                            </td>

                            <td>
                                <?php if (($backup['integrity'] ?? '') !== 'valid'): ?>
                                    <span class="darlink-badge darlink-badge-danger">
                                        <i class="bi bi-x-circle me-1"></i>
                                        Invalide
                                    </span>
                                <?php elseif (($backup['version_state'] ?? '') === 'current'): ?>
                                    <span class="darlink-badge darlink-badge-success">
                                        <i class="bi bi-check-circle me-1"></i>
                                        Conforme
                                    </span>
                                <?php else: ?>
                                    <span class="darlink-badge darlink-badge-warning">
                                        <i class="bi bi-clock-history me-1"></i>
                                        Ancienne version
                                    </span>
                                <?php endif; ?>
                            </td>

                            <td class="text-end">
                                <?= number_format(
                                    (float)($backup['file_size'] ?? 0)
                                    / 1024
                                    / 1024,
                                    2,
                                    ',',
                                    ' '
                                ) ?> Mo
                            </td>

                            <td class="text-end darlink-actions">
                                <?php $isValidBackup = ($backup['integrity'] ?? '') === 'valid'; ?>

                                <a
                                    href="index.php?page=maintenance_backup_download&file=<?= urlencode(
                                        (string)($backup['file_name'] ?? '')
                                    ) ?>"
                                    class="darlink-icon-btn darlink-icon-btn-view"
                                    title="Télécharger"
                                >
                                    <i class="bi bi-download"></i>
                                </a>

                                <button
                                    type="button"
                                    class="darlink-icon-btn <?= $isValidBackup ? 'darlink-icon-btn-order' : '' ?>"
                                    title="<?= $isValidBackup
                                        ? 'Restaurer cette sauvegarde'
                                        : htmlspecialchars(
                                            (string)($backup['integrity_message'] ?? 'Sauvegarde invalide'),
                                            ENT_QUOTES,
                                            'UTF-8'
                                        ) ?>"
                                    <?= $isValidBackup ? '' : 'disabled' ?>
                                    data-bs-toggle="<?= $isValidBackup ? 'modal' : '' ?>"
                                    data-bs-target="<?= $isValidBackup ? '#restoreBackupModal' : '' ?>"
                                    data-backup-file="<?= htmlspecialchars(
                                        (string)($backup['file_name'] ?? ''),
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>"
                                    data-backup-type="<?= htmlspecialchars(
                                        $isCompleteBackup ? 'Complète' : 'SQL',
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>"
                                >
                                    <i class="bi <?= $isValidBackup
                                        ? 'bi-arrow-counterclockwise'
                                        : 'bi-exclamation-triangle' ?>"></i>
                                </button>

                                <form
                                    method="post"
                                    action="index.php?page=maintenance_backup_delete"
                                    class="d-inline"
                                    onsubmit="return confirm('Supprimer définitivement cette sauvegarde ?');"
                                >
                                    <input
                                        type="hidden"
                                        name="file"
                                        value="<?= htmlspecialchars(
                                            (string)($backup['file_name'] ?? ''),
                                            ENT_QUOTES,
                                            'UTF-8'
                                        ) ?>"
                                    >

                                    <button
                                        type="submit"
                                        class="darlink-icon-btn darlink-icon-btn-delete"
                                        title="Supprimer"
                                    >
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="darlink-card overflow-hidden mb-4">
    <div class="darlink-admin-block-header">
        <i class="bi bi-activity me-2"></i>État du système
    </div>
<div class="row g-4 mb-4">
    <div class="col-xl-6">
        <div class="darlink-card overflow-hidden h-100">
            <div class="darlink-admin-block-header">
                <i class="bi bi-pc-display me-2"></i>
                Informations système
            </div>

            <div class="darlink-card-body">
                <div class="darlink-information-list">
                    <div>
                        <span>Application</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['application']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Version</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['version']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>PHP</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['php_version']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>MySQL / MariaDB</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['mysql_version']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Serveur</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['server']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Système</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['operating_system']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Fuseau horaire</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['timezone']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Mémoire PHP</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['memory_limit']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Upload maximal</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['upload_max_filesize']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>POST maximal</span>
                        <strong>
                            <?= htmlspecialchars($systemInformation['post_max_size']) ?>
                        </strong>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-6">
        <div class="darlink-card overflow-hidden h-100">
            <div class="darlink-admin-block-header">
                <i class="bi bi-database me-2"></i>
                Base de données
            </div>

            <div class="darlink-card-body">
                <div class="darlink-information-list">
                    <div>
                        <span>Nom de la base</span>
                        <strong>
                            <?= htmlspecialchars($databaseInformation['name']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Nombre de tables</span>
                        <strong>
                            <?= (int)$databaseInformation['table_count'] ?>
                        </strong>
                    </div>

                    <div>
                        <span>Encodage</span>
                        <strong>
                            <?= htmlspecialchars($databaseInformation['charset']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Collation</span>
                        <strong>
                            <?= htmlspecialchars($databaseInformation['collation']) ?>
                        </strong>
                    </div>

                    <div>
                        <span>Sauvegardes</span>
                        <strong>
                            <?= (int)$databaseInformation['backup_count'] ?>
                        </strong>
                    </div>

                    <div>
                        <span>Dernière sauvegarde</span>
                        <strong>
                            <?= !empty($databaseInformation['latest_backup'])
                                ? htmlspecialchars($databaseInformation['latest_backup'])
                                : 'Aucune' ?>
                        </strong>
                    </div>

                    <div>
                        <span>Dernier fichier</span>
                        <strong>
                            <?= !empty($databaseInformation['latest_backup_file'])
                                ? htmlspecialchars($databaseInformation['latest_backup_file'])
                                : 'Aucun' ?>
                        </strong>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    <div class="p-4">
		<div class="row g-3 mb-4">
    <div class="col-lg-8">
        <div class="darlink-card p-4 h-100">
            <div class="text-muted small text-uppercase fw-bold mb-2">
                Diagnostic Phoenix
            </div>

            <div class="d-flex align-items-center gap-4 flex-wrap">
                <div class="display-4 fw-bold text-<?= htmlspecialchars($summary['health_class']) ?>">
                    <?= (int)$summary['score'] ?> %
                </div>

                <div>
                    <div class="h5 mb-1">
                        <?= htmlspecialchars($summary['health_label']) ?>
                    </div>

                    <div class="text-muted">
                        <?= (int)$summary['total'] ?> contrôle(s) exécuté(s)
                    </div>
                </div>
            </div>

            <div class="progress mt-4" style="height:12px;">
                <div
                    class="progress-bar bg-<?= htmlspecialchars($summary['health_class']) ?>"
                    role="progressbar"
                    style="width:<?= (int)$summary['score'] ?>%;"
                    aria-valuenow="<?= (int)$summary['score'] ?>"
                    aria-valuemin="0"
                    aria-valuemax="100">
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="darlink-card p-4 h-100">
            <div class="text-muted small text-uppercase fw-bold mb-3">
                État global
            </div>

            <span class="darlink-badge darlink-badge-<?= htmlspecialchars($summary['health_class']) ?>">
                <?= htmlspecialchars($summary['health_label']) ?>
            </span>

            <div class="mt-3 small text-muted">
                Le score tient compte des contrôles réussis, des avertissements et des erreurs.
            </div>
        </div>
    </div>
</div>
		<div class="row g-3">
 			<div class="col-md-4">
                <div class="darlink-stat h-100">
                    <span>Contrôles OK</span>
                    <strong class="text-success"><?= (int)$summary['ok'] ?></strong>
                </div>
            </div>

            <div class="col-md-4">
                <div class="darlink-stat h-100">
                    <span>Avertissements</span>
                    <strong class="text-warning"><?= (int)$summary['warning'] ?></strong>
                </div>
            </div>

            <div class="col-md-4">
                <div class="darlink-stat h-100">
                    <span>Erreurs</span>
                    <strong class="text-danger"><?= (int)$summary['error'] ?></strong>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="darlink-card darlink-table-card">
    <div class="darlink-table-toolbar">
        <div class="darlink-table-toolbar-left">
            <strong>
                <i class="bi bi-clipboard-check me-1"></i>
                Résultat des vérifications
            </strong>
        </div>

        <div class="darlink-table-toolbar-right">
            <a href="index.php?page=maintenance"
               class="btn btn-sm btn-outline-primary">

                <i class="bi bi-arrow-clockwise me-1"></i>
                Actualiser
            </a>
        </div>
    </div>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern">
            <thead>
            <tr>
                <th style="width:130px;">Statut</th>
                <th>Contrôle</th>
                <th>Élément</th>
                <th>Détail</th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($checks)): ?>
                <tr>
                    <td colspan="4" class="text-center text-muted py-5">
                        Aucun contrôle disponible.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($checks as $check): ?>
                <?php
                $badgeClass = match ($check['status']) {
                    'ok' => 'darlink-badge-success',
                    'warning' => 'darlink-badge-warning',
                    default => 'darlink-badge-danger',
                };

                $label = match ($check['status']) {
                    'ok' => 'OK',
                    'warning' => 'Attention',
                    default => 'Erreur',
                };
                ?>

                <tr>
                    <td>
                        <span class="darlink-badge <?= $badgeClass ?>">
                            <?= $label ?>
                        </span>
                    </td>

                    <td><?= htmlspecialchars($check['label']) ?></td>

                    <td>
                        <code><?= htmlspecialchars($check['value']) ?></code>
                    </td>

                    <td><?= htmlspecialchars($check['message']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="alert alert-info mt-4 mb-0">
    <i class="bi bi-info-circle me-1"></i>
    Les contrôles permettent de vérifier la configuration générale de Darlink ERP avant la mise en production.
</div>
<div
    class="modal fade"
    id="restoreBackupModal"
    tabindex="-1"
    aria-labelledby="restoreBackupModalLabel"
    aria-hidden="true"
>
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <form
                method="post"
                action="index.php?page=maintenance_backup_restore"
            >
                <div class="modal-header">
                    <h2
                        class="modal-title fs-5"
                        id="restoreBackupModalLabel"
                    >
                        <i class="bi bi-exclamation-triangle text-warning me-2"></i>
                        Restaurer une sauvegarde
                    </h2>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="modal"
                        aria-label="Fermer"
                    ></button>
                </div>

                <div class="modal-body">
                    <div class="alert alert-warning">
                        La restauration remplacera les données actuelles de
                        Darlink ERP par celles contenues dans la sauvegarde.
                    </div>

                    <p class="mb-2">
                        Fichier sélectionné :
                    </p>

                    <div class="fw-bold" id="restoreBackupFileLabel">-</div>
                    <div class="small text-muted mb-4" id="restoreBackupTypeLabel"></div>

                    <input
                        type="hidden"
                        name="file"
                        id="restoreBackupFile"
                        value=""
                    >

                    <label
                        for="restoreBackupConfirmation"
                        class="darlink-label"
                    >
                        Pour confirmer, saisissez exactement
                        <strong>RESTAURER</strong>
                    </label>

                    <input
                        type="text"
                        name="confirmation"
                        id="restoreBackupConfirmation"
                        class="darlink-input"
                        autocomplete="off"
                        required
                    >

                    <div class="form-text">
                        Un point de sécurité SQL sera créé automatiquement avant la restauration.
                    </div>
                </div>

                <div class="modal-footer">
                    <button
                        type="button"
                        class="btn btn-outline-secondary"
                        data-bs-dismiss="modal"
                    >
                        Annuler
                    </button>

                    <button
                        type="submit"
                        class="btn btn-warning"
                    >
                        <i class="bi bi-arrow-counterclockwise me-1"></i>
                        Restaurer la sauvegarde
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const modal = document.getElementById('restoreBackupModal');

    if (!modal) {
        return;
    }

    modal.addEventListener('show.bs.modal', function (event) {
        const button = event.relatedTarget;
        const fileName = button
            ? button.getAttribute('data-backup-file')
            : '';
        const backupType = button
            ? button.getAttribute('data-backup-type')
            : '';

        const fileInput = document.getElementById('restoreBackupFile');
        const fileLabel = document.getElementById(
            'restoreBackupFileLabel'
        );
        const confirmationInput = document.getElementById(
            'restoreBackupConfirmation'
        );
        const typeLabel = document.getElementById('restoreBackupTypeLabel');

        if (fileInput) {
            fileInput.value = fileName || '';
        }

        if (fileLabel) {
            fileLabel.textContent = fileName || '-';
        }

        if (typeLabel) {
            typeLabel.textContent = backupType ? 'Type : ' + backupType : '';
        }

        if (confirmationInput) {
            confirmationInput.value = '';
        }
    });
});
</script>