<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Get the form data
  // $id = $_POST['id'];
  $name = $_POST['name'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $confirm_password = $_POST['confirm_password'];
  $welcome = $_POST['welcome'];
  $sign_up_message = $_POST['sign_up_message'];
  $total = $_POST['total'];
  $successful = $_POST['successful'];
  $gain = $_POST['gain'];
  $token_inv  = $_POST['token_inv'];
  $token_pro = $_POST['token_pro'];
  $token_target = $_POST['token_target'];
  $token_withd = $_POST['token_withd'];

  // Validate the form data
  if (empty($name) || empty($email) || empty($password) || empty($confirm_password)) {
    echo 'Please fill in all fields';
  } elseif ($password != $confirm_password) {
    echo 'Passwords do not match';
  } else {
    // Hash the password
    // $hashed_password = password_hash($password, PASSWORD_DEFAULT);

    // Connect to the database
    $conn = mysqli_connect('localhost', 'root', '', 'token');

    // Check if the email is already in use
    $sql = "SELECT id FROM users WHERE email = '$email'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
      echo 'Email already in use';
    } else {
      // Insert the user data into the database
      $sql = "INSERT INTO users (name, email, password, welcome, sign_up_message, total, successful, gain, token_inv, token_pro, token_target, token_withd) VALUES ('$name', '$email', '$password', '$welcome', '$sign_up_message', '$total', '$successful', '$gain', '$token_inv', '$token_pro', '$token_target', '$token_withd')";
      mysqli_query($conn, $sql);

    // Retrieve the auto-generated user ID
    $user_id = mysqli_insert_id($conn);

    // Redirect the user to the user dashboard
    header("Location: welcome/index.php?id=$user_id");
    }

    mysqli_close($conn);
  }
}
?>



$user = mysqli_query($con, "SELECT * FROM users WHERE email = '$email' AND password = '$password'");
if ($user && mysqli_num_rows($user) > 0) {
$row = mysqli_fetch_assoc($user);
$_SESSION['user_id'] = $row['id'];
if ($row['confirmed'] == 1) {
    // Allow the user to access their account
    // Redirect to the original index page
    header("Location: welcome/index.php?id=" . $row["id"]);
    exit();
} else {
    // Redirect the user to a page asking them to confirm their account
    header("Location: welcome/confirm.php?id=" . $row["id"]);
    exit();
}
}



$sql = "INSERT INTO users (name, email, password, welcome, sign_up_message, total, successful, gain, token_inv, token_pro, token_target, token_withd, user_confirmed) VALUES ('$name', '$email', '$password', '$welcome', '$sign_up_message', '$total', '$successful', '$gain', '$token_inv', '$token_pro', '$token_target', '$token_withd', '$user_confirmed')";
