//   $(document).on('change', 'input#quantity', function (e) {
 $('input#quantity').keyup(function(){
  var quantity = 0, unit_price = 0; var gallon_price=0; var quantity_ltr=4.56; var unit_ltr_price=0; var tax=5;var total=0;
  quantity=$('#quantity').val();
  unit_price=$('#unit_price').val();
  $('#quantity_ltr').val(quantity_ltr*quantity);
  $('#unit_ltr_price').val(unit_price/4.56);
  gallon_price=quantity * unit_price;
  $('#gallon_price').val(gallon_price);
  $('#tax').val(tax);
  total=((gallon_price*tax)/100)+gallon_price;
  $('#total').val(total);
  });
//   $(document).on('change', 'input#unit_price', function (e) {
 $('input#unit_price').keyup(function(){
  var quantity = 0, unit_price = 0; var gallon_price=0;var quantity_ltr=4.56; var unit_ltr_price=0; var tax=5; var total=0;
  quantity=$('#quantity').val();
  unit_price=$('#unit_price').val();
 $('#quantity_ltr').val(quantity_ltr*quantity);
  $('#unit_ltr_price').val(unit_price/4.56);
  gallon_price=quantity * unit_price;
  $('#gallon_price').val(gallon_price);
  $('#tax').val(tax);
  total=((gallon_price*tax)/100)+gallon_price;
  $('#total').val(total);
  });
//   $(document).on('change', 'input#tax', function (e) {
 $('input#tax').keyup(function(){
    // alert()
  var quantity = 0, unit_price = 0; var gallon_price=0;var quantity_ltr=4.56; var unit_ltr_price=0; var tax=5; var total=0;
  quantity=$('#quantity').val();
  unit_price=$('#unit_price').val();
 tax=$('#tax').val();
  
  gallon_price=quantity * unit_price;
  // $('#tax').val(tax);
  total=((gallon_price*tax)/100)+gallon_price;
  $('#total').val(total);
  });
// function hide1(val = ""){
//     var btn_value=$('#btn-id').val();
//     // console.log(val)
//     if (btn_value=='Remove Litr') {

//     $('.hide1').hide();
//     $('.hide2').hide();
//     $('#hide_value').val(1);
//     $('#btn-id').val('Active Liter');
//     }else{
//     $('.hide1').show();
//     $('.hide2').show();
//     $('#hide_value').val(0);
//     $('#btn-id').val('Remove Litr');
//     }
    
      
// }


function change_liter(val){
   if(val == 1){
        $('.hide1').show();
    $('.hide2').show();
    $('#hide_value').val(0);
 
   }else{
    $('.hide1').hide();
    $('.hide2').hide();
    $('#hide_value').val(1);
   }
    
      
}

  $("#vehicle_number").on('keyup', function () {
    var value=$(this).val();

    $.ajax({
        url: 'vehicle-data',
        type: 'get',
        data: {search:value}, // /converting the form data into array and sending it to server

        success: function (response) {

            if(response){
                $('#vehicle-name').fadeIn();
                $('#vehicle-name').html(response);
            }


        }
    });
});

$(document).on('click', 'li', function(){
    $('#vehicle_number').val($(this).text());
    $('#vehicle-name').fadeOut();
});

function vehicle_by_customer(id){

$.ajax({

    url: 'vehicle_by_id/' + id,
    dataType: 'json',
    success: function (response) {
        $("#vehicle_number").empty();
        $("#driver_name").empty();
        var len = response.length;
     
        for (var i = 0; i < len; i++) {
            var id = response[i].id;
            var vehicle_no  = response[i].vehicle_no ;
            var driver_name  = response[i].driver_name ;
            $("#vehicle_number").append(
                    " <option  value='" + id + "'>" + vehicle_no  +"-" + driver_name  +"</option>"
                    );
                $("#driver_name").append(
                    " <option  value='" + id + "'>" + driver_name  + "</option>"
                    );

        }
    }
});
}

