$(function(){
    var error   = $('<div class="alert alert-danger" role="alert"></div>');
    var success = $('<div class="alert alert-success alert-dismissable" role="alert"></div>');
    var selected = false;

    $('.revoke').click(function () {
        selected = $(this);

        $('.revoke-modal').modal('show');
        error.hide();
    });
    
    $('.revoke-confirm').click(function () {
        var selectedSnapshot = selected;
        error.hide();

        $.post('/panel/certificates/revoke', {
            name: selectedSnapshot.data('name'),
            password: $('.revoke-password').val()
        }, function(data) {
            if (data.success) {
                $('#certificates').after(success.text("Successfully revoked '" + selectedSnapshot.data('name') + "'").delay(3000).hide(0));
                $('.revoke-modal').modal('hide');
                $(selectedSnapshot).parents('tr').first().remove();
            } else {
                $('#revoke-put-error-after-me').after(error.text(data.error).show());
            }
        });

        $('.revoke-password').val('');
    });
    
    $('.remove').click(function () {
        var that = this;
        $.post('/panel/server/remove',{ fingerprint: $(this).data('fingerprint') }, function () {
           $(that).parents('tr').first().remove();
        });
    });
});