<?php 

include_once('../classes/database.class.php');

if (isset($_POST['action'])) {
	# code...
	$action = $_POST['action'];

	if ($action == 'forgot') {
		# code...
		$email = $_POST['email'];

		$db = new database();
        $stmt = $db->db->prepare("SELECT *, count(id) as total FROM users WHERE email = :email");
   		 $stmt->execute(array(':email' => $email));
        $userDetails = $stmt->fetch(PDO::FETCH_OBJ);
       // print_r($userDetails);
        if($userDetails->total == 1){
        	
        	$uniqid = uniqid();

        	$headers = "From: Invata.io" ."\r\n";	

        	$forgoturl = "https://invata.io?email=".$email."&code=".$uniqid;

        	$message = "Your Forgot Password Code Is ".$uniqid." Or Click this link ".$forgoturl;

        	if (mail($email, "Forgot Password Code", $message, $headers)) {
        		# code...



    			$stmt_forgot = $db->db->prepare("INSERT INTO forgot(email, code) VALUES (:email, :code)");

    			$stmt_forgot->execute(array(':email'=>$email, ':code'=>$uniqid));

        		echo "sent";


        	}else{

        		echo "notsent";
        	}

    	}
        else{

        	echo "notfound";

        }


	}elseif ($action == 'checkcode') {
		# code...

		$email = $_POST['email'];
		$code = $_POST['code'];

		$db = new database();
        $stmt = $db->db->prepare("SELECT *, count(id) as total FROM forgot WHERE email = :email AND code = :code");
   		$stmt->execute(array(':email' => $email, ':code'=>$code));
        $userDetails = $stmt->fetch(PDO::FETCH_OBJ);

        if ($userDetails->total > 0) {
        	# code...
        	echo "found";
        }else{
        	echo "notfound";
        }

	}elseif ($action == 'changepassword') {
		# code...
		$email = $_POST['email'];
		$pass = $_POST['pass'];

		$db = new database();
		$stmt = $db->db->prepare("UPDATE users SET password = :pass WHERE email = :email");
		$stmt->execute(array(':pass'=>$pass, ':email'=>$email));

		$stmt_delete = $db->db->prepare("DELETE FROM forgot WHERE email = :newemail");
		$stmt_delete->execute(array(':newemail'=>$email));

		echo "done";

	}elseif ($action == 'checkpt') {
        # code...
            

        $pt = $_POST['pt'];


        $db = new database();
        $stmt = $db->db->prepare("SELECT *, count(id) as total FROM payments WHERE token = :token");
        $stmt->execute(array(':token' => $pt));
        $tokDetails = $stmt->fetch(PDO::FETCH_OBJ);

        $result = array();

        if($tokDetails->total >= 1){

            $tokenQuery = $db->db->prepare("SELECT * FROM payments WHERE token = '$pt'");
            $tokenQuery->execute();
            $tokenFetch = $tokenQuery->fetch(PDO::FETCH_OBJ);
            $tokenStatus = $tokenFetch->status;

            $result = array('result'=>'found', 'status'=>$tokenStatus);

        }else{

            $result = array('result'=>'notfound');
        }

        print_r(json_encode($result));


    }elseif ($action == 'checkemail') {
        # code...
        $email = $_POST['email'];

        $db = new database();
        $stmt = $db->db->prepare("SELECT *, count(id) as total FROM users WHERE email = '$email'");
        $stmt->execute();
        $EmailDetails = $stmt->fetch(PDO::FETCH_OBJ);


        if ($EmailDetails->total >= 1) {
            # code...
             $result = array('result'=>'found');        

        }else{
             
             $result = array('result'=>'notfound');
        }

        print_r(json_encode($result));

    }elseif ($action == 'register') {
        # code...
        $fn = $_POST['fn'];
        $ln = $_POST['ln'];
        $email = $_POST['email'];
        $pw = $_POST['pw'];
        $uid = rand(10000, 100000);
        $token = $_POST['pt'];

        $fullname = $fn." ".$ln;
        $ph = $_POST['ph'];
        $pcar = $_POST['pcar'];

        $db = new database();
        $stmt = $db->db->prepare("INSERT INTO users (name, email, uid, password, accountId, auth, account_balance, starting_balance, active, daily_goal, monthly_goal, annual_goal, oanda_user_account, bearer_auth, phone, pcarrier) VALUES ('$fullname', '$email', '$uid', '$pw', '734010', '', '0.00', '0.00', '0', '0.00', '0.00', '0.00', '', '', '$ph', '$pcar')");

        if ($stmt->execute()) {
            # code...

            $stmt2 = $db->db->prepare("UPDATE payments SET status = '1' WHERE token = '$token'");

            if ($stmt2->execute()) {
                # code...

                $result = array('result'=>'success');

            }else{

                $result = array('result'=>'failed');
            }


        }else{

         $result = array('result'=>'failed');      
        
        }

        print_r(json_encode($result));
    }
}

?>