<?php
require 'db_config.php';

if (isset($_GET['id'])) {
    $user_id = $_GET['id'];

 // Fetch user data from the database
    $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_assoc();

    if ($user) {
        // Display edit form
        ?>

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Edit User</title>
            <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
            <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 400px;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 15px;
        }

        .input-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .input-group input,
        .input-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .input-group input[type="radio"] {
            width: auto;
            margin-right: 5px;
        }

        .input-group label[for^="material"] {
            display: inline-block;
            margin-right: 10px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
        </head>
        <body>
            
            <form action="update_user.php" method="post">
                <h2>Edit User Details</h2>
                <input type="hidden" name="id" value="<?php echo $user_id; ?>">
                
                <div class="input-group">
                    <label for="fullname">Full Name</label>
                    <input type="text" id="fullname" name="fullname" value="<?php echo $user['fullname']; ?>" required>
                </div>
                <div class="input-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" value="<?php echo $user['email']; ?>" required>
                </div>
                <div class="input-group">
                    <label for="phone">Phone</label>
                    <input type="tel" id="phone" name="phone" value="<?php echo $user['phone']; ?>" required>
                </div>
                <div class="input-group">
                    <label for="payment_status">Payment Status</label>
                    <select id="payment_status" name="payment_status" required>
                        <option value="Paid" <?php if ($user['payment_status'] == 'Paid') echo 'selected'; ?>>Paid</option>
                        <option value="Not Paid" <?php if ($user['payment_status'] == 'Not Paid') echo 'selected'; ?>>Not Paid</option>
                    </select>
                </div>
                <div class="input-group">
                    <label for="hotel">Hotel</label>
                    <select id="hotel" name="hotel" required>
                        <option value="Ekhaya" <?php if ($user['hotel'] == 'Ekhaya') echo 'selected'; ?>>Ekhaya</option>
                        <option value="Nkopola" <?php if ($user['hotel'] == 'Nkopola') echo 'selected'; ?>>Nkopola</option>
                        <option value="Sunbird" <?php if ($user['hotel'] == 'Sunbird') echo 'selected'; ?>>Sunbird</option>
                    </select>
                </div>
                <div class="input-group">
                    <label for="institution">Institution</label>
                    <input type="text" id="institution" name="institution" value="<?php echo $user['institution']; ?>" required>
                </div>
                <div class="input-group">
                    <label for="membership_type">Membership Type</label>
                    <select id="membership_type" name="membership_type" required>
                        <option value="Non-Member" <?php if ($user['membership_type'] == 'Non-Member') echo 'selected'; ?>>Non-Member</option>
                        <option value="Member" <?php if ($user['membership_type'] == 'Member') echo 'selected'; ?>>Member</option>
                    </select>
                </div>
                
            <div class="input-group">
                <label>Promotion Material</label><br>
                <input type="checkbox" id="material1" name="promotion_material[]" value="T-shirt" <?php if (in_array('T-shirt', $promotion_material)) echo 'checked'; ?>> <label for="material1">T-shirt</label><br>
                <input type="checkbox" id="material2" name="promotion_material[]" value="Power Bank" <?php if (in_array('Power Bank', $promotion_material)) echo 'checked'; ?>> <label for="material2">Power Bank</label><br>
                <input type="checkbox" id="material3" name="promotion_material[]" value="Bag" <?php if (in_array('Bag', $promotion_material)) echo 'checked'; ?>> <label for="material3">Bag</label><br>
            </div>

                
                <button type="submit">Update</button>
            </form>
        </body>
        </html>

        <?php
    } else {
        echo "User not found.";
    }

    $stmt->close();
}

$conn->close();
?>
