<?php
require __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
  header("Location: ../Login/login.php");
  exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
  http_response_code(400);
  echo "ID tidak valid.";
  exit;
}

// Ambil data pinjaman + user + buku
$sql = "
  SELECT 
    br.id AS borrow_id,
    br.tgl_pinjam, br.tgl_jatuh_tempo, br.tgl_kembali,
    br.status,
    br.denda, br.denda_bayar, br.denda_bayar_tanggal,
    br.bukti_denda_path,
    u.fullname AS nama_user, u.phone AS wa_number,
    b.judul AS judul_buku,
    bi.no_buku, bi.rak
  FROM borrowings br
  JOIN users u ON br.user_id = u.id
  JOIN book_items bi ON br.book_item_id = bi.id
  JOIN books b ON bi.book_id = b.id
  WHERE br.id = :id
  LIMIT 1
";
$stmt = $pdo->prepare($sql);
$stmt->execute([':id' => $id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$row) {
  http_response_code(404);
  echo "Data tidak ditemukan.";
  exit;
}

// Tentukan nilai yang dicetak di kuitansi
$nama_user = $row['nama_user'] ?? '-';
$judul_buku = $row['judul_buku'] ?? '-';
$no_buku = $row['no_buku'] ?? '-';
$rak = $row['rak'] ?? '-';

$tgl_pinjam = $row['tgl_pinjam'] ?? null;
$tgl_jt     = $row['tgl_jatuh_tempo'] ?? null;
$tgl_kembali= $row['tgl_kembali'] ?? null;

$denda_bayar = isset($row['denda_bayar']) ? (int)$row['denda_bayar'] : 0;
$tgl_bayar   = !empty($row['denda_bayar_tanggal']) ? date('d-m-Y H:i', strtotime($row['denda_bayar_tanggal'])) : '-';

function rupiah($n){ return 'Rp ' . number_format((int)$n, 0, ',', '.'); }
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<title>Kuitansi Pembayaran Denda #<?= htmlspecialchars($row['borrow_id']) ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  body { font-family: Arial, sans-serif; color:#222; margin:20px; }
  .receipt { max-width: 720px; margin: 0 auto; border:1px solid #ddd; padding:20px; border-radius:8px; }
  h1 { font-size:20px; margin:0 0 10px; }
  .meta, .items, .total { width:100%; border-collapse: collapse; margin-top:12px; }
  .meta td { padding:6px 0; }
  .items th, .items td { border:1px solid #eee; padding:8px; text-align:left; }
  .items th { background:#f9f9f9; }
  .total td { padding:8px 0; font-size: 16px; }
  .right { text-align:right; }
  .muted { color:#666; }
  .actions { margin-top:16px; display:flex; gap:10px; }
  @media print {
    .actions { display:none; }
    .receipt { border:none; }
  }
  .stamp {
    display:inline-block; padding:6px 10px; border:1px dashed #8bc34a; color:#2e7d32; border-radius:6px; font-size:12px;
  }
</style>
</head>
<body>
<div class="receipt">
  <h1>Kuitansi Pembayaran Denda</h1>
  <div class="muted">Pustakaia • #<?= htmlspecialchars($row['borrow_id']) ?></div>

  <table class="meta">
    <tr><td>Nama Peminjam</td><td>: <?= htmlspecialchars($nama_user) ?></td></tr>
    <tr><td>Judul Buku</td><td>: <?= htmlspecialchars($judul_buku) ?></td></tr>
    <tr><td>No. Buku / Rak</td><td>: <?= htmlspecialchars($no_buku) ?> / <?= htmlspecialchars($rak) ?></td></tr>
    <tr><td>Tgl Pinjam</td><td>: <?= $tgl_pinjam ? date('d-m-Y', strtotime($tgl_pinjam)) : '-' ?></td></tr>
    <tr><td>Jatuh Tempo</td><td>: <?= $tgl_jt ? date('d-m-Y', strtotime($tgl_jt)) : '-' ?></td></tr>
    <tr><td>Tgl Kembali</td><td>: <?= $tgl_kembali ? date('d-m-Y', strtotime($tgl_kembali)) : '-' ?></td></tr>
    <tr><td>Tgl Pembayaran</td><td>: <?= htmlspecialchars($tgl_bayar) ?></td></tr>
  </table>

  <table class="items">
    <thead>
      <tr><th>Deskripsi</th><th class="right">Nominal</th></tr>
    </thead>
    <tbody>
      <tr>
        <td>Denda keterlambatan</td>
        <td class="right"><?= rupiah($denda_bayar) ?></td>
      </tr>
    </tbody>
  </table>

  <table class="total">
    <tr><td class="right"><strong>Total Dibayar: <?= rupiah($denda_bayar) ?></strong></td></tr>
  </table>

  <?php if (!empty($row['bukti_denda_path'])): ?>
    <p class="muted">Lampiran bukti pembayaran:
      <a href="<?= htmlspecialchars($row['bukti_denda_path']) ?>" target="_blank" rel="noopener">Lihat File</a>
    </p>
  <?php endif; ?>

  <?php if ((int)$denda_bayar > 0): ?>
    <p><span class="stamp">LUNAS</span></p>
  <?php endif; ?>

  <div class="actions">
    <button onclick="window.print()">🖨️ Cetak / Simpan PDF</button>
    <?php if (!empty($row['bukti_denda_path'])): ?>
      <a href="<?= htmlspecialchars($row['bukti_denda_path']) ?>" target="_blank" rel="noopener">Buka Bukti Upload</a>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
