<?php
include 'db.php';
if (isset($_POST['submit'])) {
    $prefix = $_POST['prefix'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $image_name = $_FILES['image']['name'];
    $tmp_name = $_FILES['image']['tmp_name'];
    $target = "uploads/" . basename($image_name);
    move_uploaded_file($tmp_name, $target);
    $sql = "INSERT INTO user_record (prefix, fullname, email, phone, image)
            VALUES ('$prefix', '$fullname', '$email', '$phone', '$image_name')";
    mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="" method="post" enctype="multipart/form-data">
    <label>Prefix:</label>
    <select name="prefix" required>
        <option>Mr</option>
        <option>Mrs</option>
        <option>Dr</option>
        <option>Ms</option>
        <option>Prof</option>
    </select><br>
    <label>Full Name:</label>
    <input type="text" name="fullname" required><br>
    <label>Email:</label>
    <input type="email" name="email" required><br>
    <label>Phone Number:</label>
    <input type="text" name="phone" required><br>
    <label>Profile Picture:</label>
    <input type="file" name="image" required><br>
    <button type="submit" name="submit">Submit</button>
</form>
<hr>
<h2>Registered Users</h2>
<?php
$result = mysqli_query($conn, "SELECT * FROM user_record ORDER BY id DESC");
while ($row = mysqli_fetch_assoc($result)) {
    $img = "uploads/" . $row['image'];
    $email_link = "mailto:" . $row['email'];
    $call_link = "tel:" . $row['phone'];
    $whatsapp_link = "https://wa.me/" . $row['phone'];
    echo "
    <div>
        <img src='$img'>
        <div>
            <h3>{$row['prefix']} {$row['fullname']}</h3>
            <p><b>Email:</b> <a href='$email_link'>{$row['email']}</a></p>
            <p><b>Phone:</b> {$row['phone']}</p>
        </div>
        <div>
            <a href='$call_link'>📞 Call</a>
            <a href='$whatsapp_link' target='_blank'>💬 WhatsApp</a>
            <a href='edit.php?id={$row['id']}'>✏️ Edit</a>
            <a href='delete.php?id={$row['id']}'>🗑️ Delete</a>
        </div>
    </div>
    ";
}
?>
</body>
</html>