<?php

$_POST = help::sanitize($_POST);

$status = false;


if(isset($_POST['delete'])){

	$SQL = "DELETE FROM deposits  WHERE tnxid ='{$_POST['tnxid']}'";
	$status = mysqli_query($link, $SQL);
	$msg = $status ? "Data deleted successfully!" : "The data could not be deleted! ";

}

    


if(isset($_POST['approve'])) {
 
	$btc = $link->query( sQuery::select( "deposits", "tnxid = '{$_POST['tnxid']}'" ) )->fetch_assoc();
	
	# ----------- [ INSERT TX ] --------
	
	$user = help::get_user( $btc['email'] );
	
	$tx_data = array(
		"tx_no" => help::keygen(8, false),
		"tx_type" => 'credit',
		"amount" => $btc['usd'],
		"date" => date("Y/m/d"),
		"description" => "{$btc['network']} Payment",
		"to_accno" => '',
		"account" => $user['acctno'],
		"status" => 'success',
		"comments" =>  "{$btc['network']} Payment"
	);
	
	$TX_SQL = sQuery::insert( 'tbl_transaction', $tx_data );
	
	# ----------- [ BTC APPROVAL ] ---------
	
	$APPROVAL_SQL = sQuery::update( 'deposits', array( 'status' => 1 ), "tnxid = '{$btc['tnxid']}'" );
	
	# ----------- [ BALANCE UPDATE ] ----------
	
	$FUND_SQL = sQuery::update( 'tbl_users', array( "balance" => (float)$user['balance'] + (float)$btc['usd'] ), "email='{$user['email']}'" );
	
	# ----------- [ PROCESS ALL ] -----------
	
	$approved = $status = $link->query( $APPROVAL_SQL );
	
	if( $approved ) {
		
		$msg = "{$btc['network']} Payment Approved";
		
		$funded = $link->query( $FUND_SQL );
		
		$msg .= $funded ? "<br>User Account Funded" : "<br>User Account Not Funded";
		
		if( !$funded ) $link->query( sQuery::update( 'deposits', array('status' => 0), "tnxid = '{$btc['tnxid']}'" ) );
		
		else {
		
			$history = $link->query( $TX_SQL );
			
			require __DIR__ . '/for-bcm-email.php';
		
		};
		
	} else $msg = "cannot Approve! ";
 
}