<?php
/**
 * 添加文章
 */
if (!empty($_GET['id'])) {
    $title = '编辑文章-' . $_GET['id'];
} else {
    $title = '添加文章';
}
include "header.php";
if (!empty($_QET['id'])) {
    $SQ = $DB->get_row("SELECT * FROM `sky_notice` WHERE `id` = '{$_QET['id']}' ");
    if (!$SQ) show_msg('文章->' . $_QET['id'] . '不存在', '文章->' . $_QET['id'] . '不存在请检查是否访问有误?', 3);
}
?>

<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="mb-3 header-title text-success"><?= $title ?></h3>
                        <div class="form-horizontal layui-form">
                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">文章标题</label>
                                <input type="text" name="title" lay-verify="required"
                                       class="form-control" value="<?= $SQ['title'] ?>" placeholder="请输入文章标题">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">发布时间</label>
                                <input type="text" name="date" id="test" lay-verify="required"
                                       class="form-control" value="" placeholder="文章发布的时间">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">浏览用户</label>
                                <input type="text" name="browse"
                                       class="form-control" value="<?= $SQ['browse'] ?>"
                                       placeholder="可输入用户ID 用 | 分割,不需要留空即可！">
                            </div>
                            <div class="form-group mb-3" style="">
                                <label for="example-input-normal"
                                       style="position: relative;bottom: 0;left: 0">文章内容</label>
                                <textarea name="content"
                                          id="content"><?= htmlspecialchars_decode(base64_decode($SQ['content'])) ?></textarea>
                            </div>
                            <button type="submit" lay-submit lay-filter="article"
                                    class="btn btn-block btn-xs btn-success">保存文章内容
                            </button>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<?php include "bottom.php"; ?>
<link href="<?= $cdnpublic ?>summernote/0.8.12/summernote.css" rel="stylesheet">
<script src="<?= $cdnpublic ?>summernote/0.8.12/summernote.min.js"></script>
<script src="<?= $cdnpublic ?>summernote/0.8.12/lang/summernote-zh-CN.min.js"></script>
<script>
    <?php
    if (!empty($_QET['id'])) {
        echo 'var dis = ' . $_QET['id'] . ';
        var date = "' . $SQ['date'] . '";
        ';
    } else {
        echo 'var dis = "";
        var date = "' . $date . '";
        ';
    }
    ?>
    $(document).ready(function () {
        $('#content').summernote({
            lang: 'zh-CN',
            placeholder:'请输入等级介绍内容,可编辑代码哦！',
            minHeight:200,
            callbacks:{
                onImageUpload: function(files, editor, $editable) {
                    UploadFiles(files,'content');
                }
            },
        });
        function UploadFiles(files,id){
            var imageData = new FormData();
            $.each( files, function( key, val ) {
                imageData.append("imageData"+key, val);
            });
            $.ajax({
                data:imageData,
                type: "POST",
                url: "ajax.php?act=image_content",
                cache: false,
                contentType: false,
                processData: false,
                success: function(imageUrl) {
                    if(imageUrl.code==1){
                        var content = '';
                        $.each( imageUrl['SrcArr'], function( key, val ) {
                            $('#'+id).summernote('editor.insertImage',val['src']);
                            content += '图片：<font color=red>'+val['name']+'</font>大小为：<font color=red>'+val['size']+'</font><br>';
                        });
                        layer.alert(content + '<hr>Ps:图片可一次上传多张！',{title:imageUrl.msg});
                    }else layer.msg(imageUrl.msg);
                },
                error: function() {
                    layer.msg('图片上传接口异常，上传失败！');
                }
            })
        }
    });

    layui.use(['layer', 'laydate', 'form'], function () {

        var laydate = layui.laydate;
        laydate.render({
            elem: '#test'
            , value: date,
            format: 'yyyy-MM-dd HH:mm:ss'
        });

        var form = layui.form;
        form.on('submit(article)', function (data) {
            data.field['content'] = $("#content").summernote('code');
            if (data.field['content'] == '') {
                layer.msg('请填写文章内容！', {icon: 2, anim: 6});
                return false;
            }
            layer.alert('是否要执行当前操作？', {
                icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                    var index = layer.msg('数据保存中,请稍后...', {icon: 16, time: 999999});
                    if (dis != '') {
                        data.field['dis'] = dis;
                    }
                    $.post('ajax.php?act=article_msg', data.field, function (res) {
                        if (res.code == 1) {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn: ['继续', '返回列表'],
                                btn1: function (layero, index) {
                                    location.reload();
                                },
                                btn2: function (layero, index) {
                                    location.href = 'admin.article.list.php'
                                }
                            });
                        } else {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn1: function (layero, index) {
                                    location.reload();
                                }
                            });
                        }
                    });
                }
            });
            return false;
        })

    });
</script>


