$(document).ready(function(){

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

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

					default:
						html += "badge-default";
						break;
				}
				html += '">'+data.expenses[key].status+'</span>';
				html += '<td>'+data.expenses[key]._posted_by+'</span>';
				html += '</td>';
				html += '<td>'+data.currency+total+'</td>';
				if(data.expenses[key].status == 'pending' && data.admin == 1) {
					html += '<td><a class="ajax-action red" href="javascript:void(0)" data-action-callback="pm_approve_expense" data-posted_by='+data.expenses[key].posted_by+' data-ref='+data.expenses[key].ref+' data-id='+data.expenses[key].id+'">approve</a></br><a class="ajax-action red" href="javascript:void(0)" data-action-callback="pm_disapprove_expense" data-posted_by='+data.expenses[key].posted_by+' data-ref='+data.expenses[key].ref+' data-id='+data.expenses[key].id+'>disapprove</a></td>';
				} else {
					html += '<td>...</td>';
				}
				
				tr = $(tr).append(html);
				$("#expenses-table tbody").append(tr);
				i++;
			}
			$("#pm-query-filter-form-search").html("Search").attr("disabled", false);
			$("#expenses-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'] = $("#expenses-table tbody")[0].children.length;
	options['query_function'] = 'qf_get_expenses';
	options['js_callback'] = 'qf_load_expenses';
	$(this).html("Fetching Information").attr("disabled", true);
	$.loadFromServer(options);
})

