<?php include __DIR__ . '/../../views/layouts/header.php'; ?>
<div class="wrapper">
<?php include __DIR__ . '/../../views/layouts/sidebar.php'; ?>
<div class="main-content">
<?php include __DIR__ . '/../../views/layouts/topbar.php'; ?>
<div class="page-content">
  <div class="page-header">
    <h1><i class="bi bi-cloud-download me-2 text-primary"></i>Backup Data</h1>
  </div>

  <?php if ($flash): ?>
  <div class="alert alert-<?= $flash['type'] ?> alert-auto-dismiss alert-dismissible fade show">
    <?= e($flash['message']) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button>
  </div>
  <?php endif; ?>

  <div class="row justify-content-center">
    <div class="col-12 col-lg-7">
      <div class="card">
        <div class="card-header"><h6><i class="bi bi-database me-2"></i>Backup Database</h6></div>
        <div class="card-body">
          <div class="alert alert-info">
            <i class="bi bi-info-circle-fill me-2"></i>
            Backup akan mengunduh file SQL yang berisi seluruh data aplikasi.
            Simpan file backup di tempat yang aman.
          </div>

          <div class="row g-3 mb-4">
            <div class="col-6">
              <div class="stat-card">
                <div class="stat-icon blue"><i class="bi bi-people"></i></div>
                <div class="stat-info">
                  <?php
                  $db = getDB();
                  $totalSiswa = (query_fetch_assoc($db, "SELECT COUNT(*) as c FROM siswa") ?? [])['c'] ?? 0;
                  $totalGuru  = (query_fetch_assoc($db, "SELECT COUNT(*) as c FROM guru") ?? [])['c'] ?? 0;
                  ?>
                  <div class="stat-value"><?= $totalSiswa ?></div>
                  <div class="stat-label">Data Siswa</div>
                </div>
              </div>
            </div>
            <div class="col-6">
              <div class="stat-card">
                <div class="stat-icon green"><i class="bi bi-person-badge"></i></div>
                <div class="stat-info">
                  <div class="stat-value"><?= $totalGuru ?></div>
                  <div class="stat-label">Data Guru</div>
                </div>
              </div>
            </div>
          </div>

          <a href="<?= BASE_URL ?>/index.php?page=pengaturan_backup&download=1"
             class="btn btn-primary btn-lg w-100">
            <i class="bi bi-cloud-download me-2"></i>
            Download Backup SQL
          </a>

          <div class="text-muted text-center mt-3 fs-sm">
            <i class="bi bi-clock me-1"></i>
            Terakhir backup: <?= date('d F Y H:i') ?>
          </div>
        </div>
      </div>

      <div class="card mt-3">
        <div class="card-header"><h6><i class="bi bi-info-circle me-2"></i>Cara Restore Backup</h6></div>
        <div class="card-body">
          <ol class="fs-sm">
            <li>Buka phpMyAdmin di browser</li>
            <li>Pilih database <code>siakad</code></li>
            <li>Klik tab <strong>Import</strong></li>
            <li>Pilih file backup <code>.sql</code> yang sudah didownload</li>
            <li>Klik <strong>Go</strong> untuk memulai restore</li>
          </ol>
        </div>
      </div>
    </div>
  </div>
</div>
<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
