let fileInput = document.getElementById('file-input');

function goConfirm() {
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/message/addressee/rest/confirm',
        type: 'POST',
        data: {
            id: $('#id').val()
        },
        success: function (data) {
            parent.layer.alert('确认成功',{icon: 1, shadeClose: true, title: '温馨提示'},function (index) {
                parent.layer.close(index);
                window.location.reload();
            });
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (data) {
            parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
        }
    });
}



function closeWindow(){
debugger;
    $.ajax({
        url: g.ctx + 'pc/message/addressee/rest/confirmDetails',
        method: "post",
        data: {id: $('#id').val()},
        success: function (data) {
            console.log(data)

            if(data.isSupplierReply == 1 && data.isSupplierReplied !== 1){//是否供应商回复
                topWin.layer.msg('请进行回复！', {icon: 2});
                return;
            }

            if(data.isSupplierConfirm == 1 && data.isSupplierConfirmed !== 1){//是否供应商确认
                topWin.layer.msg('请进行确认！', {icon: 2});
                return;
            }

            if(data.isSupplierFile == 1 && data.isSupplierFiled !== 1){//是否供应商上传
                topWin.layer.msg('请进行确认文件上传！', {icon: 2});
                return;
            }
            parent.layer.closeAll();
        },
        error: function (data) {
            parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
        }
    });
}

function goReply() {
    topWin.layer.prompt({title: "请填写回复内容", formType: 2, value: '', maxlength: 500}, function(text, index){
        if(text ===''){
            topWin.layer.msg('请填写回复内容', {icon: 2});
            return;
        }
        var loading = topWin.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/message/addressee/rest/reply',
            method: "post",
            data: {id: $('#id').val(),replyContent:text},
            success: function () {
                topWin.layer.close(index);
                parent.layer.alert('回复成功',{icon: 1, shadeClose: true, title: '温馨提示'},function (index) {
                    parent.layer.close(index);
                    window.location.reload();
                });
            },
            complete: function(data) {
                topWin.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

function goUpload() {
    $('#file-input').click();
}
fileInput.addEventListener('change', function(event) {
    const files = event.target.files;
    let formData = new FormData();
    // 处理文件
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        formData.append("files", file);
    }
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/sys/file/rest/upload/files', type: 'POST', data: formData, processData: false,// ⑧告诉jQuery不要去处理发送的数据
        contentType: false,
        success: function (data) {
            var fileIdList = [];
            for (var i = 0; i < data.length; i++) {
                fileIdList.push(data[i].id)
            }
            var replyFile = fileIdList.join(',')
            $.ajax({
                url: g.ctx + 'pc/message/addressee/rest/replyFile',
                type: 'POST',
                data: {
                    id: $('#id').val(),
                    replyFile: replyFile,
                },
                success: function (data) {
                    parent.layer.alert('上传成功',{icon: 1, shadeClose: true, title: '温馨提示'},function (index) {
                        parent.layer.close(index);
                        window.location.reload();
                    });
                },
                complete: function(data) {
                    top.layer.close(loading);
                },
                error: function (data) {
                    parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
                }
            });
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (responseStr) {
            parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
        }
    });
});