<?php include 'header.php'; ?>
<?php
include '../koneksi.php';

// Check connection
if (!$koneksi) {
    die("Connection failed: " . mysqli_connect_error());
}

$filter_type = isset($_POST['filter_type']) ? $_POST['filter_type'] : '';
$report_data = [];

// Process the filter form
if (isset($_POST['filter_report'])) {
    if ($filter_type == 'date') {
        // Filter by specific date range
        $start_date = $_POST['start_date'];
        $end_date = $_POST['end_date'];

        $query = "SELECT pendaftaran.id_pendaftaran, pasien.nama_pasien, poli.nama_poli, dokter.nama_dokter, pendaftaran.tanggal_daftar, pendaftaran.keterangan, pendaftaran.status
                  FROM tbl_pendaftaran pendaftaran
                  JOIN tbl_pasien pasien ON pendaftaran.id_pasien = pasien.id_pasien
                  JOIN tbl_poli poli ON pendaftaran.id_poli = poli.id_poli
                  JOIN tbl_dokter dokter ON pendaftaran.id_dokter = dokter.id_dokter
                  WHERE pendaftaran.tanggal_daftar BETWEEN '$start_date' AND '$end_date'";
        $result = mysqli_query($koneksi, $query);
        
        if ($result) {
            $report_data = mysqli_fetch_all($result, MYSQLI_ASSOC);
        } else {
            echo "<div class='alert alert-danger'>Query error: " . mysqli_error($koneksi) . "</div>";
        }
    } elseif ($filter_type == 'month') {
        // Filter by specific month and year
        $month = $_POST['month'];
        $year = $_POST['year'];

        $query = "SELECT pendaftaran.id_pendaftaran, pasien.nama_pasien, poli.nama_poli, dokter.nama_dokter, pendaftaran.tanggal_daftar, pendaftaran.keterangan, pendaftaran.status
                  FROM tbl_pendaftaran pendaftaran
                  JOIN tbl_pasien pasien ON pendaftaran.id_pasien = pasien.id_pasien
                  JOIN tbl_poli poli ON pendaftaran.id_poli = poli.id_poli
                  JOIN tbl_dokter dokter ON pendaftaran.id_dokter = dokter.id_dokter
                  WHERE MONTH(pendaftaran.tanggal_daftar) = '$month' AND YEAR(pendaftaran.tanggal_daftar) = '$year'";
        $result = mysqli_query($koneksi, $query);
        
        if ($result) {
            $report_data = mysqli_fetch_all($result, MYSQLI_ASSOC);
        } else {
            echo "<div class='alert alert-danger'>Query error: " . mysqli_error($koneksi) . "</div>";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Report by Date and Month</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h2>Filter Laporan Pendaftaran Pasien</h2>
    <form method="POST" action="">
        <div class="mb-3">
            <label for="filter_type" class="form-label">Filter Berdasarkan:</label>
            <select id="filter_type" name="filter_type" class="form-select" required>
                <option value="">Pilih Filter</option>
                <option value="date" <?php if ($filter_type == 'date') echo 'selected'; ?>>Tanggal</option>
                <option value="month" <?php if ($filter_type == 'month') echo 'selected'; ?>>Bulan</option>
            </select>
        </div>
        
        <!-- Date Range Filter -->
        <div id="date_filter" style="display: none;">
            <div class="mb-3">
                <label for="start_date" class="form-label">Dari Tanggal:</label>
                <input type="date" id="start_date" name="start_date" class="form-control">
            </div>
            <div class="mb-3">
                <label for="end_date" class="form-label">Sampai Tanggal:</label>
                <input type="date" id="end_date" name="end_date" class="form-control">
            </div>
        </div>

        <!-- Month Filter -->
        <div id="month_filter" style="display: none;">
            <div class="mb-3">
                <label for="month" class="form-label">Bulan:</label>
                <input type="number" id="month" name="month" class="form-control" min="1" max="12" placeholder="1-12">
            </div>
            <div class="mb-3">
                <label for="year" class="form-label">Tahun:</label>
                <input type="number" id="year" name="year" class="form-control" placeholder="YYYY">
            </div>
        </div>
        
        <button type="submit" name="filter_report" class="btn btn-primary">Filter Laporan</button>
        <button type="button" class="btn btn-secondary" onclick="printReport()">Cetak Laporan</button>
    </form>

   
    <div id="report_section">
        <h2>Laporan Data Bulanan Dan Mingguan</h2>
        <table class="table table-bordered">
            <thead class="table-dark">
                <tr>
                    <th>No.</th>
                    <th>Nama Pasien</th>
                    <th>Poli</th>
                    <th>Dokter</th>
                    <th>Tanggal Daftar</th>
                    
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($report_data)): ?>
                    <?php $no = 1; ?>
                    <?php foreach ($report_data as $row): ?>
                        <tr>
                            <td><?php echo $no++; ?></td>
                            <td><?php echo $row['nama_pasien']; ?></td>
                            <td><?php echo $row['nama_poli']; ?></td>
                            <td><?php echo $row['nama_dokter']; ?></td>
                            <td><?php echo $row['tanggal_daftar']; ?></td>
                            
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7">Tidak ada data pendaftaran.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    document.getElementById('filter_type').addEventListener('change', function() {
        var dateFilter = document.getElementById('date_filter');
        var monthFilter = document.getElementById('month_filter');
        
        if (this.value === 'date') {
            dateFilter.style.display = 'block';
            monthFilter.style.display = 'none';
        } else if (this.value === 'month') {
            dateFilter.style.display = 'none';
            monthFilter.style.display = 'block';
        } else {
            dateFilter.style.display = 'none';
            monthFilter.style.display = 'none';
        }
    });

    function printReport() {
        var reportContent = document.getElementById('report_section').innerHTML;
        var originalContent = document.body.innerHTML;

        document.body.innerHTML = reportContent;
        window.print();
        document.body.innerHTML = originalContent;
    }
</script>
</body>
</html>
