<?php
require_once __DIR__ . '/includes/functions.php';
requireLogin();

$id = $_GET['id'] ?? 0;
$stmt = $pdo->prepare("
    SELECT t.*, k.nama as kategori_nama, a.nama_akun, s.nama as nama_siswa, s.nis, u.nama as nama_kasir
    FROM transaksi t
    JOIN kategori k ON k.id = t.kategori_id
    JOIN akun_kas a ON a.id = t.akun_id
    LEFT JOIN siswa s ON s.id = t.siswa_id
    LEFT JOIN users u ON u.id = t.created_by
    WHERE t.id = :id
");
$stmt->execute(['id'=>$id]);
$tx = $stmt->fetch();

if (!$tx) { die('Transaksi tidak ditemukan.'); }

$pengaturan = getPengaturan($pdo);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Kwitansi - <?= htmlspecialchars($tx['no_transaksi']) ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { font-family: 'Segoe UI', Arial, sans-serif; }
.kwitansi { max-width: 700px; margin: 30px auto; border: 2px solid #333; padding: 25px; }
.kop { display:flex; align-items:center; gap:15px; border-bottom:3px double #333; padding-bottom:10px; margin-bottom:20px; }
.kop img { height: 65px; }
.kop h4, .kop p { margin:0; }
@media print { .no-print { display:none; } body { background:#fff; } }
</style>
</head>
<body>
<div class="text-center my-3 no-print">
    <button class="btn btn-primary" onclick="window.print()"><i class="bi bi-printer"></i> Cetak Kwitansi</button>
    <a href="pembayaran_spp.php" class="btn btn-light">Kembali</a>
</div>
<div class="kwitansi">
    <div class="kop">
        <?php if (!empty($pengaturan['logo'])): ?><img src="assets/uploads/logo/<?= htmlspecialchars($pengaturan['logo']) ?>"><?php endif; ?>
        <div>
            <h4><?= htmlspecialchars($pengaturan['nama_sekolah']) ?></h4>
            <p class="small"><?= htmlspecialchars($pengaturan['alamat']) ?></p>
        </div>
    </div>

    <h5 class="text-center text-uppercase mb-4">Kwitansi Pembayaran</h5>

    <table class="table table-borderless mb-4">
        <tr><td width="180">No. Transaksi</td><td>: <?= htmlspecialchars($tx['no_transaksi']) ?></td></tr>
        <tr><td>Tanggal</td><td>: <?= tanggalIndo($tx['tanggal']) ?></td></tr>
        <?php if ($tx['nama_siswa']): ?>
        <tr><td>Nama Siswa</td><td>: <?= htmlspecialchars($tx['nama_siswa']) ?></td></tr>
        <tr><td>NIS</td><td>: <?= htmlspecialchars($tx['nis']) ?></td></tr>
        <?php endif; ?>
        <tr><td>Keterangan</td><td>: <?= htmlspecialchars($tx['keterangan']) ?></td></tr>
        <tr><td>Kategori</td><td>: <?= htmlspecialchars($tx['kategori_nama']) ?></td></tr>
        <tr><td>Diterima Melalui</td><td>: <?= htmlspecialchars($tx['nama_akun']) ?></td></tr>
    </table>

    <table class="table table-bordered">
        <tr class="fw-bold fs-5"><td>Jumlah Dibayar</td><td class="text-end"><?= rupiah($tx['jumlah']) ?></td></tr>
    </table>

    <div class="row mt-5">
        <div class="col-6"></div>
        <div class="col-6 text-center">
            <p>Bekasi, <?= tanggalIndo($tx['tanggal']) ?><br>Diterima oleh,</p>
            <p class="mt-5 mb-0">(<?= htmlspecialchars($tx['nama_kasir'] ?? $pengaturan['bendahara'] ?? '..........................') ?>)</p>
        </div>
    </div>
</div>
</body>
</html>
