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

    $('.save').click(function () {
        save(function(){});
    });

    $('.save-and-sign').click(function () {
       save(function () {
           sign();
       })
    });

    function save(callback) {
        var fingerprint = $('.server-form').data('fingerprint');

        var data = $('.server-form input,.server-form select').serializeArray();

        $.post('/panel/server/' + fingerprint, data, function (data) {
            console.log(data);
            if (!data.success) {
                $('h2').after(error.text(data.error));
                return;
            }

            callback();
        }, 'json');
    }

    function sign() {
        var fingerprint = $('.server-form').data('fingerprint');
        $.post(
            '/panel/server/sign',
            {
                fingerprint: fingerprint
            },
            function (data) {
                if (!data.success) {
                    $('h2').after(error.text(data.error));
                    return;
                }

                location.reload();
            },
            'json'
        );
    }
});