<?php
$suppliers = $db->selectAll('suppliers', 'order by supName asc');
?>
<section class="mb-4">
    <?php
    $general->inputBoxText('date','Date','','','daterangepicker');
    $general->inputBoxSelect($suppliers,'Supplier','supID','supID','supName','','','form-control');
    $general->inputBoxSelect([
        [
            'id'=>DEBIT,
            'title'=>'Receive from supplier'
        ],
        [
            'id'=>CREDIT,
            'title'=>'Pay to supplier'
        ],
        [
            'id'=>3,
            'title'=>'Bonus'
        ],
        ],'Transaction type','type','id','title','','','form-control','');
    ?>
    <?php $general->inputBoxText('sPayable','Balance','','','','disabled') ?>
    <span style="color: red;">সাপ্লায়ার ব্যালেন্স মাইনাস মানে সাপ্লায়ার পাবে</span>
    <?php $general->inputBoxText('pay','Amount','','','','autocomplete="off"'); ?>
    <?php $general->inputBoxText('cost','Cost','','','','autocomplete="off"'); ?>
    <?php $general->inputBoxTextArea('note','Note') ?>
    <a href="javascript:void()" class="form-control btn btn-success" onclick="supNewTransaction()">Submit</a>
</section>
<script type="text/javascript">
$('#supID').on('change', function(){
        getSupplierDue();
    }); 
function supNewTransaction(){
    var date    = $('#date').val();
    var type    = parse_int($('#type').val());
    var supID   = parse_int($('#supID').val());
    var pay     = parse_float($('#pay').val());
    var cost    = parse_float($('#cost').val());
    var note    = $('#note').val();
    errorSet=0;
    console.log(supID)
    if(supID<=0){
        errorSet=1;
        swMessage('Invalid Supplier');
    }
    else if(pay<1){
        errorSet=1;
        swMessage('Invalid Transaction amount');
    }
    else if(type<=0){
        errorSet=1;
        swMessage('Invalid transaction type');
    }
    else if(note==''){
        errorSet=1;
        swMessage('নোট দিতে হবে');
    }
    if(errorSet==0){
        $.post(ajUrl,{module:'supNewTransaction',supID:supID,pay:pay,note:note,cost:cost,type:type,date:date},function(data){
            if(data.status==1){
                $('#supID').val('');
                $('#sPayable').val('');
                $('#pay').val('');
                $('#note').val('');
                $('#cost').val('');
                select2Call();
                swMessageFromJs(data.m);
            }
            else{
                swMessageFromJs(data.m);
                $('#trDetailsArea').html('');
            }
        }); 
    }
}
    function getSupplierDue(){
        var supID= parse_int($('#supID').val());
        if(supID>0){
            $.post(ajUrl,{module:'getSupplierDue',supID:supID},function(data){
                if(data.status==1){
                    $('#sPayable').val(data.subBalance);
                }
                else{
                    $('#sPayable').val('');
                }
                swMessageFromJs(data.m);
            });            
        }
    }
</script>
