<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $studentName = $_POST["studentName"];
    $std = $_POST["std"];
    $parentName = $_POST["parentName"];
    $numMembers = $_POST["numMembers"];
    $vehicleType = $_POST["vehicleType"];
    $parking = $_POST["parking"];
    $area = $_POST["area"];

    // Generate a random numeric unique ID
    $uniqueId = generateUniqueId();

    // Display the unique ID in a pop-up
    echo "<script>alert('Dear Carnival Participant, Thank you for registering! Your unique ID: $uniqueId Please make sure to keep this ID handy and present it at the time of entry to ensure smooth access to the carnival.We look forward to seeing you at the event! Best regards, CiS');</script>";

    // Save data to the database (assuming you have a MySQL database)
    $servername = "localhost";
    $username = "nifzdonk_user";
    $password = "s1w&afHHJnu3";
    $dbname = "nifzdonk_carnival";

    // Create a database connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check the connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Insert data into the database
    $sql = "INSERT INTO registrations (unique_id, student_name, std, parent_name, num_members, vehicle_type, parking, area)
            VALUES ('$uniqueId', '$studentName', '$std', '$parentName', '$numMembers', '$vehicleType', '$parking', '$area')";

    if ($conn->query($sql) === TRUE) {
        echo "Registration successful!";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    // Close the database connection
    $conn->close();
}

// Function to generate a random numeric unique ID
function generateUniqueId() {
    // This is a simple example; you may want to use a more robust method for production
    return rand(100000, 999999);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carnival Registration Form</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        form {
            max-width: 600px;
            margin: 0 auto;
        }
        label {
            display: block;
            margin-bottom: 8px;
        }
        input, select {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h2>Carnival Registration Form</h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <label for="studentName">Student's Name:</label>
        <input type="text" id="studentName" name="studentName" required>

        <label for="std">Standard/Class:</label>
        <input type="text" id="std" name="std" required>

        <label for="parentName">Parent's Name:</label>
        <input type="text" id="parentName" name="parentName" required>

        <label for="numMembers">Number of Members Coming:</label>
        <input type="number" id="numMembers" name="numMembers" min="1" required>

        <label for="area">Select your area:</label>
        <select id="area" name="area" onchange="" required>
        
            <option value="Jail Road">Jail Road</option>
            <option value="Konark Nagar">Konark Nagar</option>
            <option value="Park Side">Park Side</option>
            <option value="Bali Maharaj Mandir">Bali Maharaj Mandir</option>
            <option value="Dasak">Dasak</option>
            <option value="Narayan Bapu Nagar">Narayan Bapu Nagar</option>

            <option value="Nashik Road">Nashik Road</option>
            <option value="Dwarka">Dwarka</option>
            <option value="Ashoka Marg">Ashoka Marg</option>
            <option value="Indira Nagar">Indira Nagar</option>
            <option value="Pathardi Phata">Pathardi Phata</option>
            <option value="Mumbai Naka">Mumbai Naka</option>
        </select>

        <label for="parking">Your Alloted parking is :</label>
        <input type="text" id="parking" name="parking" value="" disabled>

        <label for="vehicleType">Type of Vehicle:</label>
        <select id="vehicleType" name="vehicleType" required>
            <option value="car">Car</option>
            <option value="bike">Bike</option>
            <option value="bus">Bus</option>
            <option value="other">Other</option>
        </select>

        <button type="submit">Submit</button>
    </form>
</body>
</html>
