$(function() {
    var editor = editormd("editormd", {
        path: "/plugins/editor-md/lib/",
        height: 500,
        emoji: true,
        onfullscreen : function() {
            $("#editormd").css('margin-top', '46px').css('z-index', '3');
        }
    });

    $("button[type=submit]").click(function () {
        var id = $("input[name=id]").val();
        var title = $.trim($("input[name=title]").val());
        var slug = $.trim($("input[name=slug]").val());
        var category = $.trim($("input[name=category]").val());
        var tags = $.trim($("input[name=tags]").val());
        var markdown = editor.getMarkdown();
        var html = editor.getPreviewedHTML();

        if (title.length <= 0) {
            toastr['error']('Please enter title.');
            return;
        } else if (slug.length <= 0) {
            toastr['error']('Please enter slug.');
            return;
        } else if (category.length <= 0) {
            toastr['error']('Please enter category.');
            return;
        } else if (tags.length <= 0) {
            toastr['error']('Please enter tags.');
            return;
        }

        $.post('/admin/post/edit', {
            id: id,
            title: title,
            slug: slug,
            category: category,
            tags: tags,
            markdown: markdown,
            html: html
        }, function (result) {
            if (result.code === 1) {
                location.href = '/admin/post';
            } else {
                toastr['error'](result.message);
            }
        })
    });
});