<?php
require 'db_config.php';
header('Content-Type: application/json');

$response = [];
$type = $_GET['type'] ?? 'all'; // Allow specific table reload or all

// Fetch Attendance Data
if ($type === 'all' || $type === 'attendance') {
    $attendance_sql = "SELECT id, user_id, checkin_time, participant_type, fullname FROM attendance ORDER BY id DESC";
    $attendance_result = $conn->query($attendance_sql);
    $attendance_html = "";
    if ($attendance_result && $attendance_result->num_rows > 0) {
        while ($row = $attendance_result->fetch_assoc()) {
            $attendance_html .= "<tr>";
            $attendance_html .= "<td>{$row['id']}</td>";
            $attendance_html .= "<td>{$row['user_id']}</td>";
            $attendance_html .= "<td>{$row['checkin_time']}</td>";
            $attendance_html .= "<td>{$row['participant_type']}</td>";
            $attendance_html .= "<td>{$row['fullname']}</td>";
            $attendance_html .= "</tr>";
        }
    } else {
        $attendance_html .= "<tr><td colspan='5'>No attendance records found.</td></tr>";
    }
    $response['attendance'] = $attendance_html;
}

echo json_encode($response);
?>