<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改材料样例')" />
<!--    <th:block th:include="include :: summernote-css" />-->
    <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-demo-edit" th:object="${tbMaterialDemo}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-2 control-label is-required">材料名称：</label>
                <div class="col-sm-8">
                    <input name="materialName" th:field="*{materialName}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">材料封面：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{materialCover}">
                    <div class="file-loading">
                        <input id="singleFile" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label is-required">材料内容：</label>
                <div class="col-sm-10">
                    <script id="editor" name="materialContent" type="text/plain" style="height: 300px;"></script>
                    <textarea id="materialContent" style="display: none;">[[*{materialContent}]]</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">注意事项：</label>
                <div class="col-sm-8">
                    <input name="note" th:field="*{note}" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: ueditor-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/demo";
        $("#form-demo-edit").validate({
            focusCleanup: true
        });

        $(function () {
            var text = $("#materialContent").text();
            var ue = UE.getEditor('editor');
            ue.ready(function () {
                ue.setContent(text);
            });
        })

        function getContentTxt() {
            return UE.getEditor('editor').getContentTxt();
        }
        function submitHandler() {
            if ($.validate.form()) {
                var text = getContentTxt();
                if (text == '' || text.length == 0) {
                    $.modal.alertWarning("请输入材料内容！");
                    return;
                }
                $.operate.save(prefix + "/edit", $('#form-demo-edit').serialize());
            }
        }

        //初始化文件预览数据和文件上传附件
        initUpload();

        //初始化方法
        function initUpload() {
            var logoUrl = $("#materialCover").val();//拿到图片路径
            var preList = new Array();
            var initialPreviewConfig = new Array();
            //设置初始化区域值
            if (logoUrl != null && logoUrl != ''){
                var imgArr = logoUrl.split(",");
                for ( var i = 0; i < imgArr.length; i++) {
                    preList[i] =  imgArr[i]
                    var a = {
                        // caption : '主图'+(i+1),
                        // width: '120px',
                        url: '/common/del',
                        key: i,
                        extra: {url: imgArr[i]}
                    }
                    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;
            var imgPath = $("#materialCover").val();
            if(imgPath != null && imgPath != ''){
                $("#materialCover").val(imgPath+","+rsp.fileName);
            }else{
                $("#materialCover").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 = $("#materialCover").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];
                }
            }
            $("#materialCover").val(urlLinkNew);
            console.log($("#materialCover").val())
        });
    </script>
</body>
</html>