<?php
// modules/get_katim_stats.php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/app.php';
checkAuth();

$conn = getConnection();
$days = (int)($_GET['days'] ?? 7);

$data = [
    'labels' => [],
    'pasien' => [],
    'tenaga' => []
];

for ($i = $days - 1; $i >= 0; $i--) {
    $date = date('Y-m-d', strtotime("-$i days"));
    $result = $conn->query("
        SELECT COALESCE(SUM(total_pasien), 0) as total_pasien,
               COALESCE(SUM(jumlah_tenaga), 0) as total_tenaga
        FROM laporan_katim
        WHERE tanggal = '$date'
    ");
    $row = $result->fetch_assoc();
    
    $data['labels'][] = date('d/m', strtotime($date));
    $data['pasien'][] = (int)$row['total_pasien'];
    $data['tenaga'][] = (int)$row['total_tenaga'];
}

header('Content-Type: application/json');
echo json_encode($data);
?>