<?php
$page_title = "Cetak Laporan";
include 'layout/header.php';
include 'layout/sidebar.php';
require_once '../config.php';

$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 DESC");
    $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 DESC");
    $stmt->execute([$tgl_mulai, $tgl_sampai, $status]);
}
$laporan = $stmt->fetchAll();
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <h1 class="m-0">Laporan Pengajuan</h1>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="card card-primary card-outline">
                <div class="card-header">
                    <h3 class="card-title">Filter Tanggal Laporan</h3>
                </div>
                <div class="card-body">
                    <form method="GET" class="row align-items-end">
                        <div class="col-md-3">
                            <label>Dari Tanggal</label>
                            <input type="date" class="form-control" name="tgl_mulai" value="<?= $tgl_mulai ?>" required>
                        </div>
                        <div class="col-md-3">
                            <label>Sampai Tanggal</label>
                            <input type="date" class="form-control" name="tgl_sampai" value="<?= $tgl_sampai ?>" required>
                        </div>
                        <div class="col-md-3">
                            <label>Status</label>
                            <select name="status" class="form-control">
                                <option value="semua" <?= $status == 'semua' ? 'selected' : '' ?>>Semua Status</option>
                                <option value="diterima" <?= $status == 'diterima' ? 'selected' : '' ?>>Diterima</option>
                                <option value="diverifikasi" <?= $status == 'diverifikasi' ? 'selected' : '' ?>>Diverifikasi</option>
                                <option value="diproses" <?= $status == 'diproses' ? 'selected' : '' ?>>Diproses</option>
                                <option value="ditolak" <?= $status == 'ditolak' ? 'selected' : '' ?>>Ditolak</option>
                                <option value="selesai" <?= $status == 'selesai' ? 'selected' : '' ?>>Selesai</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-primary"><i class="fas fa-filter"></i> Filter</button>
                            <a href="cetak_laporan.php?tgl_mulai=<?= $tgl_mulai ?>&tgl_sampai=<?= $tgl_sampai ?>&status=<?= $status ?>" target="_blank" class="btn btn-success">
                                <i class="fas fa-print"></i> Cetak
                            </a>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-sm datatable">
                            <thead class="bg-light">
                                <tr>
                                    <th>No</th>
                                    <th>No. Tiket</th>
                                    <th>Tgl Masuk</th>
                                    <th>Nama Pemohon</th>
                                    <th>Instansi</th>
                                    <th>Tujuan</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no=1; foreach($laporan as $row): ?>
                                <tr>
                                    <td><?= $no++ ?></td>
                                    <td><?= $row->no_tiket ?></td>
                                    <td><?= date('d/m/Y', strtotime($row->created_at)) ?></td>
                                    <td><?= $row->nama_lengkap ?></td>
                                    <td><?= $row->asal_instansi ?></td>
                                    <td><?= $row->tujuan ?></td>
                                    <td><?= strtoupper($row->status) ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php include 'layout/footer.php'; ?>
