<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>Backup database<?= $this->endSection() ?>

<?php
$files = is_array($payload) && isset($payload['files']) && is_array($payload['files']) ? $payload['files'] : [];
$dir   = is_array($payload) ? (string) ($payload['directory'] ?? '') : '';
?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div>
        <h1 class="text-2xl font-semibold text-gray-900">Backup database</h1>
        <p class="mt-1 text-sm text-gray-500">File cadangan basis data disimpan di folder aplikasi (writable). Unduh atau hapus dari daftar di bawah.</p>
        <?php if ($dir !== '') : ?>
            <p class="mt-1 font-mono text-xs text-gray-400 break-all"><?= esc($dir) ?></p>
        <?php endif ?>
    </div>

    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>

    <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
        <h2 class="text-base font-semibold text-gray-900">Buat backup baru</h2>
        <p class="mt-1 text-xs text-gray-500">Cadangan basis data dalam format SQL. Proses bisa memakan waktu beberapa detik.</p>
        <form method="post" action="<?= site_url('admin/util/backup/run') ?>" class="mt-4 flex flex-wrap items-center gap-4">
            <?= csrf_field() ?>
            <label class="inline-flex items-center gap-2 text-sm text-gray-700">
                <input type="checkbox" name="save_latest" value="1" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                Juga tulis salinan <code class="rounded bg-gray-100 px-1 text-xs">latest.sql</code>
            </label>
            <button type="submit" class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-700" onclick="return confirm('Jalankan backup sekarang?');">
                <i class="fa-solid fa-database mr-2"></i> Jalankan backup
            </button>
        </form>
    </section>

    <section class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
        <div class="border-b border-gray-200 px-5 py-4">
            <h2 class="text-base font-semibold text-gray-900">File backup</h2>
            <p class="text-xs text-gray-500">Unduh untuk arsip; hapus jika sudah tidak diperlukan.</p>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200 text-sm">
                <thead class="border-y border-gray-200 bg-gray-50 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">
                    <tr>
                        <th class="px-4 py-3">Nama file</th>
                        <th class="px-4 py-3">Ukuran</th>
                        <th class="px-4 py-3">Diubah</th>
                        <th class="px-4 py-3 w-48">Tindakan</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php if ($files === []) : ?>
                        <tr>
                            <td colspan="4" class="px-4 py-10 text-center text-gray-500">Belum ada file backup.</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($files as $f) : ?>
                            <?php
                            $name = (string) ($f['name'] ?? '');
                            if ($name === '') {
                                continue;
                            }
                            $enc = rawurlencode($name);
                            ?>
                            <tr class="hover:bg-gray-50/80">
                                <td class="px-4 py-2 font-mono text-xs text-gray-900"><?= esc($name) ?></td>
                                <td class="whitespace-nowrap px-4 py-2 text-gray-600"><?= number_format((int) ($f['size'] ?? 0), 0, ',', '.') ?> B</td>
                                <td class="whitespace-nowrap px-4 py-2 text-gray-600"><?= esc((string) ($f['mtime'] ?? '')) ?></td>
                                <td class="px-4 py-2">
                                    <div class="flex flex-wrap gap-2">
                                        <a href="<?= site_url('admin/util/backup/download/' . $enc) ?>" class="inline-flex rounded-lg border border-gray-200 bg-white px-2 py-1 text-xs font-semibold text-gray-800 shadow-sm hover:bg-gray-50">Unduh</a>
                                        <form method="post" action="<?= site_url('admin/util/backup/delete/' . $enc) ?>" class="inline" onsubmit="return confirm('Hapus file backup ini?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="inline-flex rounded-lg bg-red-50 px-2 py-1 text-xs font-semibold text-red-800 hover:bg-red-100">Hapus</button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    <?php endif ?>
                </tbody>
            </table>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
