<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('用户列表')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
    <input class="file-upload" id="file" name="file" type="file" data-min-file-count="1">
    <input type="hidden" name="_file" th:value="${file}">
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script>

    /**附件组件多图片回显JS**/
    let newFilePs = []; //图片回显
    let imgConfig = []; //预览参数
    let uploadImageArray = [];//所有已上传的图片路径
    let fileP=$("input[name='_file']").val();
    let urlsArr = ($("input[name='_file']").val()).split(",");
    for(let i in urlsArr) {
        if (urlsArr[i] != "") {
            uploadImageArray.push(urlsArr[i]);
            newFilePs[i] = urlsArr[i];
            let fileType = urlsArr[i].substr(urlsArr[i].lastIndexOf(".")+1,urlsArr[i].length-1);
            let ftype ="image"; //文件类型
            if(fileType == "pdf" || fileType == "doc" || fileType == "docx"|| fileType == "xls"|| fileType == "xlsx"|| fileType == "ico"){
                ftype = fileType;
            }
            imgConfig[i] = {
                url: "/common/removeFile",
                downloadUrl:urlsArr[i], //下载路径，此项不设置的话下载按钮不显示
                type:ftype,             //文件类型
                key: urlsArr[i],        //参数，用于文件删除向后台传参
                caption:urlsArr[i].substr(urlsArr[i].lastIndexOf("/")+1,urlsArr[i].length-1)    //文件名，用于预览文件显示的文件名
            };
        }
    }


    $(".file-upload").each(function (i) {
        $(this).fileinput({
            language: 'zh', //设置语言
            uploadUrl: '/common/upload',
            deleteUrl: '/common/removeFile',
            allowedFileExtensions: ['pdf','docx','doc','jpg','png'],//接收的文件后缀
            showUpload: false, //是否显示上传按钮
            showRemove: false, //显示移除按钮
            showPreview: true, //是否显示预览
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            dropZoneEnabled: true,//是否显示拖拽区域
            initialPreviewAsData: true,
            enctype: 'multipart/form-data',
            initialPreview: newFilePs,
            uploadAsync: true, //默认异步上传（true）,回调fileuploaded，false为同步上传，回调filebatchuploadsuccess
            maxFileCount: 6,
            overwriteInitial:false,    //不覆盖已存在的图片
            initialPreviewConfig: imgConfig, //配置预览的参数
            autoReplace: true,
            preferIconicPreview: true,// 强制缩略图显示以下文件扩展名的图标
            previewFileIconSettings: {
                'doc': '<i class="fa fa-file-word-o text-primary"></i>',
                'xls': '<i class="fa fa-file-excel-o text-success"></i>',
                'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
                'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
                'htm': '<i class="fa fa-file-code-o text-info"></i>',
                'txt': '<i class="fa fa-file-text-o text-info"></i>',
                'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
                'mp3': '<i class="fa fa-file-audio-o text-warning"></i>',
            },
            previewFileExtSettings: { // configure the logic for determining icon file extensions
                'doc': function(ext) {
                    return ext.match(/(doc|docx)$/i);
                },
                'xls': function(ext) {
                    return ext.match(/(xls|xlsx)$/i);
                },
                'ppt': function(ext) {
                    return ext.match(/(ppt|pptx)$/i);
                },
                'zip': function(ext) {
                    return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i);
                },
                'htm': function(ext) {
                    return ext.match(/(htm|html)$/i);
                },
                'txt': function(ext) {
                    return ext.match(/(txt|ini|csv|java|php|js|css)$/i);
                },
                'mov': function(ext) {
                    return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i);
                },
                'mp3': function(ext) {
                    return ext.match(/(mp3|wav)$/i);
                }
            }
        }).on('fileuploaded', function (event, data, previewId, index) {
            fileP += data.response.fileName+",";
            $('#zoom-'+previewId).find(".kv-preview-data").attr("src",data.response.fileName);//上传成功后替换文件路径（替换为实际数据库的存储路径）
            $("input[name='_" + event.currentTarget.id + "']").val(data.response.fileName);
        }).on("filebatchselected", function (event, previewId, files) {//选中文件后执行
            $(".file-upload").fileinput("upload");
        }).on('fileremoved', function (event, id, index) {//预览时点击缩略图上的删除按钮才能触发的
            console.log('fileremoved');
            getMeg();
        }).on("filepredelete", function (event, key, jqXHR, data) {   //预览图片删除
            console.log('filepredelete')
            getMeg();
        }).on("filesuccessremove", function (event, id) {   //使用缩略图删除按钮删除成功上传的缩略图后
            console.log('filesuccessremove')
            getMeg();
        })
        $(this).fileinput('_initFileActions');
    });

    function nowImgTotal() {
        return $(".file-preview-thumbnails").find('.file-preview-frame').length / 2;
    }

    function getMeg() {
        if (nowImgTotal() >= 6) {
            $('#file').attr('disabled', 'disabled');
            $.modal.alert("图片允许上传的最大数值为6，请删除需要被替换的图片后再进行上传操作!")
            return false;
        }else{
            $('#file').removeAttr("disabled");
        }
    }

    /*
        获取当前页面图片附件的存储路径
     */
    function getImgUrl(){
        var initFileDiv = $(".kv-zoom-cache");
        var imgUrl = [];
        initFileDiv.each(function(i,e){
            var fullPath = $(this).find(".kv-preview-data").attr("src");
            if(fullPath!=null&&fullPath!=""&&fullPath.length>=2)
                imgUrl[i]="/profile/upload/"+(fullPath.split("upload/",fullPath.length-1))[1];
        });
        return imgUrl;
    }

</script>
</html>

