<?php

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

$status = false;


if(isset($_POST['delete'])){
	$sql = "DELETE FROM md WHERE cid = '{$_POST['cid']}'";
	$status = mysqli_query($link, $sql);
	$msg = $status ? "deleted successfully!" : "Cannnot delete! ";
}


## APPROVE 

if(isset($_POST['approve'])) {
    
	if( empty($_POST['amount']) ) $msg = "No amount was defined to fund user account";
	
	else {
		
		$md = $link->query( sQuery::select( 'md', "cid = '{$_POST['cid']}'" ) )->fetch_assoc();
		
		$FUND_SQL = "
			UPDATE tbl_users 
			SET balance = balance + {$_POST['amount']}  
			WHERE email ='{$md['email']}'
		";
		
		# --------------------
		
		$APP_SQL = sQuery::update( 'md', array( 
			'status' => 1,
			'amount' => $_POST['amount']
		), "cid = '{$md['cid']}'" );
		
		# --------------------
		
		$data = array(
			"tx_no" => help::keygen(8, false),
			"tx_type" => 'credit',
			"amount" => $_POST['amount'],
			"txndate" => date("Y/m/d"),
			"description" => "Cheque Deposit",
			"to_accno" => '',
			"account" => $md['account'],
			"status" => 'success',
			"comments" => "Cheque"
		);
		
		$TX_SQL = sQuery::insert( 'tbl_transaction', $data );
		
		# --------------------
		
		$user_funded = $status = $link->query( $FUND_SQL );
		
		if( $user_funded ) {
			
			$msg = "User account funded";
			
			$approved = $link->query( $APP_SQL );
			$transacted = $link->query( $TX_SQL );
			
			require __DIR__ . '/for-md-mail.php';
			
		} else $msg = "The approval failed!";
	  
	}
	
 }

