$( document ).ready(function() {
       addNewField(1);


});

function addNewField(val) {
    // alert(val)
 if(val){

            var id = $("#break_down tbody  tr:last input[name=row_count]").val();

            if (id == undefined) {
                var r_count = 1;
            } else {
                var r_count = parseInt(id) + 1;

            }
            var html = '';
            html='<tr class="var_row">'+'<td>'+r_count+'st insstallment</td><td><input type=""><div class="form-group"><input  type="date" name="installment['+r_count+'][start_date]" class="form-control"   required></div></td><td><div class="form-group"><input  type="date" name="installment['+r_count+'][end_date]" class="form-control"   required></div></td>';

            html+='<td><div class="form-group"><input  type="text" name="installment['+r_count+'][rent]" class="form-control"  placeholder="0" required></div></td>' ;
            html+='<td><div class="form-group"><input  type="text" name="installment['+r_count+'][vat]" class="form-control"  placeholder="0" required></div></td>' ;
            html+='<td><div class="form-group"><input type="text"  name="installment['+r_count+'][cheque_amount]"class="form-control" cols="3" placeholder="0" required></div></td><td><div class="form-group"><input  type="date" name="installment['+r_count+'][cheque_date]" class="form-control"   required></div></td><td> <a type="button" class="btn bg-gradient-danger btn-sm" onclick="removeField(this)"><i class="las la-trash"></i></a><input type="hidden" name="row_count"  value="' + r_count + '"></td></tr>';
            var count_table_tbody_tr = $("#break_down tbody tr").length;
                    if (count_table_tbody_tr >= 1) {
                        $("#break_down tbody tr:last").after(html);
                    } else {

                        $("#break_down tbody").html(html);
                    }
          }
 }


 function removeField(val) {
  $(val).closest('.var_row').remove();

}
