<?php
    $user_ledger=$acc->get_user_head($userData,$jArray);
    $balance = $acc->headBalance($user_ledger);
    $pending_data = $db->selectAll('deposit_request','where status=1 and user_id='.USER_ID,'sum(amount) as pending');
    $pending = $pending[0]['pending']??0;
    $available = $balance-$pending; 
    $bank_id = intval($_POST['bank_id']);
    $amount = floatval($_POST['amount']);
    $note = $_POST['note'];
    $cash_accounts=$acc->get_all_cash_accounts();
    
    if(!isset($cash_accounts[$bank_id])){$error=fl(); setMessage(1,'Please select send to field');}
    elseif($amount<=0){$error=fl(); setMessage(1,'Invalid amount');}
    elseif($amount>$available){$error=fl(); setMessage(1,'Insufficient balance');}
    else{
        $data = [
            'bank_id'   => $bank_id,
            'user_id'   => USER_ID,
            'time'      => TIME,
            'amount'    => $amount,
            'note'      => $note,
            'status'    => 1
        ];
        $insert = $db->insert('deposit_request',$data,false,'array',$jArray);
        if(!$insert){$error=fl(); setMessage(66);}
        else{$jArray['status']=1;setMessage(2,'Your request successfully post. It\'s wait for verify');}
    }