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

$user = currentUser();

// Jika admin, lihat semua. Jika kasir, hanya miliknya
$isAdmin = $user['role'] === 'admin';

$stmt = $isAdmin
    ? $pdo->prepare("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 50")
    : $pdo->prepare("SELECT t.*, a.nama AS kasir_nama FROM transaksi t JOIN akun a ON t.kasir_id=a.id WHERE t.kasir_id=? ORDER BY t.tanggal DESC LIMIT 50");

$isAdmin ? $stmt->execute() : $stmt->execute([$user['id']]);
$list = $stmt->fetchAll();

// Detail satu transaksi jika diminta
$detail = null;
$detailItems = [];
if (isset($_GET['id'])) {
    $s = $pdo->prepare("SELECT t.*, a.nama AS kasir_nama FROM transaksi t JOIN akun a ON t.kasir_id=a.id WHERE t.id=?");
    $s->execute([(int)$_GET['id']]);
    $detail = $s->fetch();
    if ($detail) {
        $s2 = $pdo->prepare("SELECT * FROM detail_transaksi WHERE transaksi_id=?");
        $s2->execute([$detail['id']]);
        $detailItems = $s2->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>Riwayat Transaksi — <?= 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">🕐 Riwayat Transaksi</div>
      <a href="penjualan.php" class="topbar-btn">🛒 Penjualan Baru</a>
    </div>
    <div class="page-body">
      <div class="card">
        <div class="card-header"><h3>Daftar Transaksi (<?= count($list) ?>)</h3></div>
        <div class="table-wrap">
          <table>
            <thead><tr><th>No Transaksi</th><?= $isAdmin?'<th>Kasir</th>':'' ?><th>Total</th><th>Metode</th><th>Waktu</th><th>Aksi</th></tr></thead>
            <tbody>
              <?php foreach ($list as $r): ?>
              <tr>
                <td><strong><?= htmlspecialchars($r['no_transaksi']) ?></strong></td>
                <?php if ($isAdmin): ?><td><?= htmlspecialchars($r['kasir_nama']) ?></td><?php endif; ?>
                <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="?id=<?= $r['id'] ?>" class="btn btn-ghost btn-sm">Struk</a></td>
              </tr>
              <?php endforeach; ?>
              <?php if (empty($list)): ?><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>

<!-- Modal Struk -->
<?php if ($detail): ?>
<div class="modal-overlay open" id="modalStruk">
  <div class="modal" style="max-width:380px">
    <div class="modal-header">
      <h4>🧾 Struk — <?= htmlspecialchars($detail['no_transaksi']) ?></h4>
      <button class="modal-close" onclick="this.closest('.modal-overlay').classList.remove('open')">×</button>
    </div>
    <div class="modal-body">
      <div class="struk">
        <div class="s-center s-bold" style="font-size:15px"><?= NAMA_TOKO ?></div>
        <div class="s-center" style="font-size:11px"><?= ALAMAT_TOKO ?></div>
        <div class="s-center" style="font-size:11px"><?= TELEPON_TOKO ?></div>
        <hr>
        <div>No: <?= htmlspecialchars($detail['no_transaksi']) ?></div>
        <div>Kasir: <?= htmlspecialchars($detail['kasir_nama']) ?></div>
        <div>Waktu: <?= date('d/m/Y H:i', strtotime($detail['tanggal'])) ?></div>
        <hr>
        <table style="width:100%;font-size:12px">
          <?php foreach ($detailItems as $it): ?>
          <tr><td><?= htmlspecialchars($it['nama_barang']) ?></td></tr>
          <tr>
            <td><?= $it['qty'] ?> x <?= formatRp((float)$it['harga_satuan']) ?></td>
            <td style="text-align:right"><?= formatRp((float)$it['subtotal']) ?></td>
          </tr>
          <?php endforeach; ?>
        </table>
        <hr>
        <table style="width:100%;font-size:13px">
          <tr><td>TOTAL</td><td style="text-align:right"><strong><?= formatRp((float)$detail['total']) ?></strong></td></tr>
          <tr><td>BAYAR</td><td style="text-align:right"><?= formatRp((float)$detail['bayar']) ?></td></tr>
          <tr><td>KEMBALIAN</td><td style="text-align:right"><?= formatRp((float)$detail['kembalian']) ?></td></tr>
        </table>
        <hr>
        <div class="s-center">Terima kasih!</div>
      </div>
    </div>
    <div class="modal-footer">
      <a href="riwayat.php" class="btn btn-ghost">Tutup</a>
      <button class="btn btn-primary" onclick="window.print()">🖨️ Cetak</button>
    </div>
  </div>
</div>
<?php endif; ?>

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