<?php
    $fromString=date('d-m-Y',TODAY_TIME);
    $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_data[0]['pending']??0;
    $pending = round($pending);
    $available = $balance-$pending; 
    //$banks = $db->selectAll('bank','where status=1','id,name');
    $cash_accounts=$acc->get_all_cash_accounts();

?>
<section class="mb-4">
    <div class="card">
        <div class="card-header text-center py-3"> 
            <h5 class="mb-0 text-center">
                <strong>Deposit</strong>
            </h5>
        </div>
    </div>
    <?php
        $general->inputBoxText('balance','Balance',$balance,'','amount_td','readonly');
        $general->inputBoxText('pending-balance','Pending balance',$pending,'','amount_td','readonly');
        $general->inputBoxText('available-balance','Available balance',$available,'','amount_td','readonly');
        $general->inputBoxSelect($cash_accounts,'Send to','bank_id','id','title');
        $general->inputBoxText('amount','Amount','','','amount_td');

        $general->inputBoxTextArea('note','Note');
    ?>
    <button onclick="deposit()"  class="form-control btn btn-success deposit">SUBMIT</button>

</section>
<script type="text/javascript">
    function deposit(){
        buttonLoading('deposit');
        let bank_id =parse_int($('#bank_id').val()) ;
        let balance =parse_float($('#balance').val()) ;
        let pending =parse_float($('#pending-balance').val()) ;
        let available =parse_float($('#available-balance').val()) ;
        let amount =parse_float($('#amount').val()) ;
        let note =$('#note').val() ;
        let error=0;
        if(bank_id<1){error=1; swMessage('Select a bank.');}
        else if(amount<=0){error=1; swMessage('Invalid Amount.');}
            else if(amount>available){error=1; swMessage('You do not have enough balance to send');}
                else{
                    $.ajax({
                        type:'post',
                        url:ajUrl,
                        data:{module:'mpo_deposit',bank_id:bank_id,amount:amount,note},
                        success:function(data){
                            console.log('success') ;
                            button_loading_destroy('deposet','SUBMIT');
                            if(typeof(data.status)  !== "undefined"){ 
                                if(data.status==1){
                                    $('#pending-balance').val(pending+amount);
                                    $('#available-balance').val(available-amount);
                                    $('#bank_id').val('');
                                    $('#amount').val('');
                                    $('#note').val('');
                                    select2Call();
                                    productSaleSubTotal();
                                }
                                swMessageFromJs(data.m);
                            }
                            else{
                                swMessage(AJAX_ERROR_MESSAGE); 
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.log('error') ;
                            button_loading_destroy('deposet','SUBMIT');
                            swMessage(AJAX_ERROR_MESSAGE); 
                        }
                    }); 
                }
        if(error==1){
            button_loading_destroy('deposet','SUBMIT');
        }
    }
    </script>
   
