jQuery(document).ready(function() {
    jQuery('#all_checked_payment:button').toggle(function() {
        jQuery('input:checkbox').attr('checked', 'checked');
        jQuery(this).val('uncheck all');
    }, function() {
        jQuery('input:checkbox').removeAttr('checked');
        jQuery(this).val('check all');
    })
})

function ManagePayment(action, id) {

    //View payment
    if (action == "viewpayment") {
        jQuery("#em_input_load").show();
        jQuery("#em_payment_table").css({
            "opacity": "0.27"
        });
        jQuery.ajax({
            type: 'POST',
            url: location.href,
            data: '&action=' + action + "&id=" + id,
            success: function(response) {
                jQuery('#modal-view-payment').html(jQuery(response).find('div#inner-modal'));
                jQuery("#modal-view-payment").show();
                jQuery("#em_input_load").hide();
                jQuery("#em_payment_table").css({
                    "opacity": "1"
                });
            }
        });
    }

    //Delete payment
    if (action == "deletepayment") {
		if (confirm('Are you sure want to delete this payment?')) {
			jQuery.ajax({
				type: 'POST',
				url: location.href,
				data: '&action=' + action + "&id=" + id,
				success: function(response) {
					jQuery("#pmt_record-" + id).fadeOut(1500, "linear");
					jQuery("#datatables-1").DataTable()
					jQuery('#all_checked_payment:button').toggle(function() {
						jQuery('input:checkbox').attr('checked', 'checked');
						jQuery(this).val('uncheck all');
					}, function() {
						jQuery('input:checkbox').removeAttr('checked');
						jQuery(this).val('check all');
					})
				}
			});
		}
    }

    //Delete all payments
    if (action == "deleteallpayment") {
        if (confirm('Are you sure want to delete all selected payments?')) {
            var AllPayments = [];
            jQuery('input:checkbox:checked').map(function() {
                if (jQuery.isNumeric(this.value)) {
                    AllPayments.push(this.value);
                }
            });
            //console.log(AllPayments);
            if (AllPayments.length) {
                jQuery.ajax({
                    type: 'POST',
                    url: location.href,
                    data: '&action=' + action + "&id=" + AllPayments,
                    success: function(response) {
                        for (i = 0; i < AllPayments.length; i++) {
                            jQuery("#pmt_record-" + AllPayments[i]).fadeOut(1500, "linear");
                        }
						jQuery('#all_checked_payment:button').toggle(function() {
							jQuery('input:checkbox').attr('checked', 'checked');
							jQuery(this).val('uncheck all');
						}, function() {
							jQuery('input:checkbox').removeAttr('checked');
							jQuery(this).val('check all');
						})
                    }
                });
            } else {
                alert("No payments selected to delete.");
            }
        }
    }
}