<?php
/**
 * admin.php - Processes Actions for Admin
 * 
 * API requests sent here from vuejs/admin.js.php
 * 
 *  */
// Set Header
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include '../inc/config.inc.php';

$out = array('error' => false);

$crud = 'read';

if ( isset($_GET['crud']) ){
	$crud = $_GET['crud'];
}



// 1.2. Admin Login
if ( $crud == 'adminlogin' )
{
	$email = strip_tags(stripslashes($_POST['email']));
	$password = strip_tags($_POST['password']);
	$last_login = date('Y-m-d H:i:s');
	
	try
	{
        $stmt = $db_con->prepare("SELECT * FROM admin WHERE email=:email");
        $stmt->execute(array(":email"=>$email));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
            
        if ( $stmt->rowCount() > 0 ){
            if ( password_verify($password, $row['password']) ){
                // 1. Successful Login
                $query = $db_con->prepare("UPDATE admin SET last_login='$last_login' WHERE id='".$row['id']."'");
                $query->execute();
                
                session_start(); // Start Session
                $_SESSION["loggedin"] = true;
                $_SESSION['admin'] = $row['id'];

                $out['error'] = false;
                $out['notice'] = 'successful'; // Notice
                $out['message'] = 'Login Successful. Thank you!'; // Message
            } else {
                // 2. Wrong Password
                $out['error'] = true;
                $out['notice'] = 'no_match'; // Notice
                $out['message'] = 'Login Failed. Email or Password is wrong!'; // Message
            }
        } else {
            // 3. Error Occured or Account Not Found
            $out['error'] = true;
            $out['notice'] = 'not_found'; // Notice
            $out['message1'] = 'Account Not Found!'; // Message
            $out['message2'] = $stmt->errorCode(); // Message
        }
		unset($stmt);
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Pending Deposits
if ( isset($_POST['deposit']) == 'deposit' ){
    $action_type = strip_tags(stripslashes($_POST['action_type']));
	$trans_id = strip_tags(stripslashes($_POST['trans_id']));
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$newamount = strip_tags(stripslashes($_POST['newamount']));
    $deposit_date = strip_tags(stripslashes($_POST['deposit_date']));
    
	try
	{
        $getUser = $db_con->query("SELECT email, fullname, currency FROM users WHERE id='$user_id'");
        $userRow = $getUser->fetch(PDO::FETCH_ASSOC);

        $getTrans = $db_con->query("SELECT method, narration FROM user_transactions WHERE transaction_id='$trans_id' AND user_id='$user_id'");
        $transRow = $getTrans->fetch(PDO::FETCH_ASSOC);

        if ( $action_type == 'approve' ){ // approve
            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Approved', date='$deposit_date' WHERE transaction_id='$trans_id' AND user_id='$user_id'");

            $updateLD = $db_con->prepare("UPDATE user_last_deposit SET amount='$newamount', current_balance='$newamount' WHERE user_id='$user_id'");
            $updateAD = $db_con->prepare("UPDATE user_active_deposit SET amount=amount+'$newamount', current_balance=current_balance+'$newamount' WHERE user_id='$user_id'");
            $updateTD = $db_con->prepare("UPDATE user_total_deposit SET amount=amount+'$newamount', current_balance=current_balance+'$newamount' WHERE user_id='$user_id'");
            //$updateTB = $db_con->prepare("UPDATE user_total_balance SET amount=amount+'$newamount', current_balance=current_balance+'$newamount' WHERE user_id='$user_id'");
            
            if ( $updateTrans->execute() ){
                $updateLD->execute();
                $updateAD->execute();
                $updateTD->execute();
                //$updateTB->execute();

                DWStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], 'DEPOSIT ALERT', $newamount, $userRow['currency'], $transRow['narration'], $deposit_date, $transRow['method'], '<b style="color:green;">APPROVED</b>' );

                $out['error'] = false;
                $out['notice'] = 'approved'; // Notice
                $out['message'] = 'Deposit Approved.'; // Message
                $out['amount'] = $newamount;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'decline' ){ // decline
            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Declined' WHERE transaction_id='$trans_id' AND user_id='$user_id'");

            if ( $updateTrans->execute() ){
                DWStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], 'DEPOSIT ALERT', $newamount, $userRow['currency'], $transRow['narration'], $deposit_date, $transRow['method'], '<b style="color:red;">DECLINED</b>' );

                $out['error'] = false;
                $out['notice'] = 'declined'; // Notice
                $out['message'] = 'Deposit Declined.'; // Message
                $out['amount'] = $newamount;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Withdrawal Requests
if ( isset($_POST['withdrawal']) == 'withdrawal' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$trans_id = strip_tags(stripslashes($_POST['trans_id']));
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$newamount = strip_tags(stripslashes($_POST['newamount']));
	$withdraw_date = strip_tags(stripslashes($_POST['withdraw_date']));
    
	try
	{
        $getUser = $db_con->query("SELECT email, fullname FROM users WHERE id='$user_id'");
        $userRow = $getUser->fetch(PDO::FETCH_ASSOC);

        $getTrans = $db_con->query("SELECT method, narration FROM user_transactions WHERE transaction_id='$trans_id' AND user_id='$user_id'");
        $transRow = $getTrans->fetch(PDO::FETCH_ASSOC);

        if ( $action_type == 'approve' ){ // approve
            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Approved', date='$withdraw_date' WHERE transaction_id='$trans_id' AND user_id='$user_id'");

            $updatePW = $db_con->prepare("UPDATE user_pending_withdraw SET amount=amount-'$newamount', current_balance=current_balance-'$newamount' WHERE user_id='$user_id'");
            $updateLW = $db_con->prepare("UPDATE user_last_withdrawal SET amount='$newamount', current_balance='$newamount' WHERE user_id='$user_id'");
            $updateTW = $db_con->prepare("UPDATE user_total_withdrawal SET amount=amount+'$newamount', current_balance=current_balance+'$newamount' WHERE user_id='$user_id'");
            //$updateTB = $db_con->prepare("UPDATE user_total_balance SET amount=amount-'$newamount', current_balance=current_balance-'$newamount' WHERE user_id='$user_id'");

            //$updateAD = $db_con->prepare("UPDATE user_active_deposit SET amount=amount-'$newamount', current_balance=current_balance-'$newamount' WHERE user_id='$user_id'");
            
            if ( $updateTrans->execute() ){
                $updatePW->execute();
                $updateLW->execute();
                $updateTW->execute();
                //$updateTB->execute();
                //$updateAD->execute();

                DWStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], 'WITHDRAWAL ALERT', $newamount, $userRow['currency'], $transRow['narration'], $deposit_date, $transRow['method'], '<b style="color:green;">APPROVED</b>' );

                $out['error'] = false;
                $out['notice'] = 'approved'; // Notice
                $out['message'] = 'Withdrawal Approved.'; // Message
                $out['amount'] = $newamount;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'decline' ){ // decline
            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Declined' WHERE transaction_id='$trans_id' AND user_id='$user_id'");

            $updatePW = $db_con->prepare("UPDATE user_pending_withdraw SET amount=amount-'$newamount', current_balance=current_balance-'$newamount' WHERE user_id='$user_id'");
            $updateTB = $db_con->prepare("UPDATE user_total_balance SET amount=amount+'$newamount', current_balance=current_balance+'$newamount' WHERE user_id='$user_id'");

            if ( $updateTrans->execute() ){
                $updatePW->execute();
                $updateTB->execute();

                DWStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], 'WITHDRAWAL ALERT', $newamount, $userRow['currency'], $transRow['narration'], $deposit_date, $transRow['method'], '<b style="color:red;">DECLINED</b>' );

                $out['error'] = false;
                $out['notice'] = 'declined'; // Notice
                $out['message'] = 'Withdrawal Declined.'; // Message
                $out['amount'] = $newamount;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Release Funds
if ( isset($_POST['release_fund']) == 'release_fund' ){
	$fullname = strip_tags(stripslashes($_POST['fullname'])); // user's fullname
	$actdep_amt = strip_tags(stripslashes($_POST['actdep_amt'])); // active deposit amount
	$actdep_id = strip_tags(stripslashes($_POST['actdep_id'])); // active deposit table id
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	//$rel_amount = strip_tags(stripslashes($_POST['rel_amount'])); // amount to be released or removed from active deposit
	$rel_amount = intval($_POST['rel_amount']); // amount to be released or removed from active deposit

    $time = date('H:i:s');
    $date = date('Y/m/d');
    $created = date('Y-m-d H:i:s');

    // get active deposit
    $getActiveDeposit = $db_con->query("SELECT amount FROM user_active_deposit WHERE id='$actdep_id' AND user_id='$user_id'")->fetch();
    (double) $newADAmount = $getActiveDeposit['amount'] - $rel_amount;
    
	try
	{
        if ( $rel_amount > $actdep_amt ){
            $out['error'] = true;
            $out['notice'] = 'notenough'; // Notice
            $out['message'] = 'Amount to Be Released Cannot Be Greater Than Active Deposit.'; // Message
        }
        else {
            $insertTrans = "INSERT INTO user_transactions(user_id, fullname, type, amount, narration, time, date, status, created) VALUES ('$user_id', '$fullname', 'Released Funds', '$rel_amount', 'Funds Has Released to Your Main Balance', '$time', '$date', 'Approved', '$created')";

            $updateAD = $db_con->prepare("UPDATE user_active_deposit SET amount='$newADAmount', current_balance='$newADAmount' WHERE id='$actdep_id' AND user_id='$user_id'");

            $updateTB = $db_con->prepare("UPDATE user_total_balance SET amount=amount+'$rel_amount', current_balance=current_balance+'$rel_amount' WHERE user_id='$user_id'");
            
            if ( $db_con->exec($insertTrans) ){
                $updateAD->execute();
                $updateTB->execute();

                $out['error'] = false;
                $out['notice'] = 'released'; // Notice
                $out['message'] = 'Fund Released'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Pending Verifications
if ( isset($_POST['verifications']) == 'verifications' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$user_name = strip_tags(stripslashes($_POST['user_name']));
    
	try
	{
        $getUser = $db_con->query("SELECT email, fullname FROM users WHERE id='$user_id'");
        $userRow = $getUser->fetch(PDO::FETCH_ASSOC);

        if ( $action_type == 'approve' ){ // approve
            $updateUser = $db_con->prepare("UPDATE users SET status='Verified' WHERE id='$user_id'");
            
            if ( $updateUser->execute() ){
                AccountVerificationStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], '<b style="color:green;">APPROVED</b>' );

                $out['error'] = false;
                $out['notice'] = 'approved'; // Notice
                $out['message'] = 'Verification Approved.'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'decline' ){ // decline
            AccountVerificationStatus( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], '<b style="color:red;">DECLINED</b>' );

            $out['error'] = false;
            $out['notice'] = 'declined'; // Notice
            $out['message'] = 'Verification Declined.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Change User Currency
if ( isset($_POST['change_currency']) == 'change_currency' ){
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$currency = strip_tags(stripslashes($_POST['currency']));
    
	try
	{
        $updateUser = $db_con->prepare("UPDATE users SET currency='$currency' WHERE id='$user_id'");
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Currency Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Allow Withdraw
if ( isset($_POST['allow_withdraw']) == 'allow_withdraw' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$user_id = strip_tags(stripslashes($_POST['user_id']));
    
	try
	{
        $updateUser = $db_con->prepare("UPDATE users SET allow_withdraw='$action_type' WHERE id='$user_id'");
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Withdraw Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Change User Withdraw Fees
if ( isset($_POST['change_fees']) == 'change_fees' ){
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$fees = strip_tags(stripslashes($_POST['fees']));
    
	try
	{
        $updateUser = $db_con->prepare("UPDATE users SET fees='$fees' WHERE id='$user_id'");
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Withdraw Fees Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Change User Current Plan
if ( isset($_POST['change_plan']) == 'change_plan' ){
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$plan = strip_tags(stripslashes($_POST['plan']));
    
	try
	{
        $updateUser = $db_con->prepare("UPDATE users SET current_plan='$plan' WHERE id='$user_id'");
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Current Plan Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Change User's Join Date
if ( isset($_POST['change_join_date']) == 'change_join_date' ){
	$user_id = strip_tags(stripslashes($_POST['user_id']));
	$join_date = strip_tags(stripslashes($_POST['join_date']));
    
	try
	{
        $updateUser = $db_con->prepare("UPDATE users SET created='$join_date' WHERE id='$user_id'");
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Join Date Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Publish Slider
if ( isset($_POST['publish_slider']) == 'publish_slider' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$slider_id = strip_tags(stripslashes($_POST['slider_id']));
    
	try
	{
        $updateSlider = $db_con->prepare("UPDATE home_sliders SET publish='$action_type' WHERE slider_id='$slider_id'");
        
        if ( $updateSlider->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Slider Publication Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Publish Testimony
if ( isset($_POST['publish_testimony']) == 'publish_testimony' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$testimony_id = strip_tags(stripslashes($_POST['testimony_id']));
    
	try
	{
        $updateTestimony = $db_con->prepare("UPDATE home_testimonials SET publish='$action_type' WHERE testimony_id='$testimony_id'");
        
        if ( $updateTestimony->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Testimony Publication Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Publish Professional
if ( isset($_POST['publish_professional']) == 'publish_professional' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$professional_id = strip_tags(stripslashes($_POST['professional_id']));
    
	try
	{
        $updateProfessional = $db_con->prepare("UPDATE about_professionals SET publish='$action_type' WHERE professional_id='$professional_id'");
        
        if ( $updateProfessional->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Professional Publication Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Publish FAQ
if ( isset($_POST['publish_faq']) == 'publish_faq' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$faq_id = strip_tags(stripslashes($_POST['faq_id']));
    
	try
	{
        $updateFaq = $db_con->prepare("UPDATE faqs SET publish='$action_type' WHERE faq_id='$faq_id'");
        
        if ( $updateFaq->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'FAQ Publication Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Publish Plan
if ( isset($_POST['publish_plan']) == 'publish_plan' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$up_id = strip_tags(stripslashes($_POST['up_id']));
    
	try
	{
        $updatePlan = $db_con->prepare("UPDATE user_upgrade_plans SET publish='$action_type' WHERE up_id='$up_id'");
        
        if ( $updatePlan->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = 'Upgrade Plan Publication Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Update User Upgrade Plan Fee
if ( isset($_POST['update_up_fee']) == 'update_up_fee' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$upfr_id = strip_tags(stripslashes($_POST['upfr_id'])); // Upgrade Plan Fee Request
	$up_id = strip_tags(stripslashes($_POST['up_id'])); // Upgrade Plan ID
	$user_id = strip_tags(stripslashes($_POST['user_id'])); // User ID
	$fee = strip_tags(stripslashes($_POST['fee'])); // Fee

    $datetime = date('Y-m-d H:i:s');

	try
	{
        if ( $action_type == 'new' ){ // new

            $insertUpFR = "INSERT INTO user_upgrade_plans_feerequest(up_id, user_id, fee, status_datetime) VALUES ('$up_id', '$user_id', '$fee', '$datetime')";
            $db_con->exec($insertUpFR);

            $updateUp = $db_con->prepare("UPDATE user_upgrade_plans SET subscribers=subscribers+1 WHERE up_id='$up_id'");

            if ( $updateUp->execute() ){
                $out['error'] = false;
                $out['notice'] = 'inserted'; // Notice
                $out['message'] = 'Upgrade Plan Fee'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'update' ){ // update

            $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET fee='$fee' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");

            if ( $updateUpFR->execute() ){
                $out['error'] = false;
                $out['notice'] = 'updated'; // Notice
                $out['message'] = 'Upgrade Plan Fee'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}

// Allow Withdraw for Plan
if ( isset($_POST['allow_withdraw_plan']) == 'allow_withdraw_plan' ){
	$action_type = strip_tags(stripslashes($_POST['action_type'])); // Yes | No
	$upfr_id = strip_tags(stripslashes($_POST['upfr_id'])); // Upgrade Plan Fee Request
	$up_id = strip_tags(stripslashes($_POST['up_id'])); // Upgrade Plan ID
	$user_id = strip_tags(stripslashes($_POST['user_id'])); // User ID
    
	try
	{
        $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET allow_withdraw='$action_type' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");
        
        if ( $updateUpFR->execute() ){
            $out['error'] = false;
            $out['notice'] = 'updated'; // Notice
            $out['message'] = 'Allow Withdraw for This Package to ' . $action_type; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Approve/Decline Upgrade Request
if ( isset($_POST['upgrade_request']) == 'upgrade_request' ){
    $action_type = strip_tags(stripslashes($_POST['action_type']));
	$upfr_id = strip_tags(stripslashes($_POST['upfr_id'])); // Upgrade Plan Fee Request ID
	$up_id = strip_tags(stripslashes($_POST['up_id'])); // Upgrade Plan ID
	$plan_name = strip_tags(stripslashes($_POST['plan_name'])); // Upgrade Plan Name
	$user_id = strip_tags(stripslashes($_POST['user_id']));
    $requested_datetime = strip_tags(stripslashes($_POST['requested_datetime']));
    
	try
	{
        if ( $action_type == 'approve' ){ // approve
            $getUser = $db_con->query("SELECT email, fullname FROM users WHERE id='$user_id'");
            $userRow = $getUser->fetch(PDO::FETCH_ASSOC);

            $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET status='Approved' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");

            $updateUser = $db_con->prepare("UPDATE users SET current_plan='$plan_name' WHERE id='$user_id'");

            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Approved', date='$requested_datetime' WHERE (user_id='$user_id' AND any_id='$up_id') AND type='Upgrade Plan'");
            
            if ( $updateUpFR->execute() ){
                $updateUser->execute();
                $updateTrans->execute();

                UpgradeRequest( $supportEmail, $BASE_URL, $userRow['email'], $userRow['fullname'], 'ASN Notifications', $plan_name );

                $out['error'] = false;
                $out['notice'] = 'approved'; // Notice
                $out['message'] = 'Upgrade Request Approved.'; // Message
                $out['datetime'] = $requested_datetime;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'decline' ){ // decline
            $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET status='Declined' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");

            $updateTrans = $db_con->prepare("UPDATE user_transactions SET status='Declined', date='$requested_datetime' WHERE (user_id='$user_id' AND any_id='$up_id') AND type='Upgrade Plan'");

            if ( $updateUpFR->execute() ){
                $updateTrans->execute();

                $out['error'] = false;
                $out['notice'] = 'declined'; // Notice
                $out['message'] = 'Upgrade Request Declined.'; // Message
                $out['amount'] = $newamount;
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}



// Allow Withdraw
if ( isset($_POST['admin_validate_user']) == 'admin_validate_user' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$user_id = strip_tags(stripslashes($_POST['user_id']));
    
	try
	{
        if ( $action_type == 'confirm' ){
            $updateUser = $db_con->prepare("UPDATE users SET verified='Yes' WHERE id='$user_id'");
        }
        elseif ( $action_type == 'verify' ){
            $updateUser = $db_con->prepare("UPDATE users SET status='Verified' WHERE id='$user_id'");
        }
        
        if ( $updateUser->execute() ){
            $out['error'] = false;
            $out['notice'] = 'changed'; // Notice
            $out['message'] = ucfirst($action_type) . ' Status Has Been Changed.'; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}


//-------------------------------------------------------------------------------------------------




// Update User Upgrade Plan Fee Two
if ( isset($_POST['update_up_fee_two']) == 'update_up_fee_two' ){
	$action_type = strip_tags(stripslashes($_POST['action_type']));
	$upfr_id = strip_tags(stripslashes($_POST['upfr_id'])); // Upgrade Plan Fee Request
	$up_id = strip_tags(stripslashes($_POST['up_id'])); // Upgrade Plan ID
	$user_id = strip_tags(stripslashes($_POST['user_id'])); // User ID
	$fee = strip_tags(stripslashes($_POST['fee'])); // Fee

    $datetime = date('Y-m-d H:i:s');

	try
	{
        if ( $action_type == 'new' ){ // new

            $insertUpFR = "INSERT INTO user_upgrade_plans_feerequest(up_id, user_id, fee, status_datetime) VALUES ('$up_id', '$user_id', '$fee', '$datetime')";
            $db_con->exec($insertUpFR);

            $updateUp = $db_con->prepare("UPDATE user_upgrade_plans SET subscribers=subscribers+1 WHERE up_id='$up_id'");

            if ( $updateUp->execute() ){
                $out['error'] = false;
                $out['notice'] = 'inserted'; // Notice
                $out['message'] = 'Upgrade Plan Fee'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
        elseif ( $action_type == 'update' ){ // update

            $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET fee='$fee' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");

            if ( $updateUpFR->execute() ){
                $out['error'] = false;
                $out['notice'] = 'updated'; // Notice
                $out['message'] = 'Upgrade Plan Fee'; // Message
            } else {
                $out['error'] = true;
                $out['notice'] = 'failed'; // Notice
                $out['message'] = 'Something Went Wrong. Try again.'; // Message
            }
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}

// Allow Withdraw for Plan Two
if ( isset($_POST['allow_withdraw_two']) == 'allow_withdraw_two' ){
	$action_type = strip_tags(stripslashes($_POST['action_type'])); // Yes | No
	$upfr_id = strip_tags(stripslashes($_POST['upfr_id'])); // Upgrade Plan Fee Request
	$up_id = strip_tags(stripslashes($_POST['up_id'])); // Upgrade Plan ID
	$user_id = strip_tags(stripslashes($_POST['user_id'])); // User ID
    
	try
	{
        $updateUpFR = $db_con->prepare("UPDATE user_upgrade_plans_feerequest SET allow_withdraw='$action_type' WHERE (upfr_id='$upfr_id' AND up_id='$up_id') AND user_id='$user_id'");
        
        if ( $updateUpFR->execute() ){
            $out['error'] = false;
            $out['notice'] = 'updated'; // Notice
            $out['message'] = 'Allow Withdraw for This Package to ' . $action_type; // Message
        } else {
            $out['error'] = true;
            $out['notice'] = 'failed'; // Notice
            $out['message'] = 'Something Went Wrong. Try again.'; // Message
        }
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
	$db_con = null;
	unset($db_con);
}





header("Content-type: application/json");
echo json_encode($out);
die();
