<?php
require_once '../config.php';

echo "<h2>Test Add Result Direct Submission</h2>";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "<h3>Form Data Received:</h3>";
    echo "<pre>" . print_r($_POST, true) . "</pre>";
    
    $result_date = $_POST['result_date'] ?? '';
    $game = $_POST['game'] ?? '';
    $result_number = $_POST['result_number'] ?? '';
    
    echo "<h3>Processed Values:</h3>";
    echo "<p>Date: " . htmlspecialchars($result_date) . "</p>";
    echo "<p>Game: " . htmlspecialchars($game) . "</p>";
    echo "<p>Number: " . htmlspecialchars($result_number) . "</p>";
    
    if (!empty($result_date) && !empty($game) && !empty($result_number)) {
        // Check if results table exists
        $check_table = mysqli_query($link, "SHOW TABLES LIKE 'results'");
        if (mysqli_num_rows($check_table) == 0) {
            $create_table = "CREATE TABLE results (
                id INT AUTO_INCREMENT PRIMARY KEY,
                result_date DATE NOT NULL,
                game VARCHAR(50) NOT NULL,
                result_number VARCHAR(10) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )";
            mysqli_query($link, $create_table);
            echo "<p>✅ Results table created</p>";
        }
        
        // Insert the result
        $sql = "INSERT INTO results (result_date, game, result_number) VALUES (?, ?, ?)";
        if ($stmt = mysqli_prepare($link, $sql)) {
            mysqli_stmt_bind_param($stmt, 'sss', $result_date, $game, $result_number);
            if (mysqli_stmt_execute($stmt)) {
                echo "<p style='color: green;'>✅ <strong>SUCCESS!</strong> Result added successfully!</p>";
                
                // Show the inserted data
                $id = mysqli_insert_id($link);
                echo "<p>Inserted ID: $id</p>";
                
                // Show recent results
                echo "<h3>Recent Results:</h3>";
                $recent = mysqli_query($link, "SELECT * FROM results ORDER BY created_at DESC LIMIT 5");
                if ($recent && mysqli_num_rows($recent) > 0) {
                    echo "<table border='1' style='border-collapse: collapse;'>";
                    echo "<tr><th>ID</th><th>Date</th><th>Game</th><th>Number</th><th>Created</th></tr>";
                    while ($row = mysqli_fetch_assoc($recent)) {
                        echo "<tr>";
                        echo "<td>{$row['id']}</td>";
                        echo "<td>{$row['result_date']}</td>";
                        echo "<td>{$row['game']}</td>";
                        echo "<td>{$row['result_number']}</td>";
                        echo "<td>{$row['created_at']}</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                }
            } else {
                echo "<p style='color: red;'>❌ Error: " . mysqli_stmt_error($stmt) . "</p>";
            }
            mysqli_stmt_close($stmt);
        } else {
            echo "<p style='color: red;'>❌ Prepare error: " . mysqli_error($link) . "</p>";
        }
    } else {
        echo "<p style='color: red;'>❌ Missing required fields</p>";
    }
}
?>

<h3>Test Form:</h3>
<form method="post" style="border: 1px solid #ccc; padding: 20px; margin: 20px 0;">
    <p>
        <label>Date:</label><br>
        <input type="date" name="result_date" value="<?php echo date('Y-m-d'); ?>" required>
    </p>
    <p>
        <label>Game:</label><br>
        <select name="game" required>
            <option value="">Select Game</option>
            <option value="disawar">Disawar</option>
            <option value="dl_bazaar">DL Bazaar</option>
            <option value="shri_ganesh">Shri Ganesh</option>
            <option value="faridabad">Faridabad</option>
            <option value="ghaziabad">Ghaziabad</option>
            <option value="radhey_radhey">Radhey Radhey</option>
            <option value="gali">Gali</option>
        </select>
    </p>
    <p>
        <label>Result Number:</label><br>
        <input type="text" name="result_number" placeholder="Enter number" required>
    </p>
    <p>
        <button type="submit">Test Add Result</button>
    </p>
</form>

<hr>
<p><a href="../admin/admin-dashboard.php">← Back to Admin Dashboard</a></p>