<?php
// kasir/dashboard.php
require_once '../includes/config.php';
require_once '../includes/auth.php';
requireKasir();

$user = currentUser();

$trxHariIni  = $pdo->prepare("SELECT COUNT(*) FROM transaksi WHERE kasir_id=? AND DATE(tanggal)=CURDATE()");
$trxHariIni->execute([$user['id']]);
$trxHariIni = $trxHariIni->fetchColumn();

$omzetHariIni = $pdo->prepare("SELECT COALESCE(SUM(total),0) FROM transaksi WHERE kasir_id=? AND DATE(tanggal)=CURDATE()");
$omzetHariIni->execute([$user['id']]);
$omzetHariIni = $omzetHariIni->fetchColumn();

$totalBarang = $pdo->query("SELECT COUNT(*) FROM barang WHERE aktif=1 AND stok > 0")->fetchColumn();

$riwayat = $pdo->prepare("SELECT * FROM transaksi WHERE kasir_id=? ORDER BY tanggal DESC LIMIT 6");
$riwayat->execute([$user['id']]);
$riwayat = $riwayat->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>Beranda Kasir — <?= 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>
      <div class="topbar-title">🏠 Beranda Kasir</div>
      <a href="penjualan.php" class="topbar-btn">🛒 Mulai Penjualan</a>
    </div>
    <div class="page-body">
      <div class="stat-grid">
        <div class="stat-card sc-blue">
          <div class="sc-label">Transaksi Hari Ini</div>
          <div class="sc-val"><?= $trxHariIni ?></div>
          <div class="sc-sub">oleh <?= htmlspecialchars($user['nama']) ?></div>
        </div>
        <div class="stat-card sc-green">
          <div class="sc-label">Omzet Hari Ini</div>
          <div class="sc-val"><?= formatRp((float)$omzetHariIni) ?></div>
          <div class="sc-sub"><?= date('d F Y') ?></div>
        </div>
        <div class="stat-card sc-amber">
          <div class="sc-label">Barang Tersedia</div>
          <div class="sc-val"><?= $totalBarang ?></div>
          <div class="sc-sub">item aktif</div>
        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <h3>🕐 Transaksi Terakhir Saya</h3>
          <a href="riwayat.php" class="btn btn-ghost btn-sm">Lihat Semua</a>
        </div>
        <div class="table-wrap">
          <table>
            <thead><tr><th>No Transaksi</th><th>Total</th><th>Metode</th><th>Waktu</th><th>Aksi</th></tr></thead>
            <tbody>
              <?php foreach ($riwayat as $r): ?>
              <tr>
                <td><strong><?= htmlspecialchars($r['no_transaksi']) ?></strong></td>
                <td><?= formatRp((float)$r['total']) ?></td>
                <td><span class="badge badge-blue"><?= ucfirst($r['metode']) ?></span></td>
                <td style="font-size:12px;color:var(--muted)"><?= date('d/m/Y H:i', strtotime($r['tanggal'])) ?></td>
                <td><a href="riwayat.php?id=<?= $r['id'] ?>" class="btn btn-ghost btn-sm">Struk</a></td>
              </tr>
              <?php endforeach; ?>
              <?php if (empty($riwayat)): ?><tr><td colspan="5" style="text-align:center;color:var(--muted);padding:30px">Belum ada transaksi hari ini</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="<?= BASE_URL ?>js/app.js"></script>
</body>
</html>
