<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <title>文章列表</title>
    <% include("base/meta.html"){} %>
</head>

<body>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="/admin/console/page">首页</a>
        <a><cite>文章列表</cite></a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <%if(array.contain(session.authContext.permissionPath,"/admin/save/article")){%>
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('创建文章','/admin/save/article',900,500)"><i
                            class="fa fa-plus"></i>创建文章
                    </button>
                </div>
                <%}%>
                <div class="layui-card-body ">
                    <table id="postList" lay-filter="postList"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>


<script type="text/html" id="toolbarDemo">
    <input class="text" style="height:30px;width:157px;" name="title" id="title" placeholder="请输入标题">
    <a href="#" lay-filter="" style="position: absolute;left: 144px;" onclick="searchTitle()">
        <i class="layui-icon layui-icon-search" style="background:#fff;padding:5px;"></i>
    </a>
</script>

<script type="text/html" id="operation">
    <div class="layui-btn-container">
        {{# if(d.draftId){ }}
        <button class="layui-btn layui-btn-warm" lay-event="draft"> 草稿</button>
        {{# } else{ }}
        <button class="layui-btn layui-btn-normal" lay-event="edit"> 编辑</button>
        {{# } }}
        <button class="layui-btn layui-btn-danger" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="enable" value="{{d.enable}}" lay-skin="switch" lay-text="启用|停用"
           lay-filter="selectEnable" {{ d.enable ? 'checked': ''}}>
</script>


<style>
    pass{
        color: #46c571;
    }
    reject{
        color: red;
    }
</style>

<script type="text/html" id="statusTpl">
    {{# if(d.status=='pass'){ }}
    <pass>通过</pass>
    {{# } else if(d.status=='in_progress'){ }}
    审核中
    {{# } else if(d.status=='reject'){ }}
    <reject>未通过</reject>
    {{# }else{ }}
    未知
    {{# } }}
</script>

<script type="text/html" id="titleTpl">
    {{# if(d.status=='reject'){ }}
        {{d.title}} [<reject>{{d.reason}}</reject>]
    {{# }else{ }}

        {{d.title}}

    {{# } }}
</script>
<% include("base/bottom.html"){} %>

<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            toolbar: '#toolbarDemo',
            elem: '#postList',
            url: '/post/user/list',
            page: true,
            cols: [[
                {field: 'id', hide: true},
                {field: 'ename', width: 80, hide: true},
                {field: 'shamId', width: 80, hide: true},
                {field: 'draftId', width: 80, hide: true},
                {field: 'title', width: 280, title: "标题[原因]", sort: true,templet:'#titleTpl'},
                {field: 'views', width: 90, title: "阅读数", sort: true},
                {field: 'pros', width: 90, title: "点赞数", sort: true},
                {
                    field: 'lastModifiedTime',
                    width: 170,
                    title: '最后更新时间',
                    templet: "<div>{{layui.util.toDateString(d.lastModifiedTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"
                },
                {field: 'status', width: 80, title: "审核", templet: '#statusTpl'},
                {field: 'enable', width: 100, title: "状态", templet: '#switchTpl'},
                {field: '', title: "操作", templet: '#operation'}

            ]]
        });

        table.on('tool(postList)', function (data) {

            var item = data.data;
            var ename = item.ename;
            var shamId = item.shamId;

            switch (data.event) {
                case 'edit':
                    layer.open({
                        title: '编辑文章',
                        type: 2,
                        shadeClose: true,
                        maxmin: true,
                        area: ['900px', '500px'],
                        content: '/admin/edit/article/' + ename + '/' + shamId
                    });
                    return;
                case 'draft':
                    layer.open({
                        title: '编辑草稿',
                        type: 2,
                        shadeClose: true,
                        maxmin: true,
                        area: ['900px', '500px'],
                        content: '/admin/edit/draft/' + ename + '/' + shamId
                    });
                    return;
                case 'delete':

                    if (item.enable) {
                        layer.msg("禁用后才可删除该文章", function () {

                        });
                        return;
                    }
                    layer.confirm('您确定要删除 ' + item.title + " 吗?", {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            cache: false,
                            type: "POST",
                            contentType: 'application/json',
                            url: "/post/delete/" + ename + "/" + shamId,
                            async: false,
                            success: function (data) {
                                if (data.code === 0) {
                                    layer.msg(data.message);
                                    layui.table.reload('postList');
                                } else {
                                    layer.msg(data.message, function () {
                                    });
                                }
                            },
                            error: function (data) {
                                layer.msg(data.message);
                            }
                        });

                    }, function () {

                    });
                    return;
            }
        });
    });


    layui.use(['form'], function () {
        var form = layui.form;
        form.on('switch(selectEnable)', function (data) {
            //开关是否开启，true或者false
            var item = $(data.elem).parents('tr').first().find('td');
            var ename = item.eq(1).text();
            var shamId = item.eq(2).text();
            var checked = data.elem.checked;
            $.ajax({
                cache: false,
                type: "POST",
                contentType: 'application/json',
                url: "/post/enable/" + ename + "/" + shamId,
                dataType: "json",
                data: JSON.stringify({
                    "enable": checked
                }),
                async: false,
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg((checked ? "启用" : "停用") + "成功");
                        layui.table.reload('postList');

                    } else {
                        layer.msg(data.message);
                    }
                },
                error: function (data) {
                    layer.msg(data.message);
                }
            });
        });
    });
</script>

<script type="text/javascript">

    function searchTitle() {
        debugger;
        var title = $("#title").val();
        layui.table.reload('postList', {
            page: {
                curr: 1
            }
            , where: {
                title: title
            }
        });
    }

</script>
</html>