<?php
$host = 'localhost';
$user = 'ahmeuesz_dino';
$pwd  = 'X39IW2ho3^5h6d_5';
$db   = 'ahmeuesz_myapp';
$conn = mysqli_connect($host, $user, $pwd, $db);

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name']);
    $email   = trim($_POST['email']);
    $contact = ''; // not used here
    $password = trim($_POST['password']);

    // file
    $profile_name = isset($_FILES['profile_image']['name']) ? $_FILES['profile_image']['name'] : '';
    $profile_tmp  = isset($_FILES['profile_image']['tmp_name']) ? $_FILES['profile_image']['tmp_name'] : '';

    if ($name === '' || $email === '' || $password === '' || $profile_name === '') {
        $msg = "<div class='error'>⚠️ Please fill all the entries.</div>";
    } else {
        // create uploads dir if not exists
        if (!is_dir('uploads')) {
            mkdir('uploads', 0777, true);
        }

        // check duplicate email
        $check = mysqli_query($conn, "SELECT id FROM image_table WHERE email='$email'");
        if ($check && mysqli_num_rows($check) > 0) {
            $msg = "<div class='error'>❌ Email already exists! Please use another one.</div>";
        } else {
            // save file with unique prefix
            $safe_name = time() . '_' . preg_replace('/[^A-Za-z0-9._-]/', '_', basename($profile_name));
            $target = 'uploads/' . $safe_name;

            if (move_uploaded_file($profile_tmp, $target)) {
                // insert user
                $sql = "INSERT INTO image_table (name,email,password,profile_image) VALUES ('$name','$email','$password','$safe_name')";
                if (mysqli_query($conn, $sql)) {
                    header('Location: login.php?registered=1');
                    exit();
                } else {
                    $msg = "<div class='error'>❌ Registration failed. Please try again.</div>";
                }
            } else {
                $msg = "<div class='error'>❌ Image upload failed. Please try again.</div>";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Registration</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <h1 class="page-title">REGISTRATION FORM</h1>

    <?php if ($msg) echo $msg; ?>

    <form class="reg-form" action="" method="post" enctype="multipart/form-data" novalidate>
      <div class="input-group">
        <label>Name</label>
        <input type="text" name="name" placeholder="Your full name" value="<?php echo isset($name) ? htmlspecialchars($name) : ''; ?>">
      </div>

      <div class="input-group">
        <label>Email</label>
        <input type="email" name="email" placeholder="you@example.com" value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>">
      </div>

      <div class="input-group">
        <label>Password</label>
        <input type="password" name="password" placeholder="Choose a password">
      </div>

      <div class="input-group">
        <label>Upload your profile photo</label>
        <div class="file-upload">
          <input type="file" name="profile_image" id="profile_image" accept="image/*">
          <label for="profile_image" class="file-label">📷 Choose Photo</label>
        </div>
      </div>

      <div style="display:flex;justify-content:center;">
        <input type="submit" class="btn btn-submit" value="Submit">
      </div>
    </form>

    <div class="controls" style="margin-top:18px;text-align:center;">
      <a class="showcode-btn" href="login.php">Already have an account? Login</a>
    </div>
            <?php include 'youtube.php'; ?>
  </div>
</body>
</html>
