{% load staticfiles %}
<script src="{% static 'lib/ckeditor/ckeditor.js'%}"></script>
<script src="{% static 'lib/ckeditor/lang/zh-cn.js'%}"></script>
<style>
    .sendMsg .layui-input {
        width: 260px;
    }

    /* 表单样式 */

    .layui-form-item .layui-input-inline {
        width: 50%;
    }

    .layui-form-item .layui-input-textarea {
        width: 50%;
    }

    .uploadImg {
        position: absolute;
        margin: 10px 0;
        width: 92px;
        height: 92px;
        background: none;
        border: 1px solid #b8b8b8;
        cursor: pointer;
    }

    .uploadImgIcon {
        background: url(../../static/imgs/addicon.png) no-repeat center;
        background-size: 80% 80%;
    }
</style>
<form class="layui-form" action="" id="formDiv" lay-filter="newsFormDiv">
    <div class="layui-form-item">
        <label class="layui-form-label">文章标题：</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发布人：</label>
        <div class="layui-input-inline">
            <input type="text" name="writer" required lay-verify="required" placeholder="请填写发布人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章简介：</label>
        <div class="layui-input-inline layui-input-textarea">
            <textarea name="intro" lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">介绍图：</label>
        <div class="layui-input-inline">
            <button type="button" class="uploadImg uploadImgIcon" id="newsUpImg"></button>
            <!-- 隐藏的input -->
            <input type="hidden" id="img_url" name="upload" value="" />
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1">
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">文章正文：</label>
        <div class="layui-input-block">
            <textarea name="content" id="editor1" rows="10" cols="80"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
            <button class="layui-btn layui-btn-primary" type="button" onclick="clearForm()">取消</button>
        </div>
    </div>
</form>
<!-- <form action="" id="adtaForm" class="sendMsg">
    <div>
        <div class="layui-form-item">
            <label class="layui-form-label">新闻标题：</label>
            <div class="layui-input-block">
                <input type="text" name="title" required="" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <br />
    <button type="button" onclick="aa()" class="layui-btn" id="newsUpImg">
        <i class="layui-icon"></i>保存</button>
</form> -->
<!-- <button onclick="fuzhi()">赋值</button>  -->
<!-- <button onclick="insertText()">insertText</button> -->

<script type="text/javascript">
    var form, upload;
    layui.use(['form', 'upload'], function () {
        form = layui.form;
        upload = layui.upload;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var img_url = $('#img_url').val();
            if (!img_url) {
                layer.msg("请上传文章缩略图", { icon: 5 });
                return false;
            }
            var content = editor1.getData();
            // alert(content)
            if (!content) {
                layer.msg("请填写文章正文", { icon: 5 });
                return false;
            }
            field = Object.assign(data.field, { content: content });

            // layer.msg(JSON.stringify(data.field));

            CKupdate()
            console.log($("#adtaForm").serializeArray());

            $.ajax({
                url: 'ckarticle_view',
                data: $("#formDiv").serialize(),
                type: 'post',
                success: function (data) {
                    console.log(data);
                    if (data) {
                        clearForm()
                    }
                },
                error: function (type) {

                }
            })
            return false;

        });

        //新闻图片上传
        var uploadInst = upload.render({
            elem: '#newsUpImg'
            , url: 'news_photo'
            , field: 'upicture'
            , xhr: function () { } //上传视频进度条
            , before: function (obj) {
                $('.uploadImg').removeClass('uploadImgIcon')
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url").value = res.data.src;
                    console.log(res)
                } else {
                    return layer.msg('上传失败');
                }


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    });

    function clearForm() {
        form.val('newsFormDiv', {
            "title": "",
            "writer": "",
            "intro": ""
        });
        $('#img_url').val('');
        $('.uploadImg').addClass('uploadImgIcon');
        $('#demo1').removeAttr('src'); //图片链接（base64）
        fuzhi()
    }




    // 示例1：设置工具栏为基本工具栏，高度为70
    var editor1 = CKEDITOR.replace('editor1',
        {
            filebrowserBrowseUrl: 'test_aaphoto',
            height: 430,
            toolbar :
            [
                ['Undo','Redo'],
                ['Styles', 'FontSize','Format','JustifyLeft ','JustifyCenter','JustifyRight','JustifyBlock'],
                ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Image']
            ]
        }
    );


    //处理CKEDITOR的值
    function CKupdate() {
        for (instance in CKEDITOR.instances)
            CKEDITOR.instances[instance].updateElement();
    }

    // console.log($("#adtaForm").serializeArray());
    // if (!($("#adtaForm").serializeArray()[0].value && $("#adtaForm").serializeArray()[1].value)) {
    //     alert('请填写完整')
    //     return false;
    // }

    function fuzhi() { //编辑器赋值
        editor1.setData("");
    }

    function insertText(value) { //插入文本
        editor1.insertText('value');
    }
</script>

</body>

</html>