<script>
$("#debit_acc_add_bill_payment2").select2({
  placeholder: "Select TR Account",
  dropdownParent: $("#form_vehicle_payment_add")
})
$("#payment_mode_add_bill_payment2").select2({
  placeholder: "Select TR Mode",
  dropdownParent: $("#form_vehicle_payment_add")
})
$('body').on('change keyup', '.vehicle_payment_amount_fields', function() {
  var total_amount = 0;
  $(".vehicle_payment_amount_fields").each(function(){
    total_amount += +parseFloat($(this).val());
  });
  $("#vehicle_payment_total_amount").val(total_amount);
})
$('body').on('click', '#btn_add_vehicle_payment', function() {
  var consignee_id = vehicle_id = '';
  <?php if($page_name=='consignee_profile'){?>
  consignee_id = "<?php echo $consignee_data->consignee_id ?>";
  <?php } ?>
  $.post("<?php echo base_url() ?>vehicles/pending_payment_list",{consignee_id:consignee_id,vehicle_id:vehicle_id},function (data) {
    var obj=$.parseJSON(data);
    // Op Balance Data
    var op_balance_data=obj.vehicle_op_balance;
    var op_balance_count=obj.vehicle_op_balance.length;
    for (var row = 0; row < op_balance_count; row++) {
      var op_balance_row = "<tr>"+
      "<input type='hidden' name='op_balance_count' value="+ op_balance_count +">"+
      "<td><input readonly type='text' name='vehicle_id_"+ row +"'  value='"+ op_balance_data[row]['id'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ op_balance_data[row]['created_date'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ op_balance_data[row]['vehicle_no'] + ':' + op_balance_data[row]['vehicle_name'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='Opening Balance' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ op_balance_data[row]['vehicle_op_balance'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Total Amount' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ op_balance_data[row]['op_balance_paid'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Total Amount' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ op_balance_data[row]['op_balance_balance'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Balance Amount' aria-describedby='basic-addon1'></td>"+
      "<td style='background: #fff;'><input required type='text' name='op_balance_amount_"+ row +"' max='"+ op_balance_data[row]['op_balance_balance'] +"' value='"+0+"' class='form-control m-input m-input--pill m-input--air vehicle_payment_amount_fields' placeholder='Amount to be paid' aria-describedby='basic-addon1'></td>"+
      "</tr>";
      $("#vehicle_payment_table_body").append(op_balance_row);
    };

    //Bill Data
    var bill_data=obj.bill_data;
    var count=obj.bill_data.length;
    for (var row = 0; row < count; row++) {
      var bill_row = "<tr>"+
      "<input type='hidden' name='bill_count' value="+ count +">"+
      "<td><input readonly type='text' name='bill_id_"+ row +"' id='bill_id' value='"+ bill_data[row]['work_slip_id'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' name='bill_date' id='bill_date' value='"+ bill_data[row]['worked_date'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ bill_data[row]['vehicle_no'] + ':' + bill_data[row]['vehicle_name'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' value='"+ bill_data[row]['work_name'] +"' class='form-control m-input m-input--pill m-input--air' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' name='total_amount' id='total_amount' value='"+ bill_data[row]['total_payable_amount'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Total Amount' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' name='total_paid' id='total_paid' value='"+ bill_data[row]['total_paid'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Total Amount' aria-describedby='basic-addon1'></td>"+
      "<td><input readonly type='text' name='balance_amount' id='balance_amount' value='"+ bill_data[row]['balance'] +"' class='form-control m-input m-input--pill m-input--air' placeholder='Balance Amount' aria-describedby='basic-addon1'></td>"+
      "<td style='background: #fff;'><input required type='text' name='amount_"+ row +"' id='amount_to_be_paid' max='"+ bill_data[row]['balance'] +"' value='"+0+"' class='form-control m-input m-input--pill m-input--air vehicle_payment_amount_fields' placeholder='Amount to be paid' aria-describedby='basic-addon1'></td>"+
      "</tr>";
      $("#vehicle_payment_table_body").append(bill_row);
    };


  });
});

$('#vehicle_payment_add_modal').on('hidden.bs.modal', function () {
  $(this).find('form').trigger('reset');
  $("#vehicle_payment_table_body").html('');
});
</script>
