// 获取上传按钮和文件输入框元素
const uploadButton = document.getElementById('add-file-btn');
const fileInput = document.getElementById('fileInput');

// 为上传按钮添加点击事件监听器
uploadButton.addEventListener('click', function() {
    // 触发文件输入框的点击事件，以打开文件选择对话框
    fileInput.click();
});

// 为文件输入框添加变化事件监听器，以便在文件选择后上传
fileInput.addEventListener('change', function() {
    // 检查是否选择了文件
    if (this.files.length > 0) {
        const file = this.files[0]; // 获取选择的文件
        uploadFile(file); // 调用上传文件的函数
    }
});


var uploadImages = [];
var uploadVideo = "";

// 上传文件的函数
function uploadFile(file) {
    const formData = new FormData();
    formData.append('file', file); // 将文件添加到FormData对象中

    fetch('/api/file/moment/upload', { // 发送POST请求到服务器的/upload端点
        method: 'POST',
        body: formData,
    })
        .then(response => response.json()) // 处理响应文本
        .then(data => {
            if (data.success) {
                var file = data.data;
                if (file.type == 'IMAGE') {
                    // 上传图片需要清空视频
                    uploadVideo = "";
                    uploadImages.push(file.url);
                    if (uploadImages.length == 1) {
                        $("#upload-container").empty();
                    } else if (uploadImages.length == 9) {
                        $("#add-file-btn").hide();
                    } else if (uploadImages.length > 9) {
                        layer.msg("图片最多支持上传9张");
                        return;
                    }
                    $("#upload-container").append("<div class=\"upload-images\"><img src='"+file.url+"'></div>");
                } else if (file.type == 'VIDEO') {
                    // 上传视频需要清空图片
                    uploadImages = [];
                    uploadVideo = file.url;
                    $("#upload-container").empty();
                    $("#upload-container").append("<div class=\"upload-video\"><video width='300' controls autoplay name=\"media\"><source src=\""+file.url+"\" type=\"video/mp4\"></video></div>");
                    $("#add-file-btn").hide();
                } else {
                    layer.msg("不支持的文件");
                }
            } else {
                layer.msg(data.msg);
            }
        })
        .catch(error => {
            console.error('上传失败:', error); // 处理上传错误
        });
}

$("#publish-btn").on('click', function () {
    var content = $("#content").html();
    if (content == '' && videoUrl == '' && uploadImages.length == 0) {
        layer.msg("内容不能为空");
        return;
    }
    if (content.length > 2000) {
        layer.msg("正文控制在2000字以内");
        return;
    }
    $.ajax({
        url: "/api/moment/publish",
        type: "post",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            content: content,
            images: uploadImages,
            video: uploadVideo
        }),
        success:function (data) {
            data = eval(data);
            if (data.success){
                layer.msg("发布成功");
                location.href="/h5/moment"
            } else {
                layer.msg(data.msg);
            }
        },error:function () {
            layer.msg("服务端异常");
        }
    })
})