<?php


session_start();
include 'database/config.php';
// initializing variables

// connect to the database
// REGISTER USER
  // receive all input values from the form
  $fname = mysqli_real_escape_string($link, $_POST['fname']);
  $lname = mysqli_real_escape_string($link, $_POST['lname']);
 
  $phone = mysqli_real_escape_string($link, $_POST['phone']);
  
  $country = mysqli_real_escape_string($link, $_POST['country']);
  $currency = mysqli_real_escape_string($link, $_POST['currency']);

  
  $email = mysqli_real_escape_string($link, $_POST['email']);
  $account_type = mysqli_real_escape_string($link, $_POST['currency']);
  $username = mysqli_real_escape_string($link, $_POST['username']);
  $password = mysqli_real_escape_string($link, $_POST['password']);
  $confirm_password = mysqli_real_escape_string($link, $POST['confirm_password']);
  

  $emailer = $_POST['email'];
  $usernamer = $_POST['username'];

  
  
 $sql = "select * from  user where username = '$usernamer'";
  $lqs = "select * from  user where email = '$emailer'";

  
  $q = mysqli_query($link , $sql);
  
    $r = mysqli_query($link , $lqs);

   
 
  if( mysqli_num_rows( $q ) > 0 )
  {
      $error[] = "An Account With This Username Already Exists";
      
  }elseif( mysqli_num_rows( $r ) > 0) {
        $error[] = "Email Already in Use, If This Is You Please Login";
        
  }else{
           // form validation: ensure that the form is correctly filled ...
         // by adding (array_push()) corresponding error unto $errors array
          
    
         $hash = $password;
         $randnum = uniqid('ID');
         $_SESSION['uid'] = $randnum;
         
         $activation = uniqid();
         $_SESSION['activation'] = $activation;
         
         $q1 = rand(1231,7879);
         $q2 = rand(1231,7879);
         $q3 = rand(1231,7879);
         $q4 = rand(1231,7879);

           $query = "INSERT INTO user ( fname, lname, phone, country, email, username, currency, password, activation, accounttype, uid, q1, q2, q3, q4) 
      	    	  VALUES ( '$fname', '$lname', '$phone', '$country', '$email', '$username', '$currency', '$hash', '$activation', '$account_type', '$randnum', '$q1', '$q2', '$q3', '$q4')";
           $results = mysqli_query($link, $query);
           $_SESSION['email'] = $email;
            $to = "$emailer, support@cryptotalfxt.com";
         $subject = "NEW ACCOUNT REGISTERED ON CRYPTOTALFXT";
         
        
         $message = "
             <meta name='viewport' content='width=device-width, initial-scale=1.0'>
         <div style='background-color: #4c516d; padding: 20px'>
         <img src='https://cryptotalfxt.com/logo.svg' alt='...' style='display: block; margin-left: auto; margin-right: auto; width: 50%'>
         <h1 style='font-size: 15px'>Hello ".$fname . $lname.", Thank you for choosing CRYPTOTALFXT as your trading platform.</h1></br>
         <p>Your Trading account has been successfully created. Make sure to keep your login details safe for future references.For safety and security, never share your login details or password with anyone.</p></br></div>
         <div style='background-color: #4c516d; padding: 5px; text-align: center'>
         
         
         <a href=\"cryptotalfxt.com/login.php?activation=" . $_SESSION['activation'] . "&type=active\" style='color: white; text-align: center; padding: 20px; background-color: #32CD32; border-radius: 5px'>VERIFY EMAIL</a>
         </div>
         <div style='background-color: #4c516d; padding: 20px'>
         
         <p>A large list of tradeable assets and Stocks are available, From Apple, Amazon, Tesla and Many more, You can monitor the growth of your investment on your trading dashboard, with ease and convinience.</p>
         
         <p>Should you have any questions or concerns, please contact us via support@cryptotalfxt.com with your email or visit our contact page or use the live chat in our website.</p>
         
            <p>You’re just a few steps away from trading with us. Happy Trading from all of us at CRYPTOTALFXT LTD.</br>Regards, cryptotalfxt.com.</p>
         </div>
         
         </div>";
        
         
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


   $_SESSION['name'] = "Hello ".$username." Your Account Has Been Registered, Please Check Your Email For Your Verification link to Verify Your Account";
         $_SESSION["email"] = $Email;
        							$_SESSION["loggedin"] = "^*@#[;K";
           header("Location: signin.html");
           echo 'Saved!';
           exit();

          
  }
  
  if(count($error) > 0)
  {
      $_SESSION['ERROR'] = $error;
      header("location: signup.html");
      exit;
  }
  

 
  




?>