<?php
// countdown.php
require_once 'config.php';
checkLogin();

$stmt = $db->prepare("SELECT result_time_start, result_time_end FROM settings LIMIT 1");
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);

$result_time_start = strtotime($data['result_time_start']);
$result_time_end = strtotime($data['result_time_end']);
$now = time();

$status = "";

if ($now < $result_time_start) {
    // Masih countdown
    $diff = $result_time_start - $now;
    $hours = floor($diff / 3600);
    $minutes = floor(($diff % 3600) / 60);
    $seconds = $diff % 60;
    $status = sprintf("Countdown: %02d:%02d:%02d", $hours, $minutes, $seconds);
} elseif ($now >= $result_time_start && $now <= $result_time_end) {
    // Dalam proses perhitungan
    $status = "Calculation";
} else {
    // Waktu sudah habis, cek apakah result sudah ditambahkan
    $stmt = $db->prepare("SELECT COUNT(*) as total FROM lottery_results WHERE market = 'Bandung'");
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($result['total'] > 0) {
        $status = "Result Added";
    } else {
        $status = "Waiting for market update";
    }
}

echo $status;
?>
