$(document).ready(function(){
    $("#rc_to").on('focus',function(){
        var id = $("#rc_from").val();
        if (id != '')
        {
            $.ajax({
                url:"../Admin_modules/mob.php",
                method: "POST",
                data: {query:id},
                success:function(data)
                {
                    $('#mob_no').val(data);
                }
            });
            $.ajax({
                url:"../Admin_modules/gst.php",
                method: "POST",
                data: {query:id},
                success:function(data)
                {
                    $('#gst').val(data);
                }
            });
        }
    });
    $("#rc_from").on('keyup',function(){
        var id = $(this).val();
        if (id != '')
        {
            $.ajax({
                url:"../Admin_modules/suggestion.php",
                method: "POST",
                data: {query:id},
                success:function(data)
                {
                    $('#nameList').fadeIn();
                    $('#nameList').html(data);
                }
            });
        }
    });
    $(document).on('click', 'li', function(){  
       $('#rc_from').val($(this).text());  
       $('#nameList').fadeOut();  
    });
});

function showTable(which) 
{
    var rc_gst = $("#with_gst").val();
    
    if(which == "1") {
        $("#paymethod").css("display","table");
        if (rc_gst !== '')
        {
            $.ajax({
                url:"../Admin_modules/rc_gst.php",
                method: "POST",
                data: {query:rc_gst},
                success:function(data)
                {
                    //console.log(data);
                    $('#rc_no').val(data);
                }
            });
        }
    }
    else if (which == "2") 
    {
        $("#paymethod").css("display","table");
        if (rc_gst !== '')
        {
            $.ajax({
                url:"../Admin_modules/rc_gst.php",
                method: "POST",
                data: {query:rc_gst},
                success:function(data)
                {
                    //console.log(data);
                    var rcnum = "A"+data;
                    $('#rc_no').val(rcnum);
                }
            });
        }
    }
}

function branch() 
{
    var br_to = $("#br_to").val();
    var doc_type = $("#particulars").val();
    $("#pay_type").val() == "toPay";
    $.ajax({
        url:"../Admin_modules/charges.php",
        method: "POST",
        data: {to: br_to, type: doc_type},
        async: false,
        success:function(data)
        {
            var res = data;
            $('#cour_charg').val(res);
            var charge = $('#cour_charg').val();
    

            // if ($("#pay_type").val() == "toPay")
            // {
                var to_pay = $("#vvp_amount").val();
                var new_vvp = parseInt(to_pay) / 1000;
                $.ajax({
                    url:"../Admin_modules/new_charge.php",
                    method: "POST",
                    data: {to: br_to},
                    async: false,
                    success:function(new_data)
                    {
                        
                        var n_vvp = new_vvp * new_data;
                        var nw_vvp = n_vvp.toFixed(2);
                        $('#vpp_charge').val(nw_vvp);
                    }
                });
                $('#vpp_charge').attr("type","text");

                var kg = $("#kg").val();
                var new_cc = parseInt(charge) * kg;
                $('#cour_charg').val(new_cc);
                $('#cc').html(new_cc);

                var one = $('#cc').text();
                var two = $('#vpp_charge').val();
                var total = Math.ceil(parseInt(one) + parseFloat(two));

                $('#total_cc').html(total);
                $('#grand_tot').html(total);
                $('#g_tot').val(total);
        }
    });
}


function editInput()
{
    $('#cour_charg').removeAttr('readonly');
    $('#vpp_charge').removeAttr('readonly');
}

function makeEdit()
{
    if ($("#pay_type").val() == "toPay")
    {
        var new_cour = $('#cour_charg').val();
        var new_vpp = $('#vpp_charge').val();

        var total = Math.ceil(parseInt(new_cour) + parseFloat(new_vpp));           

        //Storing values in all variables
        $('#cour_charg').val(new_cour);
        $('#cc').html(new_cour);
         $('#vpp_charge').val(new_vpp);
        $('#total_cc').html(total);
        $('#grand_tot').html(total);
        $('#g_tot').val(total);
    }
    else if($("#pay_type").val() == "paid")
    {
        var new_cour = $('#cour_charg').val();
        var total = Math.ceil(parseInt(new_cour));           

        //Storing values in all variables
        $('#cour_charg').val(new_cour);
        $('#cc').html(new_cour);
        $('#total_cc').html(total);
        $('#grand_tot').html(total);
        $('#g_tot').val(total);
    }
}


function edit_receipt()
{
    var br_to = $("#br_to").val();
    var doc_type = $("#particulars").val();
    var kg = $("#newweight").val();
    var inp_vpp = $("#new_vpp").val();
    var tot = $("#new_tot").val();

    $.ajax({
        url:"../Admin_modules/charges.php",
        method: "POST",
        data: {to: br_to, type: doc_type},
        async: false,
        success:function(data)
        {
            var res = data;
            var new_cc = res * kg;
            $('#newcc').val(new_cc);

            var new_total = parseFloat(new_cc) + parseFloat(inp_vpp);
            $('#new_tot').val(new_total);
        }
    })
}

function only_vpp()
{
    var br_to = $("#br_to").val();
    var newvvp  = $("#editvvp").val();
    var ccnew = $('#newcc').val();
    $.ajax({
        url:"../Admin_modules/new_charge.php",
        method: "POST",
        data: {to: br_to},
        async: false,
        success:function(data)
        {
            var res = data;
            var new_vvp = (newvvp / 1000) * res;
            var cnf = new_vvp.toFixed(2);
            $("#new_vpp").val(cnf);

            var new_total = parseFloat(ccnew) + parseFloat(cnf);
            $('#new_tot').val(new_total);
        }
    })
}