<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <div class="d-flex flex-column flex-xl-row justify-content-between gap-3 align-items-xl-center mb-4">
                <div>
                    <h1 class="h3 fw-bold text-cfc-dark mb-1">Backup / Restaurar</h1>
                    <p class="text-muted mb-0">Cópia de segurança do banco e dos arquivos enviados.</p>
                </div>
                <div class="d-flex flex-wrap gap-2">
                    <form method="post" action="<?= e(url('admin/backups/create')) ?>">
                        <?= CSRF::input() ?>
                        <button class="btn btn-primary rounded-pill px-4" <?= empty($archiveEnabled) ? 'disabled' : '' ?>>
                            <i class="bi bi-cloud-arrow-down"></i> Gerar backup completo
                        </button>
                    </form>
                    <?php if(!empty($sqliteDownloadAvailable)): ?>
                        <form method="post" action="<?= e(url('admin/backups/database')) ?>">
                            <?= CSRF::input() ?>
                            <button class="btn btn-outline-primary rounded-pill px-4" title="Baixa somente o banco, sem depender da pasta de backups">
                                <i class="bi bi-database-down"></i> Baixar banco agora
                            </button>
                        </form>
                    <?php endif; ?>
                </div>
            </div>

            <div class="alert alert-info rounded-4">
                <strong>Modo de backup disponível:</strong> <?= e($archiveMode ?? 'desconhecido') ?>.
                O backup completo tenta ZIP, TAR.GZ e, no modo SQLite, uma cópia do banco como último recurso.
                <?php if(!empty($sqliteDownloadAvailable)): ?>O botão “Baixar banco agora” funciona sem precisar gravar na pasta de backups.<?php endif; ?>
            </div>

            <div class="row g-4">
                <div class="col-xl-7">
                    <div class="card border-0 shadow-soft rounded-4">
                        <div class="card-body p-4">
                            <h2 class="h5 fw-bold mb-3">Backups salvos</h2>
                            <div class="table-responsive">
                                <table class="table align-middle">
                                    <thead><tr><th>Arquivo</th><th>Tamanho</th><th>Criado em</th><th></th></tr></thead>
                                    <tbody>
                                    <?php foreach($backups as $b): ?>
                                        <tr>
                                            <td><strong><?= e($b['name']) ?></strong></td>
                                            <td><?= e(BackupManager::humanSize((int)$b['size'])) ?></td>
                                            <td><?= e(date('d/m/Y H:i', strtotime($b['created_at']))) ?></td>
                                            <td class="text-end">
                                                <div class="d-inline-flex gap-2 flex-wrap">
                                                    <a class="btn btn-sm btn-success" href="<?= e(url('admin/backups/download', ['file' => $b['name']])) ?>"><i class="bi bi-download"></i> Baixar</a>
                                                    <form method="post" action="<?= e(url('admin/backups/restore-saved')) ?>" onsubmit="const c=prompt('Digite RESTAURAR para confirmar:');if(c!=='RESTAURAR')return false;this.querySelector('[name=confirm_restore_saved]').value=c;return confirm('Restaurar este backup?');">
                                                        <?= CSRF::input() ?>
                                                        <input type="hidden" name="file" value="<?= e($b['name']) ?>">
                                                        <input type="hidden" name="confirm_restore_saved">
                                                        <button class="btn btn-sm btn-warning">Restaurar</button>
                                                    </form>
                                                    <form method="post" action="<?= e(url('admin/backups/delete')) ?>" onsubmit="return confirm('Apagar este backup?');">
                                                        <?= CSRF::input() ?>
                                                        <input type="hidden" name="file" value="<?= e($b['name']) ?>">
                                                        <button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                    <?php if(empty($backups)): ?>
                                        <tr><td colspan="4" class="text-center text-muted py-4">Nenhum backup criado.</td></tr>
                                    <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-xl-5">
                    <div class="card border-0 shadow-soft rounded-4">
                        <div class="card-body p-4">
                            <h2 class="h5 fw-bold mb-3">Restaurar arquivo</h2>
                            <form method="post" action="<?= e(url('admin/backups/restore')) ?>" enctype="multipart/form-data" onsubmit="return confirm('Restaurar este backup?');">
                                <?= CSRF::input() ?>
                                <div class="mb-3">
                                    <label class="form-label">Arquivo (.zip, .tar.gz, .sqlite ou .db)</label>
                                    <input type="file" name="backup_file" class="form-control" accept=".zip,.tar.gz,.tgz,.sqlite,.db" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Confirmação</label>
                                    <input name="confirm_restore" class="form-control" placeholder="RESTAURAR" required>
                                </div>
                                <button class="btn btn-danger rounded-pill px-4">Restaurar backup</button>
                            </form>
                            <hr>
                            <small class="text-muted">Limites atuais: upload_max_filesize <?= e(ini_get('upload_max_filesize')) ?> · post_max_size <?= e(ini_get('post_max_size')) ?> · max_execution_time <?= e(ini_get('max_execution_time')) ?>s.</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
