<title>文章表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsArticle/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" placeholder="请输入标题" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="typeId" id="typeId">
                        <option value="">请选择所属分类</option>
                        {{# layui.each(d.data.categories, function(index, item){ }}
                        <option value="{{ item.id }}">{{ item.name }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="isPub" id="isPub">
                        <option value="">请选择是否发布</option>
                        <option value="True">是</option>
                        <option value="False">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline" style="width: 260px;">
                    <input type="text" name="createTime" id="searchTime-CoreCmsArticle-createTime" placeholder="请输入创建时间" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsArticle-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsArticle-tableBox" lay-filter="LAY-app-CoreCmsArticle-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsArticle-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
    </div>
</script>
<script type="text/html" id="LAY-app-CoreCmsArticle-tableBox-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coreHelper'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , view = layui.view;

                laydate.render({
                    elem: '#searchTime-CoreCmsArticle-createTime',
                    type: 'datetime',
                    range: '到',
                });
                laydate.render({
                    elem: '#searchTime-CoreCmsArticle-updateTime',
                    type: 'datetime',
                    range: '到',
                });
                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsArticle-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsArticle-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsArticle-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsArticle/GetPageList',
                    method: 'POST',
                    toolbar: '#LAY-app-CoreCmsArticle-toolbar',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-189',//无面包屑127,搜索框189,1行62
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { field: 'id', title: '序列', width: 60, sort: false },
                            { field: 'title', title: '标题', sort: false },
                            {
                                field: 'coverImage', title: '封面图', width: 80, sort: false,
                                templet: function (d) {
                                    if (d.coverImage) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.coverImage + '")><image style="max-width:28px;max-height:28px;" src="' + d.coverImage + '" /></a>';
                                    } else {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + setter.noImagePicUrl + '")><image style="max-width:30px;max-height:30px;" src="' + setter.noImagePicUrl + '" /></a>';
                                    }
                                }
                            },
                            {
                                field: 'typeId', title: '分类', sort: false, width: 105,
                                templet: function (data) {
                                    for (var i = 0; i < d.data.categories.length; i++) {
                                        if (data.typeId == d.data.categories[i].id) {
                                            return d.data.categories[i].name;
                                        }
                                    }
                                    return "";
                                }
                            },
                            { field: 'sort', title: '排序', sort: false, width: 105 },
                            { field: 'isPub', title: '是否发布', width: 95, templet: '#switch_isPub', sort: false, unresize: true },
                            { field: 'isDel', title: '是否删除', width: 95, templet: '#switch_isDel', sort: false, unresize: true },
                            { field: 'pv', title: '访问量', sort: false, width: 105 },
                            { field: 'createTime', title: '创建时间', width: 130, sort: false },
                            { field: 'updateTime', title: '更新时间', width: 130, sort: false },
                            { width: 122, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsArticle-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsArticle-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsArticle-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //头工具栏事件
                table.on('toolbar(LAY-app-CoreCmsArticle-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                    };
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsArticle-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {

                    coreHelper.Post("Api/CoreCmsArticle/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['1200px', '95%'],
                                id: 'LAY-popup-CoreCmsArticle-create',
                                success: function (layero, index) {
                                    view(this.id).render('article/articles/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArticle-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isPub = field.isPub == 'on';
                                                field.isDel = field.isDel == 'on';
                                                field.contentBody = window.editor.getData();
                                                if (!!!field.contentBody) {
                                                    layer.msg('文章详情内容不能为空', { icon: 5 });
                                                    return false;
                                                }

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArticle/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsArticle-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArticle-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsArticle/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['1200px', '95%'],
                                id: 'LAY-popup-CoreCmsArticle-edit',
                                success: function (layero, index) {
                                    view(this.id).render('article/articles/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArticle-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isPub = field.isPub == 'on';
                                                field.isDel = field.isDel == 'on';

                                                if (!!!field.typeId) {
                                                    layer.msg("请选择文章分类", { icon: 5 });
                                                    return false;
                                                }

                                                field.contentBody = window.editor.getData();
                                                if (!!!field.contentBody) {
                                                    layer.msg('文章详情内容不能为空', { icon: 5 });
                                                    return false;
                                                }

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArticle/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsArticle-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArticle-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsArticle/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            table.reload('LAY-app-CoreCmsArticle-tableBox');
                            layer.msg(e.msg);
                        });
                    });
                }

                layui.form.on('switch(switch_isPub)', function (obj) {
                    coreHelper.Post("Api/CoreCmsArticle/DoSetisPub", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reload('LAY-app-CoreCmsArticle-tableBox');
                        layer.msg(e.msg);
                    });
                });

                layui.form.on('switch(switch_isDel)', function (obj) {
                    coreHelper.Post("Api/CoreCmsArticle/DoSetisDel", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reload('LAY-app-CoreCmsArticle-tableBox');
                        layer.msg(e.msg);
                    });
                });

            });
    };
</script>

<!--设置是否发布-->
<script type="text/html" id="switch_isPub">
    <input type="checkbox" name="switch_isPub" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isPub" {{ d.isPub ? 'checked' : '' }}>
</script>

<!--设置是否删除-->
<script type="text/html" id="switch_isDel">
    <input type="checkbox" name="switch_isDel" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isDel" {{ d.isDel ? 'checked' : '' }}>
</script>