
<?php
session_start(); require __DIR__ . '/includes/header.php';
$backupDir = __DIR__ . '/../data';
$downloadFile = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT && ($_SESSION['role'] ?? '') === 'admin') {
  $tmp = sys_get_temp_dir() . '/oldies-data-backup-' . date('Ymd-His') . '.zip';
  $zip = new ZipArchive();
  if ($zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($backupDir, FilesystemIterator::SKIP_DOTS));
    foreach ($it as $file) { if ($file->isFile()) $zip->addFile($file->getRealPath(), 'data/' . substr($file->getRealPath(), strlen(realpath($backupDir)) + 1)); }
    $zip->close(); $downloadFile = basename($tmp); copy($tmp, __DIR__ . '/../' . $downloadFile);
  }
}
?>
<section class="content-card"><h1 class="admin-title">🧰 Backup / Restore</h1><div class="page-builder-grid"><section class="content-card" style="text-align:left;"><p>Create a quick downloadable zip of your current <code>/data</code> folder before making major edits.</p><?php if (($_SESSION['role'] ?? '') !== 'admin'): ?><p class="msg error">Only admins can create backup zips.</p><?php else: ?><form method="post"><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Create data backup zip</button></form><?php endif; ?><?php if ($downloadFile): ?><p class="msg success">Backup created: <a href="/<?= admin_h($downloadFile) ?>"><?= admin_h($downloadFile) ?></a></p><?php endif; ?></section><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Restore note</h3><p>Automatic restore was not wired into this merge to avoid overwriting your live data by mistake. Use your hosting control panel or upload a backup manually when needed.</p></section></div></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
