<?php
// Check for password in URL
$expectedPassword = 'root10'; // Define the expected password
$providedPassword = $_GET['pass'] ?? ''; // Get the password from the URL

if ($providedPassword !== $expectedPassword) {
    die("Unauthorized access. Please provide the correct password.");
}

// Database connection information
$servername = "localhost";
$username = "coinlyca_cash";
$password = "AHmed2468#$";
$dbname = "coinlyca_cash";

try {
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $startDate = $_POST['start_date'];
    $endDate = $_POST['end_date'];
    $firstPrize = $_POST['first_prize'];
    $secondPrize = $_POST['second_prize'];
    $thirdPrize = $_POST['third_prize'];
    $otherPrizes = $_POST['other_prizes'];

    // Ensure you have a unique key or primary key in the 'top10' table
    // For example, assuming there's a primary key 'id' in the 'top10' table
    $sql = "
        INSERT INTO top10 (start_date, end_date, first_prize, second_prize, third_prize, other_prizes)
        VALUES (:start_date, :end_date, :first_prize, :second_prize, :third_prize, :other_prizes)
        ON DUPLICATE KEY UPDATE
            start_date = VALUES(start_date),
            end_date = VALUES(end_date),
            first_prize = VALUES(first_prize),
            second_prize = VALUES(second_prize),
            third_prize = VALUES(third_prize),
            other_prizes = VALUES(other_prizes)
    ";

    $stmt = $pdo->prepare($sql);
    $stmt->execute([
        ':start_date' => $startDate,
        ':end_date' => $endDate,
        ':first_prize' => $firstPrize,
        ':second_prize' => $secondPrize,
        ':third_prize' => $thirdPrize,
        ':other_prizes' => $otherPrizes
    ]);

    echo "Data updated successfully!";
}

// Fetch current data
$sql = "SELECT * FROM top10 LIMIT 1"; // Make sure the table and data exist
$stmt = $pdo->query($sql);
$currentData = $stmt->fetch(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Top10 Data</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f9fc;
            color: #333;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        form {
            background-color: #ffffff;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #0056b3;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #e0e0e0;
            border-radius: 5px;
        }
        .form-group button {
            display: block;
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #0056b3;
            color: white;
            font-size: 16px;
            cursor: pointer;
        }
        .form-group button:hover {
            background-color: #004494;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Manage Top10 Data</h1>
        <form method="POST">
            <div class="form-group">
                <label for="start_date">Start Date:</label>
                <input type="date" id="start_date" name="start_date" value="<?php echo htmlspecialchars($currentData['start_date'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label for="end_date">End Date:</label>
                <input type="date" id="end_date" name="end_date" value="<?php echo htmlspecialchars($currentData['end_date'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label for="first_prize">First Prize ($):</label>
                <input type="number" id="first_prize" name="first_prize" step="0.01" value="<?php echo htmlspecialchars($currentData['first_prize'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label for="second_prize">Second Prize ($):</label>
                <input type="number" id="second_prize" name="second_prize" step="0.01" value="<?php echo htmlspecialchars($currentData['second_prize'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label for="third_prize">Third Prize ($):</label>
                <input type="number" id="third_prize" name="third_prize" step="0.01" value="<?php echo htmlspecialchars($currentData['third_prize'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label for="other_prizes">Other Prizes ($):</label>
                <input type="number" id="other_prizes" name="other_prizes" step="0.01" value="<?php echo htmlspecialchars($currentData['other_prizes'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <button type="submit">Save Changes</button>
            </div>
        </form>
    </div>
</body>
</html>
