<?php
header('Content-Type: application/json');
require_once __DIR__ . "/../../../app/config/koneksi.php";
date_default_timezone_set('Asia/Jakarta');

$tanggal = date("Y-m-d");

$q = mysqli_query($koneksi, "SELECT * FROM jamaah WHERE tanggal='$tanggal' LIMIT 1");

if (!$q) {
    echo json_encode(["error" => mysqli_error($koneksi)]);
    exit;
}

$data = mysqli_fetch_assoc($q);

if (!$data) {
    $data = [
        "subuh" => 0,
        "dzuhur" => 0,
        "ashar" => 0,
        "maghrib" => 0,
        "isya" => 0,
        "diluar_waktu_sholat" => 0
    ];
}

echo json_encode([
    "subuh" => (int)$data['subuh'],
    "dzuhur" => (int)$data['dzuhur'],
    "ashar" => (int)$data['ashar'],
    "maghrib" => (int)$data['maghrib'],
    "isya" => (int)$data['isya'],
    "total" => (
        (int)$data['subuh'] +
        (int)$data['dzuhur'] +
        (int)$data['ashar'] +
        (int)$data['maghrib'] +
        (int)$data['isya']
    ),
    "luar_waktu" => (int)$data['diluar_waktu_sholat']
]);