<?php
// fetch_laporan.php

// Panggil file koneksi database dan helper
require_once('db.php');
require_once('session_helper.php');

// Pastikan user sudah login
require_login();

// Mengambil data dari payload POST (format JSON)
$input = file_get_contents('php://input');
$data = json_decode($input, true);

$startDate = $data['start_date'] ?? null;
$endDate = $data['end_date'] ?? null;

// Mengirim respons header
header('Content-Type: text/html');

if (!$startDate || !$endDate) {
    echo '<div class="text-center py-2 text-gray-500">Mohon pilih rentang tanggal yang valid.</div>';
    exit();
}

// Fungsi untuk mengambil data absensi (gunakan fungsi yang sama seperti di absensiadmin.php)
function getAbsensiData($conn, $startDate, $endDate) {
    $sql = "
        SELECT a.status, a.keterangan, s.nama_lengkap, s.kelas, a.waktu, a.metode
        FROM absensi a
        JOIN siswa s ON a.siswa_id = s.user_id
        WHERE DATE(a.waktu) BETWEEN ? AND ? AND a.jenis_absensi = 'masuk'
        ORDER BY a.waktu DESC
    ";
    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        return ['error' => "Query Error: " . $conn->error];
    }
    $stmt->bind_param("ss", $startDate, $endDate);
    $stmt->execute();
    
    $stmt->bind_result($status_db, $keterangan_db, $nama_lengkap_db, $kelas_db, $waktu_db, $metode_db);
    
    $data = [];
    while ($stmt->fetch()) {
        $data[] = [
            'status' => $status_db,
            'keterangan' => $keterangan_db,
            'nama_lengkap' => $nama_lengkap_db,
            'kelas' => $kelas_db,
            'waktu' => $waktu_db,
            'metode' => $metode_db
        ];
    }
    $stmt->close();
    return $data;
}

$filteredAbsensi = getAbsensiData($conn, $startDate, $endDate);
$conn->close();

// Mulai membuat HTML tabel
$htmlOutput = '';
$htmlOutput .= '<h3 class="text-sm font-semibold text-gray-700 mb-3">Laporan Tanggal ' . htmlspecialchars($startDate) . ' s/d ' . htmlspecialchars($endDate) . '</h3>';
$htmlOutput .= '<table class="w-full text-left text-xs table-auto border-collapse">';
$htmlOutput .= '<thead><tr class="bg-gray-100 text-gray-600"><th class="px-2 py-2 border w-1/2">Nama</th><th class="px-2 py-2 border w-1/6">Status</th><th class="px-2 py-2 border w-1/3">Keterangan</th></tr></thead>';
$htmlOutput .= '<tbody>';

if (!empty($filteredAbsensi) && !isset($filteredAbsensi['error'])) {
    foreach ($filteredAbsensi as $row) {
        $status_class = 'text-gray-600';
        if ($row['keterangan'] === 'izin') {
            $status_text = 'Izin';
            $status_class = 'text-yellow-600';
        } elseif ($row['keterangan'] === 'terlambat') {
            $status_text = 'Terlambat';
            $status_class = 'text-orange-600';
        } elseif ($row['status'] === 'berhasil') {
            $status_text = 'Hadir';
            $status_class = 'text-green-600';
        } else {
            $status_text = 'Alpha';
            $status_class = 'text-red-600';
        }

        $htmlOutput .= '<tr class="hover:bg-gray-50 align-top">';
        $htmlOutput .= '<td class="px-2 py-2 border"><span class="block font-semibold text-gray-800">' . htmlspecialchars($row['nama_lengkap']) . '</span><span class="block text-gray-500 text-[10px]">' . htmlspecialchars($row['kelas']) . '</span></td>';
        $htmlOutput .= '<td class="px-2 py-2 border font-semibold"><span class="' . $status_class . '">' . $status_text . '</span></td>';
        $htmlOutput .= '<td class="px-2 py-2 border"><div><span class="block font-semibold text-gray-700">' . date('d M Y H:i', strtotime($row['waktu'])) . '</span><span class="block text-gray-500 text-[10px]">' . htmlspecialchars($row['metode']) . '</span></div></td>';
        $htmlOutput .= '</tr>';
    }
} else {
    $htmlOutput .= '<tr><td colspan="3" class="px-2 py-2 text-center text-gray-500">Tidak ada data absensi untuk periode ini.</td></tr>';
}

$htmlOutput .= '</tbody>';
$htmlOutput .= '</table>';

echo $htmlOutput;
?>