<?php
// admin/dashboard.php
require_once '../includes/config.php';
require_once '../includes/auth.php';
requireAdmin();

$user = currentUser();

// Statistik
$totalBarang    = $pdo->query("SELECT COUNT(*) FROM barang WHERE aktif=1")->fetchColumn();
$totalAkun      = $pdo->query("SELECT COUNT(*) FROM akun WHERE aktif=1")->fetchColumn();
$totalHariIni   = $pdo->query("SELECT COUNT(*) FROM transaksi WHERE DATE(tanggal)=CURDATE()")->fetchColumn();
$omzetHariIni   = $pdo->query("SELECT COALESCE(SUM(total),0) FROM transaksi WHERE DATE(tanggal)=CURDATE()")->fetchColumn();
$omzetBulanIni  = $pdo->query("SELECT COALESCE(SUM(total),0) FROM transaksi WHERE MONTH(tanggal)=MONTH(CURDATE()) AND YEAR(tanggal)=YEAR(CURDATE())")->fetchColumn();
$stokRendah     = $pdo->query("SELECT COUNT(*) FROM barang WHERE stok <= 5 AND aktif=1")->fetchColumn();

// Transaksi terbaru
$transaksiTerbaru = $pdo->query("
    SELECT t.*, a.nama AS kasir_nama
    FROM transaksi t
    JOIN akun a ON t.kasir_id = a.id
    ORDER BY t.tanggal DESC
    LIMIT 8
")->fetchAll();

// Grafik 7 hari
$grafik = $pdo->query("
    SELECT DATE(tanggal) AS tgl, SUM(total) AS omzet
    FROM transaksi
    WHERE tanggal >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
    GROUP BY DATE(tanggal)
    ORDER BY tgl ASC
")->fetchAll();
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <title>Dashboard Admin — <?= NAMA_TOKO ?></title>
  <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="<?= BASE_URL ?>css/style.css?v=<?= time() ?>">
</head>
<body>
<div class="layout">
  <?php include '../includes/sidebar.php'; ?>

  <div class="main-content">
    <div class="topbar">
      <button class="hamburger" id="hamburgerBtn" aria-label="Menu">☰</button>
      <button class="btn btn-ghost btn-sm" id="sidebarToggle" style="display:none">☰</button>
      <div class="topbar-title">Dashboard Admin</div>
      <span style="font-size:13px;color:var(--muted)">Halo, <?= htmlspecialchars($user['nama']) ?> 👋</span>
    </div>

    <div class="page-body">

      <!-- Stat Cards -->
      <div class="stat-grid">
        <div class="stat-card sc-blue">
          <div class="sc-label">Omzet Hari Ini</div>
          <div class="sc-val"><?= formatRp((float)$omzetHariIni) ?></div>
          <div class="sc-sub"><?= $totalHariIni ?> transaksi</div>
        </div>
        <div class="stat-card sc-green">
          <div class="sc-label">Omzet Bulan Ini</div>
          <div class="sc-val"><?= formatRp((float)$omzetBulanIni) ?></div>
          <div class="sc-sub"><?= date('F Y') ?></div>
        </div>
        <div class="stat-card sc-amber">
          <div class="sc-label">Total Barang</div>
          <div class="sc-val"><?= $totalBarang ?></div>
          <div class="sc-sub"><?= $stokRendah ?> stok rendah (≤5)</div>
        </div>
        <div class="stat-card sc-red">
          <div class="sc-label">Total Akun</div>
          <div class="sc-val"><?= $totalAkun ?></div>
          <div class="sc-sub">Admin & Kasir aktif</div>
        </div>
      </div>

      <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px">

        <!-- Transaksi Terbaru -->
        <div class="card" style="grid-column:1/-1">
          <div class="card-header">
            <h3>📋 Transaksi Terbaru</h3>
            <a href="laporan.php" class="btn btn-primary btn-sm">Lihat Semua</a>
          </div>
          <div class="table-wrap">
            <table>
              <thead>
                <tr><th>No Transaksi</th><th>Kasir</th><th>Total</th><th>Bayar</th><th>Metode</th><th>Waktu</th></tr>
              </thead>
              <tbody>
                <?php foreach ($transaksiTerbaru as $t): ?>
                <tr>
                  <td><strong><?= htmlspecialchars($t['no_transaksi']) ?></strong></td>
                  <td><?= htmlspecialchars($t['kasir_nama']) ?></td>
                  <td><?= formatRp((float)$t['total']) ?></td>
                  <td><?= formatRp((float)$t['bayar']) ?></td>
                  <td><span class="badge badge-blue"><?= ucfirst($t['metode']) ?></span></td>
                  <td style="color:var(--muted);font-size:12px"><?= date('d/m/Y H:i', strtotime($t['tanggal'])) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($transaksiTerbaru)): ?>
                <tr><td colspan="6" style="text-align:center;color:var(--muted);padding:30px">Belum ada transaksi</td></tr>
                <?php endif; ?>
              </tbody>
            </table>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>
<script src="<?= BASE_URL ?>js/app.js"></script>
</body>
</html>
