<?php
$persons=$db->selectAll('person','where isActive=1 order by name asc');
?>
<section class="mb-4">
    <?php
    $general->inputBoxText('date','Date','','','daterangepicker');
    $general->inputBoxSelect($persons,'Persone','id','id','name',@$_POST['id'],1);
    $general->inputBoxText('mobile','mobile',inputScript:'disabled');
    $general->inputBoxSelect([
        [
            'id'=>'1',
            'title'=>'Receive from person'
        ],
        [
            'id'=>'2',
            'title'=>'Pay to person'
        ],
        ],'Transaction type','trType','id','title','','','form-control','onclick="newBalance()"');
    ?>
    <?php $general->inputBoxText('currentBalance','Balance','','','','disabled') ?>
    <?php $general->inputBoxText('trAmount','Amount','','','','autocomplete="off" onkeyup="newBalance()"'); ?>

    <?php $general->inputBoxText('nBalance','New Balance','','','','disabled') ?>
    <?php $general->inputBoxTextArea('trNote','Note') ?>
        <a href="javascript:void()" class="form-control btn btn-success" onclick="transactionWithPerson()">Submit</a>

</section>
<script type="text/javascript">
    function newBalance(){
        var cBalance=parse_int($('#currentBalance').val());
        var trAmount=parse_int($('#trAmount').val());
        var trType      = parse_int($('#trType').val());
        var nBalance=0;
        if(trType==1){
            nBalance=cBalance-trAmount;
            $('#forPay').hide();
        }
        else if(trType==2){
            nBalance=cBalance+trAmount;
            $('#forPay').show(); 
        }
        else{
            nBalance='';
        }
        $('#nBalance').val(nBalance);
    }
    $('#id').on('change', function(){
        personCurrentBalance();
    }); 

    function transactionWithPerson(){
        var id          = parse_int($('#id').val());
        var date        = $('#date').val();
        var trNote      = $('#trNote').val();
        var trAmount    = parse_float($('#trAmount').val());
        var trType      = parse_int($('#trType').val());
        console.log('dd')
        errorSet=0;
        if(1>id){
                        swMessage('Please select person.');

            errorSet=1;   
        }
        else if(0==trType){
            errorSet=1;
            swMessage('Please select transaction type.');
        } 
        else if(0==trAmount){
            errorSet=1;
            swMessage('Amount field is required.');
        } 
        else if(''==trNote){
            errorSet=1;
            swMessage('Note field is required.');
        } 
        if(errorSet==0){
            $('#personTransactionBtn').hide();
            //$('#reportArea').html(loadingImage);
            $.ajax({
                type:'post',
                url:ajUrl,
                data:{module:'transactionWithPerson',id:id,date:date,trNote:trNote,trType:trType,trAmount:trAmount},
                success:function(data){
                    //$('#reportArea').html('');
                    if(data.status==1){
                        $('#id').val('');
                        $('#trNote').val('');
                        $('#trAmount').val('');
                        $('#nBalance').val('');
                        $('#currentBalance').val('');
                        $('#customerStBtn').html('');
                        $('#trType').val('');
                        $('#personTransactionBtn').show();
                        select2Call();
                    }
                    swMessageFromJs(data.m);
                }
            });
        }
        else{
            console.log('do')
        }
    }         
    function personCurrentBalance(){
        var id= parse_int($('#id').val());
        if(0<id){

            $.ajax({
                type:'post',
                url:ajUrl,
                data:{module:'personCurrentBalance',id:id},
                success:function(data){
                    if(data.status==1){
                        $('#currentBalance').val(data.balance);
                        $('#mobile').val(data.mobile);
                        newBalance();
                    }
                    //swMessageFromJs(data.m);
                }
            }); 
        }
    }
</script>
