<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Cetak Laporan Perpustakaan</title>
    <style>
        *{ box-sizing:border-box; }
        body { font-family: Arial, Helvetica, sans-serif; font-size: 12px; }
        h2, h3, p { text-align: center; margin: 2px; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        table th, table td { border: 1px solid black; padding: 5px; }
        table th { background: #f2f2f2; }
        .text-center { text-align: center; }
        .text-right { text-align: right; }
        .summary { margin-top: 20px; }
        .cetak-toolbar{
            display:flex;
            gap:10px;
            justify-content:center;
            margin-bottom:30px;
            padding:12px 20px;
            background:#f8f9fa;
            border:1px solid #dee2e6;
            border-radius:6px;
            position:sticky;
            top:10px;
            z-index:999;
        }
        .cetak-toolbar .btn{
            display:inline-flex;
            align-items:center;
            gap:6px;
            padding:10px 24px;
            font-size:14px;
            font-weight:600;
            border:none;
            border-radius:5px;
            cursor:pointer;
            text-decoration:none;
            transition:all 0.2s ease;
        }
        .cetak-toolbar .btn-print{
            background:#28a745;
            color:#fff;
        }
        .cetak-toolbar .btn-print:hover{
            background:#218838;
        }
        .cetak-toolbar .btn-exit{
            background:#dc3545;
            color:#fff;
        }
        .cetak-toolbar .btn-exit:hover{
            background:#c82333;
        }
        @media print{
            .cetak-toolbar{ display:none !important; }
        }
    </style>
</head>
<body>
    <!-- Toolbar tombol navigasi -->
    <div class="cetak-toolbar">
        <button class="btn btn-print" onclick="window.print()">
            🖨 Cetak
        </button>
        <button class="btn btn-exit" onclick="exitCetak()">
            ✕ Tutup / Exit
        </button>
    </div>
    <h2>PERPUSTAKAAN</h2>
    <h3>LAPORAN PEMINJAMAN BUKU</h3>
    <p>Tanggal Cetak: <?= date('d-m-Y H:i'); ?></p>
    <hr>

    <table class="summary">
        <tr>
            <td width="150">Total Peminjaman</td>
            <td>: <?= $totalPinjam ?></td>
            <td width="150">Total Denda</td>
            <td>: Rp <?= number_format($totalDenda,0,',','.') ?></td>
        </tr>
        <tr>
            <td>Sudah Dikembalikan</td>
            <td>: <?= $totalKembali ?></td>
            <td>Belum Dikembalikan</td>
            <td>: <?= $belumKembali ?></td>
        </tr>
    </table>

    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>NISN</th>
                <th>Nama</th>
                <th>Kelas</th>
                <th>Total Pinjam</th>
                <th>Sudah Kembali</th>
                <th>Belum Kembali</th>
                <th>Total Denda</th>
            </tr>
        </thead>
        <tbody>
        <?php $no=1; foreach($laporan as $row): ?>
            <tr>
                <td class="text-center"><?= $no++ ?></td>
                <td><?= $row['nisn'] ?></td>
                <td><?= $row['nama'] ?></td>
                <td><?= $row['kelas'] ?></td>
                <td class="text-center"><?= $row['total_pinjam'] ?></td>
                <td class="text-center"><?= $row['sudah_kembali'] ?></td>
                <td class="text-center"><?= $row['belum_kembali'] ?></td>
                <td class="text-right">Rp <?= number_format($row['total_denda'],0,',','.') ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <script>
        function exitCetak() {
            if (window.opener) {
                window.close();
            } else {
                window.location.href = '<?= base_url("dashboard") ?>';
            }
        }
    </script>
</body>
</html>
