<?php

$actionParam = new USER();
//user class stored in variable actionParam 
if(isset($_GET['referral']))
{
    $referral = $_GET['referral'];
}

if(isset($_POST['btn-reg']))
{
 $fullname = trim($_POST['fullname']);
 $email = trim($_POST['email']);
 $refemail = trim($_POST['refemail']);
 $state = trim($_POST['state']);
 $country = trim($_POST['country']);
 $phone = trim($_POST['phone']);
 $wallet = trim($_POST['wallet']);
 $password = trim($_POST['password']);
 $account_id = USER::AlphaNumeric(5);
 $date = date("d/m/Y");
 $uemail = $email;
 
 //check if referrer email exist
 if(!empty($refemail))
 {
	 $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:remail");
     $stmt->execute(array(":remail"=>$refemail));
     $rrow = $stmt->fetch(PDO::FETCH_ASSOC);
     if($stmt->rowCount() == 0)
       {
         header("Location: register?referral-email-error");
         exit;
       }
 }

 //check if user email already exist 
 $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:email");
 $stmt->execute(array(":email"=>$email));
 $row = $stmt->fetch(PDO::FETCH_ASSOC);

 if($stmt->rowCount() > 0)
   {
     header("Location: register?register-email-error");
     exit;
   }
  else
   {
      //check if user accountID exist
	  $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE accountID=:acctid");
      $stmt->execute(array(":acctid"=>$account_id));
      $row = $stmt->fetch(PDO::FETCH_ASSOC);
	  if($stmt->rowCount() > 0) 
      {
        //if it exist create a new account_id
        $account_id = USER::AlphaNumeric(5);
      }
	   
			  if($actionParam->register_user($fullname,$email,$refemail,$state,$country,$phone,$wallet,$password,$account_id,$date))
              {
                 $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:email");
                 $stmt->execute(array(":email"=>$email));
                 $row = $stmt->fetch(PDO::FETCH_ASSOC);
                  
                 $id = base64_encode($row['userID']);
                  
				 $message = '
				 <img src=  http://sescentigate.com/pip.png alt= Sescentigate><br><br>
                 Hello '.$fullname.',<br><br> Your registration on Sescentigate was successful. We are very glad to have you on board with us. Thank you.<br>
                 Best Regards;<br>
                 Sescentigate Team.
                 ';

                 $subject = "Registration successful";

                 $actionParam->send_mail($email,$message,$subject);
                  
                 // send mail to referrer
                 if($refemail != ''){
                     $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:email");
                     $stmt->execute(array(":email"=>$refemail));
                     $row = $stmt->fetch(PDO::FETCH_ASSOC);
                     
                     $message = '
                      <img src=  http://sescentigate.com/pip.png alt= Sescentigate><br><br>
                     Hello '.$row['fullname'].',<br> '.$fullname.' just registered as your downline.Keep earning as you refer your friends and colleagues.
                     <br>
                      Best Regards;<br>
                    Sescentigate Team.
                     
                     ';

                     $subject = "New referral";
                     $email = $refemail;
                     $actionParam->send_mail($email,$message,$subject);
                 }
				  
                 header("Location: login?email=$uemail&password=$password");
                 exit;
              } 
       
   }
}
?>
