<?php
require 'db_config.php';

// Check if an ID is provided
if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Fetch existing data
    $stmt = $conn->prepare("SELECT fullname, email, phone, gender FROM sub_user WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
    } else {
        echo "No sub-user found with this ID.";
        exit();
    }
    $stmt->close();
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['id'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $gender = $_POST['gender'];
    $password = $_POST['password'];

    // Hash the password before storing it
    $hashed_password = password_hash($password, PASSWORD_BCRYPT);

    // Update the database
    $stmt = $conn->prepare("UPDATE sub_user SET fullname = ?, email = ?, phone = ?, gender = ?, password = ? WHERE id = ?");
    $stmt->bind_param("sssssi", $fullname, $email, $phone, $gender, $hashed_password, $id);

    if ($stmt->execute()) {
        echo "Sub-user information updated successfully.";
        header("Location: Disubuser.php"); // Redirect to the management page
        exit();
    } else {
        echo "Error updating record: " . $stmt->error;
    }

    $stmt->close();
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Sub User</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        .form-container {
            margin: auto;
            max-width: 500px;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        form input, form select {
            width: 100%;
            padding: 8px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        form button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>Edit Sub User</h2>
        <form action="edit_sub_user.php" method="post">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input type="text" name="fullname" value="<?php echo htmlspecialchars($user['fullname'], ENT_QUOTES, 'UTF-8'); ?>" required>
            <input type="email" name="email" value="<?php echo htmlspecialchars($user['email'], ENT_QUOTES, 'UTF-8'); ?>" required>
            <input type="text" name="phone" value="<?php echo htmlspecialchars($user['phone'], ENT_QUOTES, 'UTF-8'); ?>" required>
            <select name="gender" required>
                <option value="Male" <?php if($user['gender'] == 'Male') echo 'selected'; ?>>Male</option>
                <option value="Female" <?php if($user['gender'] == 'Female') echo 'selected'; ?>>Female</option>
            </select>
            <input type="password" name="password" placeholder="New Password" required>
            <button type="submit">Update</button>
        </form>
    </div>
</body>
</html>
