$(document).ready(function(){

	(function($){
		$.fn.qf_load_payouts = function(data) {
			var table = $("#payouts-table").DataTable();
			table.clear();
			table.destroy();
			var i = 1;
			for(var key in data.payouts) {
				var tr = $("<tr></tr>");
				var html = "<td>"+i+"</td>";
				html += "<td>"+data.payouts[key].date+"</td>";
				html += "<td>"+data.payouts[key].ref+"</td>";
				html += "<td>"+data.payouts[key].requester+"</td>";
				// html += "<td>"+data.payouts[key].receiving_account+"</td>";
				html += "<td>"+data.payouts[key].amount+"</td>";
				html += '<td><span class="badge ';
				switch(data.payouts[key].status) {
					case "disapproved":
					case "cancelled":
						html += "badge-danger";
						break;

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

					default:
						html += "badge-default";
						break;
				}
				html += '">'+data.payouts[key].status+'</span>';
				html += '</td>';
				if(data.payouts[key].status == 'pending' && data.admin == 1) {
					html += '<td><a class="green popmeup" href="pm-approve-payout?id='+data.payouts[key].id+'" data-width=700 data-height=500>approve</a></br><a class="ajax-action red" href="javascript:void(0)" data-prompt=\'{"reason": "Please State Reason"}\' data-content="You are about to disapprove this payout. If you are sure about this action: " data-action-callback="pm_disapprove_payout" data-userid='+data.payouts[key].user_id+' data-ref='+data.payouts[key].ref+' data-id='+data.payouts[key].id+'>disapprove</a></td>';
				} else {
					html += '<td>...</td>;'
				}
				
				tr = $(tr).append(html);
				$("#payouts-table tbody").append(tr);
				i++;
			}
			$("#pm-query-filter-form-search").html("Search").attr("disabled", false);
			$("#payouts-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'] = $("#payouts-table tbody")[0].children.length;
	options['query_function'] = 'qf_get_payouts';
	options['js_callback'] = 'qf_load_payouts';
	$(this).html("Fetching Information").attr("disabled", true);
	$.loadFromServer(options);
})