function vehicle_change(id){
$.ajax({

    url: 'vehicle_change/' + id,
    dataType: 'json',
    success: function (response) {
    
        $("#driver_name").empty();
        var len = response.length;
     
        for (var i = 0; i < len; i++) {
            var id = response[i].id;
          
            var driver_name  = response[i].driver_name ;
          
                $("#driver_name").append(
                    " <option  value='" + id + "'>" + driver_name  + "</option>"
                    );


        }
    }
});
}



function edit_vehicle_by_customer(id){

$.ajax({

    url: '../vehicle_by_id/' + id,
    dataType: 'json',
    success: function (response) {
        $("#edit_vehicle_number").empty();
        $("#edit_driver_name").empty();
        var len = response.length;
     
        for (var i = 0; i < len; i++) {
            var id = response[i].id;
            var vehicle_no  = response[i].vehicle_no ;
            var driver_name  = response[i].driver_name ;
            $("#edit_vehicle_number").append(
                    " <option  value='" + id + "'>" + vehicle_no  +"-" + driver_name  +"</option>"
                    );
               $("#edit_driver_name").append(
                    " <option  value='" + id + "'>" + driver_name  + "</option>"
                    );

        }
    }
});
}

function edit_vehicle_change(id){
$.ajax({

    url: '../vehicle_change/' + id,
    dataType: 'json',
    success: function (response) {
    
        $("#edit_driver_name").empty();
        var len = response.length;
     
        for (var i = 0; i < len; i++) {
            var id = response[i].id;
          
            var driver_name  = response[i].driver_name ;
          
                $("#edit_driver_name").append(
                    " <option  value='" + id + "'>" + driver_name  + "</option>"
                    );

        }
    }
});
}
$( document ).ready(function() {
    getDoNumber('CashMemo');
    get_litr_show('SRK DIESEL TRADING L.L.C');
    
});
function getDoNumber(val){
    if(val == 'DoNo'){
        $('.enterdonno').show();
        
    }else{
         $('.enterdonno').hide();
         $( "#ledger_value" ).prop( "required", false );
    }
}


function qtyavailable(val){
    // alert(val)
       var stock_value=$('#stock_value').val();
       if (parseFloat(val) > parseFloat(stock_value)) {
         swal(stock_value+" Available Quanity!", {
                      icon: "error",
                      });
         $(".save").prop('disabled', true);
       }else{
         $(".save").prop('disabled', false);
       }
   }
function editqtyavailable(val){
    // alert(val)
       var stock_value=$('#edit_stock_value').val();
       if (parseFloat(val) > parseFloat(stock_value)) {
         swal(stock_value+" Available Quanity!", {
                      icon: "error",
                      });
         $(".update").prop('disabled', true);
       }else{
         $(".update").prop('disabled', false);
       }

}

function getPrice(val){
    $.ajax({

    url: '/get_price/' + val,
    dataType: 'json',
    success: function (response) {
    // alert(response.dono_status)
        $("#unit_price").empty();
  $("#unit_price").val(response.amount);
  change_liter(response.litr_status);
  if(response.dono_status == 1){
      $(".enterdonno").hide();
  }
      
   
    }
});
}
function edit_customer_amount(val){
    $.ajax({

    url: '/get_price/' + val,
    dataType: 'json',
    success: function (response) {
    // alert(response;)
        $("#unit_price").empty();
  $("#unit_price").val(response.amount);
       change_liter(response.litr_status);
   
    }
});
}
function get_litr_show(val){
    if (val == 'SRK DIESEL TRADING L.L.C') {
    $('.hide1').hide();
    $('.hide2').hide();
    $('#btn-id').val('Active Liter');
    $('#hide_value').val(1);
    }else{
     $('.hide1').show();
    $('.hide2').show();
    $('#hide_value').val(0);
    $('#btn-id').val('Remove Litr');
    }
     
}