<?php

$user = help::get_user( $email );

### [FUNDING] ###

$_POST = help::sanitize($_POST);
	
if( isset($_POST['fund']) ):
	
	$charges = (float)$_POST['amount'] * 0.01 * (float)$settings->get('charges');
	
	$data = array(
		"tx_no" => strtolower(help::keygen(10, false)),
		"tx_type" => $_POST['type'],
		"amount" => (float)$_POST['amount'],
		"date" => date("Y/m/d"),
		"description" => $_POST['description'],
		"to_accno" => $user['acctno'],
		"account" => $user['acctno'],
		"status" => "SUCCESS",
		"comments" => '',
		"charges" => round($charges,2),
		"method" => 'local',
		"bank_name" => $settings->get('bname'),
		"beneficiary" => "{$user['fname']} {$user['lname']}",
		"sender_name" => $settings->get('bname'),
		"country" => $user['country']
	);
	
	foreach( $data as $key => $value ) $data[$key] = $link->real_escape_string($value);
	
	$SQL = sQuery::insert( 'tbl_transaction', $data );
	
	if( $data['tx_type'] == 'debit' ) {
	
		$debit_amount = $data['amount'] + $data['charges'];
		$new_balance = (float)$user['balance'] - $debit_amount;
		if( $new_balance < 0 ) $fund_msg = "User account balance is insufficient";
		
	} else {
	
		$credit_amount = $data['amount'] - $data['charges'];
		$new_balance = (float)$user['balance'] + $credit_amount;
		
	};
	
	if( empty($fund_msg) ) {
		
		$FUND_SQL = sQuery::update( "tbl_users", array( "balance" => round($new_balance, 2) ), "email='{$user['email']}'" );
		
		$fund_updated = $link->query( $FUND_SQL );
		
		if( $fund_updated ) {
			
			$tx_updated = $link->query( $SQL );
			
			$fund_status = ( $fund_updated && $tx_updated );
			
			$fund_msg = "The user account has been {$data['tx_type']}ed";
			
			if( !$fund_status ) $fund_msg .= '<br> Error: Transaction history not updated';
			
			require __dir__ . '/for-user-details-email.php';
			
		} else $fund_status = !($fund_msg = "Error: The user was not {$data['tx_type']}ed");
	
	} else $fund_status = false;
	
endif;



#### [ACTIONS] ####

if( isset($_POST['action']) ):

	switch( $_POST['action'] ) {
	
		case "activate":
		case "deactivate":
			
			$value = (int)($_POST['action'] == 'activate');
			$SQL = sQuery::update( 'tbl_users', array( "active" => $value ), "email = '{$user['email']}'" );
			
			$status = $link->query( $SQL );
			$msg = $status ? "User account {$_POST['action']}d" : "Error: Account not {$_POST['action']}d";
			
			break;
			
		case "verify":
		case "unverify":
		
			$value = (int)($_POST['action'] == 'verify');
			$SQL = sQuery::update( 'tbl_users', array( "status" => $value ), "email = '{$user['email']}'" );
			
			$action_name = substr($_POST['action'], 0, -1) . "ied";
			
			$status = $link->query( $SQL );
			$msg = $status ? "User account {$action_name}" : "Error: Account not {$action_name}";
			
			if( $status ) require __dir__ . '/for-user-details-email2.php';
			
			break;
			
		case "activate-acone":
		case "deactivate-acone":
		
			$value = (int)($_POST['action'] == 'activate-acone');
			$SQL = sQuery::update( 'tbl_users', array( 'acone' => $value ), "email = '{$user['email']}'" );
			$event = $value ? "activated" : "deactivated";
			
			$status = $link->query( $SQL );
			$msg = $status ? "Code Transfer {$event}" : "Error: Code Transfer not {$event}";
			
			break;
			
		case "activate-actwo":
		case "deactivate-actwo":
		
			$value = (int)($_POST['action'] == 'activate-actwo');
			$SQL = sQuery::update( 'tbl_users', array( 'actwo' => $value ), "email = '{$user['email']}'" );
			$event = $value ? "activated" : "deactivated";
			
			$status = $link->query( $SQL );
			$msg = $status ? "Email OTP Transfer {$event}" : "Error: Email OTP Transfer not {$event}";
			
			break;
			
		case "delete":
		
			$SQL = "DELETE FROM tbl_users WHERE id = '{$user['id']}'";
			
			$status = $link->query( $SQL );
			
			if( !$status ) $msg = "Error: Account not deleted";
			else {
				header("location : " . help::url( PANEL_DIR . '/users.php' ));
				exit;
			}
			
			break;
		
	};

endif;


?>
