<?php
header('Content-Type: application/json; charset=utf-8');
include 'connection.php';

try {
    $drawNumber = $_POST['Draw_Number'] ?? '';

    if (empty($drawNumber)) {
        throw new Exception("Draw number required");
    }

    $sql = "SELECT Winner_Position_Number, Winner_Bond_Number 
            FROM Past_Draw_History 
            WHERE Draw_Number = ? 
            ORDER BY Winner_Position_Number ASC";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $drawNumber);
    $stmt->execute();
    $result = $stmt->get_result();

    $winners = [];
    while ($row = $result->fetch_assoc()) {
        $winners[] = [
            "Winner_Position_Number" => $row['Winner_Position_Number'],
            "Winner_Bond_Number"     => $row['Winner_Bond_Number']
        ];
    }

    echo json_encode([
        "status" => "success",
        "data"   => $winners
    ]);

} catch (Exception $e) {
    echo json_encode([
        "status" => "error",
        "message" => $e->getMessage()
    ]);
}
?>
