<?php



//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer as PHPMailer;
use PHPMailer\PHPMailer\SMTP as SMTP;
use PHPMailer\PHPMailer\Exception as Exception;
require 'vendor/autoload.php';
// require 'dashboard/bolt/hmailer.php';
require 'admin/connect.php';
//Load Composer's autoloader

class Hmailer {
    public $mail;
    function __construct($Remail){
// Instantiation and passing `true` enables exceptions
    $this->mail = new PHPMailer(true);
    //Server settings
    $this->mail->SMTPDebug = SMTP::DEBUG_OFF;                      //Enable verbose debug output
    $this->mail->isSMTP();                                            //Send using SMTP
    $this->mail->Host       = 'tradxbits.com';                    // Set the SMTP server to send through
    $this->mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $this->mail->Username   = 'info@tradxbits.com';                     // SMTP username
    $this->mail->Password   = 'TradxBits@2023';  
    $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $this->mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $this->mail->setFrom('info@tradxbits.com', 'TradxBits');
    $this->mail->addAddress($Remail);               
    $this->mail->addReplyTo('info@tradxbits.com', 'TradxBits');
    }
    public function alert_mail($cl_email, $cl_unique){
    $this->mail->isHTML(true);                                 
    $this->mail->Subject = 'EMAIL CONFIRMATION -  TradxBits';
    $this->mail->Body    = $this->confirm_mail_msg($cl_email, $cl_unique);

    try{$this->mail->send();
        return true;
    } catch (Exception $e) {
        return false;
    }
    }
   public function confirm_mail_msg($cl_email, $cl_unique){
       return '<div style="background-color:white; color:black; display:block; width:100%; padding:10px; font-size:0.85em">
       <div style="display:flex; justify-content:center;width:100%"><img src="https://www.tradxbits.com/img/favicon.png" height="50px"></div>
       <h3 style="color:brown; text-align:center;">Welcome to TradxBits</h3>
       <p style="text-align:center"><strong>Confirm Your Registration</strong></p>
       <p>
       You\'re now part of the growing online marketplace that connects you to TradxBits around the world. 
       <br>Please verify your email address below to complete your registration.
       </p>
       <a href="https://www.tradxbits.com/confirmmail.php?confirmtoken='.$cl_email.'&unique='.$cl_unique.'" target="blank" style="color:white; background:blue; text-decoration:none;padding:5px;margin:5px">Confirm my email address</a>
       <p>
       This is an important step in setting up your new account because it allows you to access the full range of TradxBits Services.
       </p>
       Thanks,
       <br>
       <small><em>The TradxBits Team</em></small>
       </div>';
   }
}
// $option = ['cost'=>12,];
        // echo $customer_new_password = password_hash('Prot@#2022', PASSWORD_BCRYPT, $option);

