<?php
require_once '../config.php';

if (!isset($_SESSION['level'])) {
    die("Akses Ditolak.");
}

$tgl_mulai = isset($_GET['tgl_mulai']) ? $_GET['tgl_mulai'] : date('Y-m-01');
$tgl_sampai = isset($_GET['tgl_sampai']) ? $_GET['tgl_sampai'] : date('Y-m-t');
$status = isset($_GET['status']) ? $_GET['status'] : 'semua';

if ($status == 'semua') {
    $stmt = $pdo->prepare("SELECT * FROM tbl_pengajuan WHERE DATE(created_at) >= ? AND DATE(created_at) <= ? ORDER BY created_at ASC");
    $stmt->execute([$tgl_mulai, $tgl_sampai]);
} else {
    $stmt = $pdo->prepare("SELECT * FROM tbl_pengajuan WHERE DATE(created_at) >= ? AND DATE(created_at) <= ? AND status = ? ORDER BY created_at ASC");
    $stmt->execute([$tgl_mulai, $tgl_sampai, $status]);
}
$laporan = $stmt->fetchAll();

$app_title = getSetting($pdo, 'app_title');
$app_desc = getSetting($pdo, 'app_desc');
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Laporan Pengajuan</title>
    <style>
        body {
            font-family: 'Times New Roman', Times, serif;
            color: #000;
            background: #fff;
            margin: 0;
            padding: 20px;
        }
        .kop-surat {
            border-bottom: 3px solid #000;
            padding-bottom: 10px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }
        .kop-surat img {
            max-height: 90px;
            width: auto;
        }
        .kop-teks {
            flex: 1;
            text-align: center;
            margin: 0 0px;
        }
        .kop-teks p {
            margin: 0;
            line-height: 1.3;
        }
        .kop-teks .instansi {
            font-size: 15px;
            font-weight: bold;
        }
        .kop-teks .kantor {
            font-size: 16px;
            font-weight: bold;
        }
        .kop-teks .alamat {
            font-size: 11px;
        }
        .judul-laporan {
            text-align: center;
            margin-bottom: 20px;
        }
        .judul-laporan h4 {
            margin: 0;
            font-size: 18px;
            text-transform: uppercase;
        }
        .judul-laporan p {
            margin: 5px 0 0;
            font-size: 14px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
        }
        th, td {
            border: 1px solid #000;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            -webkit-print-color-adjust: exact;
            text-align: center;
        }
        .text-center {
            text-align: center;
        }
        @media print {
            body { padding: 0; }
            @page { margin: 15mm; }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="kop-surat">
        <div style="width: 15%; text-align: center;">
            <?php if(file_exists('../logo/logo.png')): ?>
                <img src="../logo/logo.png" alt="Logo">
            <?php else: ?>
                <div style="width: 80px; height: 80px; background: #ddd; border-radius: 50%; margin: 0 auto;"></div>
            <?php endif; ?>
        </div>
        <div class="kop-teks">
            <p class="instansi">KEMENTERIAN IMIGRASI DAN PEMASYARAKATAN RI</p>
            <p class="instansi">KANTOR WILAYAH DIREKTORAT JENDERAL IMIGRASI JAWA BARAT</p>
            <p class="kantor">KANTOR IMIGRASI KELAS I NON TPI SUKABUMI</p>
            <p class="alamat">Jl. Lingkar Selatan No. 7, Sudajayahilir, Baros, Kota Sukabumi, Jawa Barat</p>
            <p class="alamat">Telepon : (0266) 243900, Faksimile : - Laman : -, Pos-el : kanim_sukabumi@imigrasi.go.id</p>
        </div>
        <div style="width: 15%;"></div> <!-- Balancer -->
    </div>

    <div class="judul-laporan">
        <h4>Laporan Pengajuan Permohonan</h4>
        <p>Periode: <?= tgl_indo($tgl_mulai) ?> s/d <?= tgl_indo($tgl_sampai) ?> | Status: <?= strtoupper($status) ?></p>
    </div>

    <table>
        <thead>
            <tr>
                <th width="5%">No</th>
                <th width="15%">No. Tiket</th>
                <th width="12%">Tgl Masuk</th>
                <th width="20%">Nama Pemohon</th>
                <th width="20%">Instansi / Kota</th>
                <th width="18%">Tujuan</th>
                <th width="10%">Status</th>
            </tr>
        </thead>
        <tbody>
            <?php if(count($laporan) > 0): ?>
                <?php $no=1; foreach($laporan as $row): ?>
                <tr>
                    <td class="text-center"><?= $no++ ?></td>
                    <td class="text-center"><?= htmlspecialchars($row->no_tiket) ?></td>
                    <td class="text-center"><?= date('d/m/Y', strtotime($row->created_at)) ?></td>
                    <td>
                        <?= htmlspecialchars($row->nama_lengkap) ?><br>
                        <small style="color: #555;"><?= htmlspecialchars($row->no_telp) ?></small>
                    </td>
                    <td>
                        <?= htmlspecialchars($row->asal_instansi) ?><br>
                        <small style="color: #555;">Kota: <?= htmlspecialchars($row->kota) ?></small>
                    </td>
                    <td><?= htmlspecialchars($row->tujuan) ?></td>
                    <td class="text-center"><?= strtoupper($row->status) ?></td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="7" class="text-center">Tidak ada data pengajuan pada periode ini.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <div style="margin-top: 50px; float: right; text-align: center; width: 300px;">
        <p>Sukabumi, <?= date('d') . ' ' . explode(' ', tgl_indo(date('Y-m-d')))[1] . ' ' . date('Y') ?></p>
        <p>Mengetahui,</p>
        <p style="margin-top: 80px; font-weight: bold; text-decoration: underline; text-transform: uppercase;"><?= htmlspecialchars($_SESSION['nama_lengkap']) ?></p>
    </div>

</body>
</html>
