<script>
$("#debit_acc_add_bill_payment2").select2({
  placeholder: "Select TR Account",
  dropdownParent: $("#form_supplier_payment_add")
})
$("#payment_mode_add_bill_payment2").select2({
  placeholder: "Select TR Mode",
  dropdownParent: $("#form_supplier_payment_add")
})

$('body').on('change keyup', '.supplier_payment_amount_fields', function() {
  var total_amount = 0;
  $(".supplier_payment_amount_fields").each(function(){
    total_amount += +parseFloat($(this).val());
  });
  $("#supplier_total_amount").val(total_amount);
})

$('body').on('click', '#btn_add_supplier_payment', function() {
  var supplier_id = $('#supplier_id').val();
  $.post("<?php echo base_url() ?>supplier/supplier_payment_details",{supplier_id:supplier_id},function (data) {
    var obj=$.parseJSON(data);

    //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]['purchase_id'] +"' 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='bill_date' id='bill_date' value='"+ bill_data[row]['purchase_date'] +"' 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_amount' id='total_amount' value='"+ bill_data[row]['bill_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_amount'] +"' 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_amount'] +"' value="+0+" class='form-control m-input m-input--pill m-input--air supplier_payment_amount_fields' placeholder='Amount to be paid' aria-describedby='basic-addon1'></td></tr>";
      $("#supplier_payment_table_body").append(bill_row);
    };

    //Opening Balance
    var op_data=obj.op_data;
    var balance = op_data['balance'];
    if(balance>0){
      var op_row = '<tr><td></td><td>Opening Balance : </td><td><input readonly type="text" name="" id="" value='+ op_data["total"] +'  class="form-control m-input m-input--pill m-input--air " placeholder="" aria-describedby="basic-addon1"></td><td><input readonly type="text" name="" id="" value='+ op_data["paid"] +' class="form-control m-input m-input--pill m-input--air " placeholder="" aria-describedby="basic-addon1"></td><td><input readonly type="text" name="" id="" value='+ op_data["balance"] +' class="form-control m-input m-input--pill m-input--air " placeholder="" aria-describedby="basic-addon1"></td><td style=" background: #fff; "><input required type="text" name="op_amount_to_be_paid" id="op_amount_to_be_paid" max='+ op_data["balance"] +' value="0" class="form-control m-input m-input--pill m-input--air supplier_payment_amount_fields" placeholder="" aria-describedby="basic-addon1"></td></tr>';
      $("#supplier_payment_table_body").append(op_row);
    }
    //Advance payments
    var ap_row = '<tr><td></td><td></td><td></td><td></td><td>Advance Amount : </td><td style=" background: #fff; "><input required type="text" name="advance_amount" id="advance_amount" value="0" class="form-control m-input m-input--pill m-input--air supplier_payment_amount_fields" placeholder="Advance amount" aria-describedby="basic-addon1"></td></tr>';
    $("#supplier_payment_table_body").append(ap_row);


  });
});





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