$login_err = '';
$signup_err = '';
if(isset($_POST['loginbtn'])){
	
		$email = trim($_POST['emailadd']);
		$pass = trim($_POST['password']);

		function test_input($cxn, $data) {
			$data = stripslashes($data);
			$data = htmlspecialchars($data);
			$data = $cxn->real_escape_string($data);
			return $data;
        }
        
		$pass = test_input($cxn, $pass);
		$email = test_input($cxn, $email);

		$email_ad = explode('@',$email);
		if($email_ad[0] == 'admin'){
			if(filter_var($email, FILTER_VALIDATE_EMAIL)){
				$select_data = "SELECT * FROM admin_access WHERE admin_email = '$email'";
				$runs = mysqli_query($cxn, $select_data);
			if(!mysqli_num_rows($runs) > 0){
                $login_err = '<div class="alert alert-danger">Unable to Login, try again later!</div>';
				}
			else {
			$getnow = mysqli_fetch_assoc($runs); 
			if (password_verify($pass, $getnow['admin_hash_password'])){
			   session_start();
			   $_SESSION['goodlogin'] = true;
			   $_SESSION['unique_admin_id'] = $getnow['admin_email'];
			   header('location:/admin');
			}else
			{ 
				$login_err = '<div class="alert alert-danger">Incorrect Email or Password, check & try again later!</div>';
			}}
			}
		}else{
		if(filter_var($email, FILTER_VALIDATE_EMAIL)){
			$select_data = "SELECT * FROM new_customer WHERE customer_email = '$email'";
			$runs = mysqli_query($cxn, $select_data);
			if(!mysqli_num_rows($runs) > 0){
			$login_err = '<div class="alert alert-danger">User Account does not Exist, Register an account!</div>';
			}
		else {
		$getnow = mysqli_fetch_assoc($runs);
		if($getnow['active_account'] == 'false'){
		    	$login_err = '<div class="alert alert-warning">Your account has been suspended! Please contact support to rectify the issue!</div>';
		}else{
		if (password_verify($pass, $getnow['customer_password'])){
			session_start();
			$_SESSION['goodloginistrue'] = true;
			$_SESSION['customer_email'] = $getnow['customer_email'];
			$isLoginSuccessful = true;
// 	header('location:/user/dashboard');
		}else
        { 
		$login_err = '<div class="alert alert-danger">Incorrect Email or Password, check & try again later!</div>';
		}
		}
	}
		}
	}
}
if(isset($_POST['create_acct'])){
    $customer_email = trim($_POST['customer_email']);
    if(filter_var($customer_email, FILTER_VALIDATE_EMAIL)){
			$select_data = "SELECT * FROM new_customer WHERE customer_email = '$customer_email' AND active_account = 'true'";
			$runs = mysqli_query($cxn, $select_data);
			if(mysqli_num_rows($runs) > 0){
			$signup_err = '<div class="alert alert-warning">User account already exist, Click here to <a href="/login" style="color:black">Login!</a></div>';
			}else{
	//retrieve info from input								
	 $customer_fname = trim($_POST['customer_fname']);
	 $customer_lname = trim($_POST['customer_lname']);
	 
     $customer_password = trim($_POST['customer_password']);
     $customer_phone = trim($_POST['customer_phone']);
     $customer_country = trim($_POST['customer_country']);
          $referer = trim($_POST['referal_code']);
    //  $current_plan = $_POST['choose_plan'].'-'.$_POST['choose_plan'];
    //  $user_coin = $_POST['user_coin'];
     $demo_starter = 'null';
     $user_currency = 'USD-$';
     $refer_idx = 'null';
     $refer_id =  rand(0000,999999);
     $refer_bal = 0;
    //  $invest_amount = explode('-',$current_plan)[0];
    $customer_unique = 'NC'.date('Y').date('m').date('d').rand(100,9999);
    $investment_unique = 'IN'.date('Y').date('m').date('d').rand(100,9999);
    $hash_acct_password = $customer_password;
	$option = ['cost'=>12,];
	$hash_acct_password = password_hash($hash_acct_password, PASSWORD_BCRYPT, $option);
    $sendtoData = "INSERT INTO new_customer (customer_fname,customer_lname,customer_unique,customer_email,customer_password,customer_password_unhash,email_confirm_status,total_profit,customer_phone,customer_country,user_currency,active_account,demo_starter,demo_id,refer_bal,refer_id,user_coin)
    VALUES('$customer_fname','$customer_lname','$customer_unique','$customer_email','$hash_acct_password','$customer_password','no',5,'$customer_phone','$customer_country','$user_currency','true','$demo_starter','$investment_unique','$refer_bal','$refer_id','BTC')";
$querytoData = mysqli_query($cxn, $sendtoData);
$tnx = "TRX".rand(000000,9999999);
            $type = "Signup Bonus";
            $proof_url = 'null';
            $status = 'success';
            $method = 'System';
            $plan_end = false;
            $user_id = $customer_email;
            $amount = 5;
            $description = "Signup Bonus";
            $write_cmd = "INSERT INTO transactions (tnx,type,method,amount,final_amount,charge,description,plan_end,status,user_id,proof_url) VALUES 
                    ('$tnx','$type','$method','$amount',0,0,'$description','$plan_end','$status','$user_id','$proof_url')";
$querytoData = mysqli_query($cxn, $write_cmd);
if($querytoData && strlen($referer) > 0){

$getRemail = "SELECT * FROM new_customer WHERE refer_id = '$referer'";
$querytoData = mysqli_query($cxn, $getRemail);
if(mysqli_num_rows($querytoData) > 0){
   $RData = mysqli_fetch_assoc($querytoData); 
$sendtoData = "INSERT INTO referals (referer_id,referee_id,amount)VALUES('$referer','$refer_id','50')";
$querytoData = mysqli_query($cxn, $sendtoData);

$Remail = $RData['customer_email'];
$newRbal = $RData['total_profit'] + 50;


$updateRbal = "UPDATE new_customer SET total_profit = '$newRbal' WHERE customer_email = '$Remail'";
$querytoData = mysqli_query($cxn, $updateRbal);


$tnx = "TRX".rand(000000,9999999);
            $type = "Referal Bonus";
            $proof_url = 'null';
            $status = 'success';
            $method = 'System';
            $plan_end = false;
            $user_id = $Remail;
            $amount = 50;
            $description = "Referal Bonus";
            $write_cmd = "INSERT INTO transactions (tnx,type,method,amount,final_amount,charge,description,plan_end,status,user_id,proof_url) VALUES 
                    ('$tnx','$type','$method','$amount',0,0,'$description','$plan_end','$status','$user_id','$proof_url')";
$querytoData = mysqli_query($cxn, $write_cmd);


}
}
            // $hmailer = new Hmailer($customer_email);
			// $hmailer->alert_mail($customer_email,$customer_unique);
if(!$querytoData){
    $signup_err = '<div class="alert alert-danger" style="font-size: 1em;"><a class="alert-close"></a>Unable to Register, Please try again later!</div>';
}else{
			$signup_err = '<div class="alert alert-success" style="font-size: 1em;"><a class="alert-close"></a>Your Registration was Successful, Please login to validate your Account. <br>click here to <a href="/login" class="text-primary">Login</a></div>';
}
			}		    
			}
}

?>