
$(document).on('click', 'td.details-control', function () {

  var tr = $(this).closest('tr');
  var row = table.row(tr);
  var tableId = 'purchases-' + row.data().id;
  var client_id = row.data().id;
  if (row.child.isShown()) {
    row.child.hide();
    tr.removeClass('shown');
  } else {
    row.child(template(row.data())).show();
    initTable(tableId, row.data(), client_id);
    tr.addClass('shown');
    tr.next().find('td').addClass('no-padding bg-gray');
  }
});
function initTable(tableId, data, client_id) {
  $('#' + tableId).DataTable({
    paging: false,
    searching: false,
    ordering: false,
    info: false,
    ajax: data.details_url,
    columns: [
    { data: 'loanprocess_no', name: 'loanprocess_no' },
    { data: 'loanprocess_datepayment', name: 'loanprocess_datepayment' },
    { data: 'loanprocess_principle', name: 'loanprocess_principle', "className": 'text-right' },
    { data: 'loanprocess_interest', name: 'loanprocess_interest', "className": 'text-right' },
    { data: 'loanprocess_principleos', name: 'loanprocess_principleos', "className": 'text-right' },
    { data: 'loanprocess_owe', name: 'loanprocess_owe', "className": 'text-right' },
    { data: 'loanprocess_owepay', name: 'loanprocess_owepay', "className": 'text-right' },
    { data: 'penalty', "className": 'text-right' },
    {
      data: 'status', "render": function (data, type, row, meta) {
      if (meta.row === 0) {
        if (row["loanprocess_paid"] === '1') {
        return data;
        }
        else if (row["loanprocess_paid"] === '2') {
        return data;
        }
        var button = '';
          var button = '<a data-toggle="modal" href="#form-pay" onclick="pay(this)" class="btn btn-xs btn-outline-primary"  title="Pay" ><i class="fa fa-check-circle"></i></a> Pending';
        return button;
      }
      return data;
      }
    }
    ],
    "initComplete": function (settings, json) {
    $('.main-spinner').hide();
    }
  });
}
function payoff(obj) {
  
  var id = $(obj).closest("tr").attr("id");
  $('#payoff_loan_id').val(id);
  $.ajax({
    type: 'GET',
    url: '/realty/get-loan-info-record/' + id,
    dataType: 'json',
    data: {
      'id': id,
    },
    success: function (data) {
      $('#payoff_amount').val(data.loan_amountos);
    }
  });
}