$(document).ready(function(){

	(function($){
		$.fn.qf_load_direct_credits = function(data) {
			console.log(data);
			var table = $("#direct_credits-table").DataTable();
			table.clear();
			table.destroy();
			var i = 1;
			var total = 0;
			for(var key in data.direct_credits) {
				var tr = $("<tr></tr>");
				var html = "<td>"+i+"</td>";
				html += "<td>"+data.direct_credits[key].date+"</td>";
				html += "<td>"+data.direct_credits[key].ref+"</td>";
				html += "<td>"+data.direct_credits[key].narration+"</td>";
				html += "<td>"+data.direct_credits[key]._amount+"</td>";
				html += '<td><span class="badge ';
				switch(data.direct_credits[key].status) {
					case "disapproved":
					case "cancelled":
						html += "badge-danger";
						break;

					case "confirmed":
						html += "badge-success";
						break;

					default:
						html += "badge-default";
						break;
				}
				html += '">'+data.direct_credits[key].status+'</span>';
				html += '<td>'+data.direct_credits[key]._posted_by+'</span>';
				html += '</td>';
				if(data.direct_credits[key].status == 'pending' && data.admin == 1) {
					html += '<td><a class="ajax-action red" href="javascript:void(0)" data-action-callback="pm_approve_direct_credit" data-posted_by='+data.direct_credits[key].posted_by+' data-ref='+data.direct_credits[key].ref+' data-id='+data.direct_credits[key].id+'">approve</a></br><a class="ajax-action red" href="javascript:void(0)" data-action-callback="pm_disapprove_direct_credit" data-posted_by='+data.direct_credits[key].posted_by+' data-ref='+data.direct_credits[key].ref+' data-id='+data.direct_credits[key].id+'>disapprove</a></td>';
				} else {
					html += '<td>...</td>';
				}
				
				tr = $(tr).append(html);
				$("#direct_credits-table tbody").append(tr);
				i++;
			}
			$("#pm-query-filter-form-search").html("Search").attr("disabled", false);
			$("#direct_credits-table").DataTable({
		        lengthMenu: [[-1, 10, 50, 75, 100], ["All", 10, 50, 75, 100]],
		        iDisplayLength: 10,
		        dom: 'lBfrtip',
		        buttons: [
				    {
				        extend: 'excel',
				        text: 'Excel',
				        className: 'btn btn-default',
				        exportOptions: {
				            columns: ':not(.notexport)'
				        }
				    },
				    {
				        extend: 'pdf',
				        text: 'PDF',
				        className: 'btn btn-default',
				        exportOptions: {
				            columns: ':not(.notexport)'
				        }
				    },
				    {
				        extend: 'print',
				        text: 'Print',
				        className: 'btn btn-default',
				        exportOptions: {
				            columns: ':not(.notexport)'
				        }
				    }
		        ]
		    });
		}
	})(jQuery);
});

$(document).on("click", "#pm-query-filter-form-search", function(e){
	e.preventDefault();
	var data = $("#"+$(this).data("form")).serializeObject();
	var options = {};
	options['args'] = data;
	options['size'] = $("#direct_credits-table tbody")[0].children.length;
	options['query_function'] = 'qf_get_direct_credits';
	options['js_callback'] = 'qf_load_direct_credits';
	$(this).html("Fetching Information").attr("disabled", true);
	$.loadFromServer(options);
})

