'use strict';
let el = {
    excelFile: '#fb'
};

$(function () {
    // 初始化文件上传控件
    $(el.excelFile).filebox({
        buttonText: popupi18n.xzwj,
        buttonAlign: 'right',
        accept: '.xlsx',
        multiple: false,
        prompt: '请选择excel文件',
        width: 400
    });
});

// 下载导入模板
function downloadTemplate() {
    topWin.window.open("");
}

function uploadFile(call) {
    var filebox = $(el.excelFile).filebox("files");
    if (filebox.length == 1) {
        var formData = new FormData();
        formData.append("file", filebox[0]);
        var index = topWin.layer.msg('0%', {icon: 16, time: 0, shade: [0.8, '#393D49']});
        $.ajax({
            url: g.ctx + 'pc/sys/file/rest/upload/file',
            data: formData,
            type: 'post',
            contentType: false,
            processData: false,
            xhr: function () {
                var xhr = new XMLHttpRequest();
                xhr.upload.addEventListener('progress', function (e) {
                    var progressRate = ((e.loaded / e.total) * 100).toFixed(0) + '%';
                    topWin.layer.close(index);
                    index = topWin.layer.msg(progressRate, {
                        icon: 16,
                        time: 0,
                        shade: [0.8, '#393D49']
                    });
                });
                return xhr;
            },
            success: function (d) {
                topWin.layer.close(index);
                call(d.id)
            }, error: function (d) {
                topWin.layer.close(index);
                topWin.layer.msg("上传文件错误!");
                $(el.excelFile).filebox("clear");
            }
        })
    } else {
        topWin.msg("请选文件")
    }
}