<?php
/**
 * View: admin/backup
 *
 * Injected variables:
 *   @var string|null $error
 *   @var string|null $success
 *   @var string|null $warning
 *   @var array       $backups
 *   @var int         $securiteSize
 *   @var int         $securiteFiles
 *   @var string      $csrfToken
 *
 * Available from bootstrap (via extract in View::display):
 *   $auth, $settings, $moduleRegistry, $currentUser
 *
 * Note: fmtSize() is defined in the controller (global function scope).
 */

$pageTitle = t('backup.title');
include WEBROOT_PATH . '/includes/header.php';
?>

<div class="admin-layout">
<?php include WEBROOT_PATH . '/includes/admin_sidebar.php'; ?>
<div class="admin-content">

<div class="page-header">
    <h1>💾 <?= t('backup.title') ?></h1>
</div>

<?php if ($error): ?>
<div class="alert alert-error" style="white-space:pre-line"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success" style="white-space:pre-line"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<?php if ($warning): ?>
<div class="alert alert-warning" style="white-space:pre-line"><?= htmlspecialchars($warning) ?></div>
<?php endif; ?>

<!-- Overview -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem"><?= t('backup.overview') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .5rem"><?= t('backup.overview_hint') ?></p>
    <div style="display:flex;flex-wrap:wrap;gap:.5rem .9rem;font-size:.88rem">
        <span><?= t('backup.files_count') ?> : <strong><?= $securiteFiles ?></strong></span>
        <span><?= t('backup.total_size') ?> : <strong><?= fmtSize($securiteSize) ?></strong></span>
    </div>
</div>

<!-- Backup actions -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem">📦 <?= t('backup.create_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .75rem"><?= t('backup.create_hint') ?></p>
    <form method="post">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
        <label style="display:block;font-size:.85rem;margin-bottom:.35rem">
            <input type="checkbox" name="include_key" value="1"> <?= t('backup.include_key') ?>
        </label>
        <p style="font-size:.78rem;color:var(--color-text-muted);margin:0 0 .6rem"><?= t('backup.include_key_warn') ?></p>
        <div style="display:flex;gap:.5rem;flex-wrap:wrap">
            <button type="submit" name="action" value="backup_local" class="btn btn-primary">💾 <?= t('backup.save_local') ?></button>
            <button type="submit" name="action" value="backup_download" class="btn btn-secondary">⬇ <?= t('backup.download') ?></button>
        </div>
    </form>
</div>

<!-- Restore -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem">🔄 <?= t('backup.restore_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .75rem"><?= t('backup.restore_hint') ?></p>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
        <input type="hidden" name="action" value="restore">
        <div class="form-group">
            <label for="restore_zip"><?= t('backup.restore_label') ?></label>
            <input type="file" id="restore_zip" name="restore_zip" accept=".zip" required>
        </div>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary"
                    onclick="return confirm('<?= htmlspecialchars(t('backup.restore_confirm')) ?>')">
                🔄 <?= t('backup.restore_apply') ?>
            </button>
        </div>
    </form>
</div>

<!-- Local backups list -->
<div class="card">
    <h2 style="font-size:1rem;margin:0 0 .5rem">📋 <?= t('backup.local_list') ?></h2>
    <?php if (empty($backups)): ?>
    <p style="color:var(--color-text-muted);font-size:.85rem;margin:0"><?= t('backup.no_backups') ?></p>
    <?php else: ?>
    <table class="data-table" style="font-size:.85rem">
        <thead>
            <tr>
                <th><?= t('backup.col_name') ?></th>
                <th style="text-align:right"><?= t('backup.col_size') ?></th>
                <th><?= t('backup.col_date') ?></th>
                <th><?= t('user.actions') ?></th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($backups as $bk): ?>
            <tr>
                <td><code style="font-size:.8rem"><?= htmlspecialchars($bk['name']) ?></code></td>
                <td style="text-align:right"><?= fmtSize($bk['size']) ?></td>
                <td><?= htmlspecialchars($bk['date']) ?></td>
                <td style="white-space:nowrap">
                    <form method="post" style="display:inline">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="action" value="restore">
                        <input type="hidden" name="restore_from_local" value="<?= htmlspecialchars($bk['name']) ?>">
                        <button type="submit" class="btn btn-secondary btn-sm"
                                onclick="return confirm('<?= htmlspecialchars(t('backup.restore_confirm')) ?>')"
                                title="<?= t('backup.restore_this') ?>">
                            🔄
                        </button>
                    </form>
                    <form method="post" style="display:inline"
                          onsubmit="return confirm('<?= htmlspecialchars(sprintf(t('backup.delete_confirm'), $bk['name'])) ?>')">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="action" value="delete_backup">
                        <input type="hidden" name="file" value="<?= htmlspecialchars($bk['name']) ?>">
                        <button type="submit" class="btn btn-danger btn-sm"><?= t('user.delete') ?></button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>

</div>
</div>

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
