<!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-demo-add">
            <div class="form-group">    
                <label class="col-sm-2 control-label is-required">材料名称：</label>
                <div class="col-sm-8">
                    <input name="materialName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">材料封面：</label>
                <input name="materialCover" type="hidden" id="materialCover"/>
                <div class="col-sm-8">
                    <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>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">注意事项：</label>
                <div class="col-sm-8">
                    <input name="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";

        var ue = UE.getEditor('editor');

        function getContentTxt() {
            return UE.getEditor('editor').getContentTxt();
        }

		$("#form-demo-add").validate({
            focusCleanup: true
        });

        $(document).ready(function () {
            // 单图上传
            $("#singleFile").fileinput({
                uploadUrl: ctx + 'common/upload',
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                var rsp = data.response;
                log.info("return url：" + rsp.url)
                log.info("reutrn fileName：" + rsp.fileName)
                log.info("reutrn newFileName：" + rsp.newFileName)
                log.info("return originalFilename：" + rsp.originalFilename)
                $("#materialCover").val(rsp.fileName);
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
        });

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