$().ready(function () {

    $('.summernote').summernote({
        height: '220px',
        lang: 'zh-CN',
        callbacks: {
            onImageUpload: function (files, editor, $editable) {
                sendFile(files);
            }
        }
    });
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save(1);
    }
});

function save(status) {
    $("#status").val(status);
    var content_sn = $("#content_sn").summernote('code');
    $("#content").val(content_sn);
    let valid = $("#signupForm").valid()
    if (valid) {
        $.ajax({
            cache: true,
            type: "POST",
            url: "/blog/bContent/save",
            data: $('#signupForm').serialize(),// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (r) {
                if (r.code == 0) {
                    parent.layer.msg(r.msg);
                    parent.reLoad();
                    $("#cid").val(r.cid);

                } else {
                    parent.layer.alert(r.msg)
                }
            }
        });
    }
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    return $("#signupForm").validate({
        rules: {
            title: "required",
            author: "required",
            content: "required"
        },
        messages: {
            title: "请填写文章标题",
            author: "请填写文章作者",
            content: "请填写文章内容"
        }
    });
}

function returnList() {
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}


function sendFile2(files, editor, $editable) {
    var size = files[0].size;
    if ((size / 1024 / 1024) > 20) {
        alert("图片大小不能超过20M...");
        return false;
    }
    var formData = new FormData();
    formData.append("file", files[0]);
    $.ajax({
        data: formData,
        type: "POST",
        url: "/common/sysFile/upload",    // 图片上传出来的url，返回的是图片上传后的路径，http格式
        cache: false,
        contentType: false,
        processData: false,
        dataType: "json",
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            $('.carouselPic').summernote('insertImage', data.fileName);
        },
        error: function () {
            alert("上传失败");
        }
    });
}

