<!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>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-partners-edit" th:object="${tbSchoolPartners}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">院校名称：</label>
                <div class="col-sm-8">
                    <input name="schoolId" th:field="*{schoolCnName}" class="form-control" type="text" disabled="true">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">合作院校的名称：</label>
                <div class="col-sm-8">
                    <input name="partnerName" th:field="*{partnerName}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">合作院校的图片：</label>
                <input type="hidden" id="partnerImage" th:field="*{partnerImage}">
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="singleFile" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/partners";
        $("#form-partners-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var b=$("#partnerImage").val();
                if(b == '' || b.length == 0){
                    $.modal.alertWarning("请选择院校图片！");
                    return ;
                }
                $.operate.save(prefix + "/edit", $('#form-partners-edit').serialize());
            }
        }

        initUpload();

        //初始化方法
        function initUpload() {
            var logoUrl = $("#partnerImage").val();//拿到图片路径
            var preList = new Array();
            var initialPreviewConfig = new Array();
            //设置初始化区域值
            if (logoUrl != null && logoUrl != ''){
                var a = {
                    // caption : '主图'+(i+1),
                    // width: '120px',
                    url: '/common/del',
                    // key: i,
                    extra: {url: logoUrl}
                }
                preList.push(logoUrl);
                initialPreviewConfig.push(a);
            }

            $("#singleFile").fileinput({
                language: 'zh', //设置语言
                uploadUrl: "/common/upload", //上传的地址，改成自己的
                allowedFileExtensions:['bmp', 'gif', 'jpg', 'jpeg', 'png'],//接收的文件后缀
                initialPreviewAsData:true,
                initialPreview:preList,//预览图片
                uploadAsync: true, //默认异步上传
                showUpload: true, //是否显示上传按钮
                showRemove : false, //显示移除按钮
                showPreview : true, //是否显示预览
                showCaption: false,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                dropZoneEnabled: false,//是否显示拖拽区域
                maxFileCount: 1, //表示允许同时上传的最大文件个数
                maxFileSize:10 *1024 *1024,
                messages: {maxFileSize:'文件上传的最大大小为 10MB',acceptFileTypes:'此文件是不支持的图片格式'    },
                enctype: 'multipart/form-data',
                validateInitialCount:true,
                msgFilesTooMany:5,
                autoReplace:false,
                initialPreviewConfig:initialPreviewConfig
            });

            $("#singleFile").on('fileuploaded', function (event, data, previewId, index) {
                var rsp = data.response;
                $("#partnerImage").val(rsp.fileName);
                $("#singleFile").fileinput('destroy');//销毁fileUploadFileInput
                initUpload();//重新初始化文件预览数据和文件上传附件
                $('#singleFile').fileinput('enable');//enable作用：destroy并重新初始化fileinput插件后，插件会处于disable状态
            });

            $('#singleFile').on('filepredelete', function(event, key, jqXHR, data) {  //就是在删除原图片之前触发，而新选择的图片不会触发。能满足我们的要求。
                console.log('Key = ' + key);
                var logoUrl = $("#partnerImage").val();
                var urlLinkNew = '';
                var imgArr = logoUrl.split(",");
                for ( var i = 0; i < imgArr.length; i++) {
                    if (i == key) continue;
                    if (urlLinkNew !=''){
                        urlLinkNew = urlLinkNew + "," + imgArr[i];
                    }else{
                        urlLinkNew = imgArr[i];
                    }
                }
                $("#partnerImage").val(urlLinkNew);
                console.log($("#partnerImage").val())
            });
        }
    </script>
</body>
</html>