jQuery(document).ready(function() {
    jQuery('#all_checked_ticket: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 ManageTicket(action, id) {
    //add ticket
    if (action == "addticket") {
        jQuery("#em_input_load").show();
        jQuery("#em_ticket_table").css({
            "opacity": "0.27"
        });
        jQuery.ajax({
            type: 'POST',
            url: location.href,
            data: jQuery('#em_ticket_form').serialize() + '&action=' + action,
            success: function(response) {
                jQuery('#em_ticket_table').html(jQuery(response).find('div#em_ticket_table'));
                jQuery("#datatables-1").remove();
                jQuery("#datatables-1").DataTable()
                jQuery('#all_checked_ticket: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');
                })
                jQuery("#em_input_load").hide();
                jQuery("#em_ticket_table").css({
                    "opacity": "1"
                });
				jQuery("#em_ticket_form").trigger('reset');
            }
        });
    }

    //Edit ticket
    if (action == "editticket") {
        var tkt_category = jQuery("#tkt_category").val();
        jQuery("#em_input_load").show();
        jQuery("#em_ticket_table").css({
            "opacity": "0.27"
        });
        jQuery.ajax({
            type: 'POST',
            url: location.href,
            data: '&action=' + action + "&id=" + id,
            success: function(response) {
                jQuery('#modal-edit-ticket').html(jQuery(response).find('div#inner-modal'));
				jQuery("#modal-edit-ticket").show();
                jQuery("#em_input_load").hide();
                jQuery("#em_ticket_table").css({
                    "opacity": "1"
                });
            }
        });
    }

    //Update ticket
    if (action == "updateticket") {
        var em_edit_ticket_name = jQuery("#em_edit_ticket_name").val();
        var em_edit_ticket_desc = jQuery("#em_edit_ticket_desc").val();
        var em_edit_ticket_price = jQuery("#em_edit_ticket_price").val();
        var em_edit_ticket_status = jQuery("#em_edit_ticket_status").val();
        var em_edit_ticket_category_div = jQuery("#em_edit_ticket_category_div").val();
        jQuery("#em_input_load").show();
        jQuery("#em_ticket_table").css({
            "opacity": "0.27"
        });
        jQuery.ajax({
            type: 'POST',
            url: location.href,
            data: jQuery('#em_ticket_up_form').serialize() + '&action=' + action + "&id=" + id,
            success: function(response) {
                jQuery('#em_ticket_table').html(jQuery(response).find('div#em_ticket_table'));
                jQuery("#datatables-1").remove();
                jQuery("#datatables-1").DataTable()
                jQuery('#all_checked_ticket: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');
                })
                jQuery("#em_input_load").hide();
                jQuery("#em_ticket_table").css({
                    "opacity": "1"
                });
            }
        });
    }

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

    //Delete ticket
    if (action == "deleteticket") {
		if (confirm('Are you sure want to delete this ticket?')) {
			jQuery.ajax({
				type: 'POST',
				url: location.href,
				data: '&action=' + action + "&id=" + id,
				success: function(response) {
					jQuery("#tkt_record-" + id).fadeOut(1500, "linear");
					jQuery("#datatables-1").DataTable()
					jQuery('#all_checked_ticket: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 ticket
    if (action == "deleteallticket") {
        if (confirm('Are you sure want to delete all selected tickets?')) {
            var AllTickets = [];
            jQuery('input:checkbox:checked').map(function() {
                if (jQuery.isNumeric(this.value)) {
                    AllTickets.push(this.value);
                }
            });
            //console.log(AllTickets);
            if (AllTickets.length) {
                jQuery.ajax({
                    type: 'POST',
                    url: location.href,
                    data: '&action=' + action + "&id=" + AllTickets,
                    success: function(response) {
                        for (i = 0; i < AllTickets.length; i++) {
                            jQuery("#tkt_record-" + AllTickets[i]).fadeOut(1500, "linear");
                        }
                    }
                });
            } else {
                alert("No Tickets selected to delete.");
            }
        }
    }
}