<section>
  <h2 class="title-head" id="allresults">Updated Indian Satta Results</h2>
  <div class="card-box">

<?php
// 📅 Current Date 
$today = date('Y-m-d');

// 🎯 Fetch All Active Games
$gamesQuery = $db->query("SELECT * FROM games WHERE is_active = TRUE ORDER BY time_open");
$games = $gamesQuery->fetchAll(PDO::FETCH_ASSOC);

// 🔁 Loop through each game
foreach ($games as $game) {
    $gameId = $game['game_id'];
    $titleRaw = (string) ($game['title'] ?? '');
    $aliasRaw = (string) ($game['alias'] ?? '');
    $title = htmlspecialchars($titleRaw, ENT_QUOTES, 'UTF-8');
    $time_open = htmlspecialchars((string) ($game['time_open'] ?? ''), ENT_QUOTES, 'UTF-8');
    $time_close = htmlspecialchars((string) ($game['time_close'] ?? ''), ENT_QUOTES, 'UTF-8');
    $color = $game['color'] ?: 'blue';
    $highlight = $game['highlight'];
    $isHoliday = $game['is_holiday'];

    // 📦 Get today's result
    $stmt = $db->prepare("SELECT * FROM results WHERE game_id = ? AND draw_date = ?");
    $stmt->execute([$gameId, $today]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    // 🕗 If no result found for today, fallback to latest previous result
    if (!$result) {
        $stmt = $db->prepare("SELECT * FROM results WHERE game_id = ? AND draw_date < ? ORDER BY draw_date DESC LIMIT 1");
        $stmt->execute([$gameId, $today]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
    }

    // 🔢 Prepare result display
    $open = $result['open_digits'] ?? '';
    $close = $result['close_digits'] ?? '';
    $noteOpen = $result['note_open'] ?? '';
    $noteClose = $result['note_close'] ?? '';

    if ($isHoliday) {
        $finalResult = "<span style='animation: pulse 1s infinite;'>🎉 *-Holiday-* 🎉</span>";
    } elseif (!$open && !$close) {
        $finalResult = "<span style='font-style:italic; color: #555;'>Loading...</span>";
    } else {
        $sumOpen = $open ? array_sum(str_split($open)) % 10 : '';
        $sumClose = $close ? (array_sum(str_split($close)) % 10) . '-' : '';
        $finalResult = "$open-$sumOpen$sumClose$close";
    }

    $timeOpenFormatted = date("h:i A", strtotime($time_open));
    $timeCloseFormatted = date("h:i A", strtotime($time_close));
    $pageAlias = strtolower(trim($aliasRaw));
    if ($pageAlias === '') {
        $pageAlias = strtolower(trim($titleRaw));
    }
    $pageAlias = str_replace(' ', '-', $pageAlias);
    $jodiUrl = function_exists('cms_info_chart_url') ? cms_info_chart_url($pageAlias, 'jodi') : '/info-chart/' . $pageAlias . '-jodi';
    $panelUrl = function_exists('cms_info_chart_url') ? cms_info_chart_url($pageAlias, 'panel') : '/info-chart/' . $pageAlias . '-panel';

    // 🖼 Output game result block (Highlight logic included)
    if ($highlight) {
        echo "<div style='background-color:#fff049; padding:10px; border-radius: var(--radius-md); margin-bottom: 15px;'>";
        echo "  <article style='border: none; padding-bottom: 0;'>";
    } else {
        echo "  <article>";
    }

    echo "    <div class='txt-lg color-secondary'><span class='txt-running'>$title</span></div>";
    echo "    <div class='txt-lg'>$finalResult</div>";

    // Notes display if available
    if ($noteClose) {
        echo "    <div class='txt-sm' style='color:red;'>" . htmlspecialchars($noteClose) . "</div>";
    }
    if ($noteOpen) {
        echo "    <div class='txt-sm' style='color:red;'>" . htmlspecialchars($noteOpen) . "</div>";
    }

    echo "    <div class='txt-sm'>{$timeOpenFormatted} - {$timeCloseFormatted}</div>";
    echo "    <div style='text-align:center'><a href='/aapka-link-yahan-daalein' class='btn-gradient'>Click Here</a></div>";

    // Buttons logic (adjusted bottom position for highlighted items)
    if ($highlight) {
echo "    <div class='btn-chart left' style='bottom: -15px;'><a href='" . htmlspecialchars($jodiUrl, ENT_QUOTES, 'UTF-8') . "'>Jodi</a></div>";
echo "    <div class='btn-chart right' style='bottom: -15px;'><a href='" . htmlspecialchars($panelUrl, ENT_QUOTES, 'UTF-8') . "'>Panel</a></div>";
    } else {
echo "    <div class='btn-chart left'><a href='" . htmlspecialchars($jodiUrl, ENT_QUOTES, 'UTF-8') . "'>Jodi</a></div>";
echo "    <div class='btn-chart right'><a href='" . htmlspecialchars($panelUrl, ENT_QUOTES, 'UTF-8') . "'>Panel</a></div>";
    }

    echo "  </article>";

    if ($highlight) {
        echo "</div>";
    }
}
?>

  </div>

<style>
    /* Updated class name to match SET 1 structure */
    .txt-running {
    background: linear-gradient(270deg, #004abf, #ff0000, #00c853);
    background-size: 600% 600%;
    animation: textRun 5s linear infinite;

    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

@keyframes textRun {
    0% { background-position: 0% 50%; }
    100% { background-position: 100% 50%; }
}
</style>
</section>
