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

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT t.*, a.nama AS kasir_nama FROM transaksi t JOIN akun a ON t.kasir_id=a.id WHERE t.id=?");
$stmt->execute([$id]);
$trx = $stmt->fetch();
if (!$trx) { echo 'Transaksi tidak ditemukan.'; exit; }

$items = $pdo->prepare("SELECT * FROM detail_transaksi WHERE transaksi_id=?");
$items->execute([$id]);
$items = $items->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>Detail 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() ?>">
  <style>
    /* Detail Transaksi - Responsive Layout */
    .detail-grid {
      display: grid;
      grid-template-columns: 1fr 320px;
      gap: 20px;
    }
    @media (max-width: 768px) {
      .detail-grid {
        grid-template-columns: 1fr;
      }
      /* Info transaksi jadi card ringkas di mobile */
      .info-trx-table td {
        padding: 10px 0 !important;
        font-size: 13px !important;
      }
      .info-trx-table td:first-child {
        width: 40%;
        color: var(--muted);
      }
    }
  </style>
</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">Detail Transaksi</div>
      <a href="laporan.php" class="topbar-btn secondary">← Kembali</a>
      <button class="topbar-btn" onclick="window.print()">🖨️ Cetak Struk</button>
    </div>
    <div class="page-body">
      <div class="detail-grid">
        <div class="card">
          <div class="card-header"><h3>📄 Item Pembelian</h3></div>
          <div class="table-wrap">
            <table>
              <thead><tr><th>Barang</th><th>Harga</th><th>Qty</th><th>Subtotal</th></tr></thead>
              <tbody>
                <?php foreach ($items as $it): ?>
                <tr>
                  <td><strong><?= htmlspecialchars($it['nama_barang']) ?></strong></td>
                  <td><?= formatRp((float)$it['harga_satuan']) ?></td>
                  <td><?= $it['qty'] ?></td>
                  <td><?= formatRp((float)$it['subtotal']) ?></td>
                </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        </div>
        <div class="card">
          <div class="card-header"><h3>Info Transaksi</h3></div>
          <div class="card-body">
            <table class="info-trx-table" style="width:100%;font-size:14px">
              <tr><td style="color:var(--muted);padding:6px 0">No Transaksi</td><td style="font-weight:700"><?= htmlspecialchars($trx['no_transaksi']) ?></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Kasir</td><td><?= htmlspecialchars($trx['kasir_nama']) ?></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Waktu</td><td><?= date('d/m/Y H:i', strtotime($trx['tanggal'])) ?></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Metode</td><td><span class="badge badge-blue"><?= ucfirst($trx['metode']) ?></span></td></tr>
              <tr><td colspan="2"><hr style="margin:10px 0;border:none;border-top:1px solid var(--border)"></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Total</td><td style="font-size:18px;font-weight:700;color:var(--primary)"><?= formatRp((float)$trx['total']) ?></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Bayar</td><td><?= formatRp((float)$trx['bayar']) ?></td></tr>
              <tr><td style="color:var(--muted);padding:6px 0">Kembalian</td><td><?= formatRp((float)$trx['kembalian']) ?></td></tr>
            </table>
          </div>
        </div>
      </div>

      <!-- Struk cetak -->
      <div class="struk" style="display:none" id="struk-print">
        <div class="s-center s-bold" style="font-size:15px"><?= NAMA_TOKO ?></div>
        <div class="s-center"><?= ALAMAT_TOKO ?></div>
        <div class="s-center"><?= TELEPON_TOKO ?></div>
        <hr>
        <div>No: <?= htmlspecialchars($trx['no_transaksi']) ?></div>
        <div>Kasir: <?= htmlspecialchars($trx['kasir_nama']) ?></div>
        <div>Waktu: <?= date('d/m/Y H:i', strtotime($trx['tanggal'])) ?></div>
        <hr>
        <table>
          <?php foreach ($items 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>
          <tr><td>TOTAL</td><td style="text-align:right"><strong><?= formatRp((float)$trx['total']) ?></strong></td></tr>
          <tr><td>BAYAR</td><td style="text-align:right"><?= formatRp((float)$trx['bayar']) ?></td></tr>
          <tr><td>KEMBALIAN</td><td style="text-align:right"><?= formatRp((float)$trx['kembalian']) ?></td></tr>
        </table>
        <hr>
        <div class="s-center">Terima kasih!</div>
      </div>
    </div>
  </div>
</div>
<script src="<?= BASE_URL ?>js/app.js"></script>
<script>
function printStruk() {
  document.getElementById('struk-print').style.display = 'block';
  window.print();
  document.getElementById('struk-print').style.display = 'none';
}
</script>
</body>
</html>
