$(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) 
{
    if(which == "1") {
        $("#paymethod").css("display","table");
        var rc_gst = $("#with_gst").val();
        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);
                }
            });
        }
    }
}

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);
            //}
            // else if($("#pay_type").val() == "paid")
            // {
            //     $('#vpp_charge').val("PAID");
            //     $("#vvp_amt"). removeAttr("hidden");
            //     $('#vpp_charge').attr("type","hidden");

            //     var kg = $("#kg").val();
            //     var new_cc = parseInt(charge) * kg;
            //     $('#cour_charg').val(new_cc);
                
            //     $('#cc').html(new_cc);
            //     var total = $('#cc').text();
                
            //     $('#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();
    $.ajax({
        url:"../Admin_modules/charges.php",
        method: "POST",
        data: {to: br_to, type: doc_type},
        async: false,
        success:function(data)
        {
            var res = data;
            var kg = $("#editWeight").val();
            var vvp = $("#editvvp").val();
            var og_vvp = vvp / 1000 * 5; 
            var new_cc = res * kg + og_vvp;
            var calc = 9 / 100 * new_cc;
            var new_calc = calc.toFixed(2);
            var tot = $("#newtot").val();
            var n_tot = parseFloat(new_cc) + parseFloat(new_calc);
            $('#newcc').html(new_cc);
            $("#newgst").html(new_calc);
            $('#newtot').html(n_tot);
            $('#new_cc').val(new_cc);
            $("#new_gst").val(new_calc);
            $('#new_tot').val(n_tot);
        }
    })
}

