<?php
session_start(); // Start the session to store the user ID

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'];
	$user_confirmed = $_POST['user_confirmed'];

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

    // Connect to the database
    $conn = mysqli_connect('localhost', 'kvfxminr_token', 'qHj}Sa7k(_4f', 'kvfxminr_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) {
			$error = "Email already in use.";
    } else {
      // Insert the user data into the database
      $sql = "INSERT INTO users (name, email, password, user_confirmed, free, total, successful, gain, token_inv, token_pro, token_target, token_withd) VALUES ('$name', '$email', '$password', '$user_confirmed', '$free', '$total', '$successful', '$gain', '$token_inv', '$token_pro', '$token_target', '$token_withd')";
			// $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')";
      mysqli_query($conn, $sql);

    // Retrieve the auto-generated user ID
    // $user_id = mysqli_insert_id($conn);
		$sql = "SELECT id FROM users WHERE email = '$email'";
    $result = mysqli_query($conn, $sql);
		$row = mysqli_fetch_assoc($result);
		$_SESSION['user_id'] = $row['id'];

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

		$user = mysqli_query($conn, "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['user_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();
		}
	}

    mysqli_close($conn);
  }
}
?>

<!DOCTYPE html>
<html lang="zxx" class="js">
<head>
	<meta charset="utf-8">
<meta name="author" content="Softnio">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<!-- Fav Icon  -->
<link rel="shortcut icon" href="images/favicon.png">
<!-- Site Title  -->
<title>Register | ICO TOKENS FX</title>
<!-- Bundle and Base CSS -->
<link rel="stylesheet" href="assets/css/vendor.bundle.css?ver=1930">
<link rel="stylesheet" href="assets/css/style.css?ver=1930" id="changeTheme">
<!-- Extra CSS -->
<link rel="stylesheet" href="assets/css/theme.css?ver=1930">




</head>



    <body class="nk-body body-wider bg-light-alt">

	<div class="nk-wrap">

        <main class="nk-pages nk-pages-centered bg-theme">
            <div class="ath-container">
                <div class="ath-header text-center">
                    <a href="#" class="ath-logo"><img src="images/logoo.jpg" srcset="images/logoo.jpg" alt="logo"></a>
                </div>
                <div class="ath-body">
                    <h5 class="ath-heading title">Sign Up <small class="tc-default">Please enter your details to sign up and be part of our great community</small></h5>
										<?php if (isset($error)) : ?>
                     <p style="color: red;"><?php echo $error; ?></p>
                    <?php endif; ?>
                    <form method="post" action="page-register.php">
											<div class="field-item">
													<div class="">
															  <input type="hidden" name="id" class="" value="id" placeholder="id" required>
													</div>
											</div>
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="text" name="name" class="input-bordered" placeholder="Full Name" required>
                            </div>
                        </div>
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="email" name="email" class="input-bordered" placeholder="Email Address" required>
                            </div>
                        </div>
												<div class="field-item">
                            <div class="field-wrap">
                                <input type="text" class="input-bordered" placeholder="Country" required>
                            </div>
                        </div>
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="password" name="password" class="input-bordered" placeholder="Password" required>
                            </div>
                        </div>
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="password" name="confirm_password" class="input-bordered" placeholder="Confirm Password" required>
                            </div>
                        </div>
												<!-- <div class="">
														<div class="">
																  <input type="hidden" name="welcome" class="" value=" " placeholder="id" required>
														</div>
												</div> -->
												<div class="">
														<div class="">
																  <input type="hidden" name="free" value="" class="input-bordered" value="id" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="total" value="$0.00" class="" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="successful" value=" " class=""  placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="gain" value=" " class=""  placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="token_inv" value="$0.00" class="" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="token_pro" value="$0.00" class="" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="token_target" value="$0.00" class="" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="token_withd" value="$0.00" class="" placeholder="id" required>
														</div>
												</div>
												<div class="">
														<div class="">
																  <input type="hidden" name="user_confirmed" value="0" class="" placeholder="id" required>
														</div>
												</div>
                        <div class="field-item">
                            <!-- <input class="input-checkbox" id="agree-term-2" type="checkbox">
                            <label for="agree-term-2">I agree to Icos <a href="#">Privacy Policy</a> &amp; <a href="#">Terms</a>.</label> -->
                        </div>
                        <button class="btn btn-primary btn-block btn-md">Sign Up</button>
                    </form>
                    <!-- <div class="sap-text"><span>Or Sign Up With</span></div> -->

                    <!-- <ul class="btn-grp gutter-20px gutter-vr-20px">
                        <li class="col"><a href="#" class="btn btn-md btn-facebook btn-block"><em class="icon fab fa-facebook-f"></em><span>Facebook</span></a></li>
                        <li class="col"><a href="#" class="btn btn-md btn-google btn-block"><em class="icon fab fa-google"></em><span>Google</span></a></li>
                    </ul> -->
                </div>
                <div class="ath-note text-center tc-light">
                    Already have an account? <a href="page-login.php"> <strong>Sign in here</strong></a>
                </div>
            </div>
        </main>


				<footer class="nk-footer bg-theme-dark section-connect">
 					 <div class="section section-m pb-0 tc-light ov-h">

 							 <div class="nk-ovm shape-contain shape-center-top shape-p"></div>
 					 </div>
 					 <div class="section section-footer section-s tc-light bg-transparent">
 							 <div class="container">
 									 <!-- Block @s -->
 									 <div class="nk-block block-footer">
 											 <div class="row">
 													 <div class="col">
 															 <div class="wgs wgs-text text-center mb-3">
 																	 <div class="copyright-text copyright-text-s3 pdt-m">
 																			 <p><span class="d-sm-block">Copyright &copy; 2023, ICO Crypto. All Rights Resevered.</p>
 																	 </div>
 															 </div>
 													 </div><!-- .col -->
 											 </div><!-- .row -->
 									 </div><!-- .block @e -->
 							 </div>
 					 </div>
 			 </footer>

	</div>
	<div class="preloader"><span class="spinner spinner-round"></span></div>

	<!-- JavaScript -->
	<script src="assets/js/jquery.bundle.js?ver=1930"></script>
	<script src="assets/js/scripts.js?ver=1930"></script>
	<script src="assets/js/charts.js"></script>
</body>
</html>
