<!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-detail-edit" th:object="${playletPublicDetail}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">公众号：</label>
            <div class="col-sm-8">
                <select id="publicId" name="publicId" class="form-control m-b" required>
                    <option th:each="publicAccount:${publicAccounts}" th:value="${publicAccount.id}" th:text="${publicAccount.name}" th:field="*{publicId}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">标题：</label>
            <div class="col-sm-8">
                <input name="title" th:field="*{title}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">作者花名：</label>
            <div class="col-sm-8">
                <input name="authorAlias" th:field="*{authorAlias}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">缩略图：</label>
            <div class="col-sm-8">
                <input name="imgUrl" th:field="*{imgUrl}" type="text" hidden="hidden">
                <div class="file-loading">
                    <input id="imgUrlFile" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">头图：</label>
            <div class="col-sm-8">
                <input name="headUrl" th:field="*{headUrl}" type="text" hidden="hidden">
                <div class="file-loading">
                    <input id="headUrlFile" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">文章类型：</label>
            <div class="col-sm-8">
                <label class="radio-box">
                    <input type="radio" th:checked="${playletPublicDetail.type == '01' ? true : false}" value="01" name="type">富文本</label>
                <label class="radio-box">
                    <input type="radio" th:checked="${playletPublicDetail.type == '02' ? true : false}" value="02" name="type">PDF</label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">PDF附件：</label>
            <div class="col-sm-8">
                <input name="pdfUrl" type="text" hidden="hidden">
                <div class="file-loading">
                    <input id="pdfUrlFile" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">所在地：</label>
            <div class="col-sm-8">
                <input name="address" th:field="*{address}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">简介：</label>
            <div class="col-sm-8">
                <input name="remark" th:field="*{remark}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">文章标签：</label>
            <div class="col-sm-8">
                <select name="detailTag" class="form-control m-b" th:with="type=${@dict.getType('playlet_detail_tag')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictLabel}" th:field="*{detailTag}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">详情(富文本)：</label>
            <div class="col-sm-8">
                <input type="hidden" class="form-control" th:field="*{content}">
                <div class="summernote" id="content"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/playlet/detail";
    $("#form-detail-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-detail-edit').serialize());
        }
    }

    // 详情页
    $("#imgUrlFile").fileinput({
        uploadUrl: ctx + 'tool/oss/uploadFromPlaylet',
        maxFileCount: 1,
        dropZoneEnabled: false,      //是否显示拖拽区域
        showPreview: false,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        console.log(data);
        if(data.response.success){
            $("input[name='imgUrl']").val(data.response.msg)
        }else {
            alert("上传失败!");
        }
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
        $("input[name='imgUrl']").val('')
    })

    $("#headUrlFile").fileinput({
        uploadUrl: ctx + 'tool/oss/uploadFromPlaylet',
        maxFileCount: 1,
        dropZoneEnabled: false,      //是否显示拖拽区域
        showPreview: false,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        console.log(data);
        if(data.response.success){
            $("input[name='headUrl']").val(data.response.msg)
        }else {
            alert("上传失败!");
        }
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
        $("input[name='headUrl']").val('')
    })

    // 详情页
    $("#pdfUrlFile").fileinput({
        uploadUrl: ctx + 'tool/oss/uploadFromPlaylet',
        maxFileCount: 1,
        dropZoneEnabled: false,      //是否显示拖拽区域
        showPreview: false,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        console.log(data);
        if(data.response.success){
            $("input[name='pdfUrl']").val(data.response.msg)
        }else {
            alert("上传失败!");
        }
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
        $("input[name='pdfUrl']").val('')
    })

    $(function() {
        $('.summernote').each(function(i) {
            $('#' + this.id).summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                height: 300,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            var content = $("input[name='" + this.id + "']").val();
            $('#' + this.id).summernote('code', content);
        })
    });
</script>
</body>
</html>