<?php
// modules/laporan/print.php
require_once '../../config/database.php';
$id = (int)($_GET['id'] ?? 0);
$conn = getConnection();

$laporan = $conn->query("SELECT l.*, e.full_name as pembuat FROM laporan_unit l LEFT JOIN employees e ON l.created_by = e.id WHERE l.id = $id")->fetch_assoc();
if (!$laporan) die("Laporan tidak ditemukan");

$permasalahan = $conn->query("SELECT * FROM permasalahan_unit WHERE laporan_id = $id");
$disiplin = $conn->query("SELECT * FROM disiplin_staf WHERE laporan_id = $id");
?>
<!DOCTYPE html>
<html>
<head><title>Cetak Laporan</title>
<link href="https://cdn.tailwindcss.com" rel="stylesheet">
<style>@media print { .no-print { display: none; } }</style>
</head>
<body class="p-8">
    <div class="no-print text-right mb-4">
        <button onclick="window.print()" class="bg-blue-600 text-white px-4 py-2 rounded">Cetak</button>
        <a href="javascript:window.close()" class="bg-gray-500 text-white px-4 py-2 rounded ml-2">Tutup</a>
    </div>
    <!-- Kop Laporan -->
    <div class="text-center border-b pb-4 mb-6">
        <h1 class="text-2xl font-bold">RS BALIMED DENPASAR</h1>
        <h2 class="text-xl">LAPORAN KEPERAWATAN</h2>
        <p><?= $laporan['bulan'] ?> <?= $laporan['tahun'] ?> - Unit <?= $laporan['unit'] ?></p>
    </div>
    <!-- Data Tenaga -->
    <table class="w-full mb-4">
        <tr><td>Jumlah Pasien</td><td>: <?= number_format($laporan['jumlah_pasien']) ?></td></tr>
        <tr><td>Jumlah Tenaga</td><td>: <?= $laporan['jumlah_tenaga'] ?></td></tr>
        <tr><td>Rasio</td><td>: 1:<?= number_format($laporan['rasio_tenaga'],2) ?></td></tr>
    </table>
    <!-- Permasalahan -->
    <?php if ($permasalahan->num_rows > 0): ?>
    <h3 class="font-bold mt-4 mb-2">Permasalahan:</h3>
    <table class="w-full border">
        <thead><tr><th>No</th><th>Permasalahan</th><th>Tindak Lanjut</th></tr></thead>
        <tbody><?php $no=1; while($row=$permasalahan->fetch_assoc()): ?>
        <tr><td><?= $no++ ?></td><td><?= $row['permasalahan'] ?></td><td><?= $row['tindak_lanjut'] ?></td></tr>
        <?php endwhile; ?></tbody>
    </table>
    <?php endif; ?>
    <!-- Tanda Tangan -->
    <div class="text-right mt-12">Denpasar, <?= date('d F Y') ?><br><br><br><?= $laporan['pembuat'] ?></div>
</body>
</html>