<title>单页列表</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统设置</cite></a>
        <a><cite>信息管理</cite></a>
        <a><cite>单页管理</cite></a>
        <a><cite>单页列表</cite></a>
    </div>
</div>


<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn" data-type="add">添加</button>
                <button class="layui-btn layui-btn-normal layuiadmin-btn" data-type="batchreorder">排序</button>
                <button class="layui-btn layui-btn-danger layuiadmin-btn" data-type="batchdel">删除</button>
            </div>
            <script type="text/html" id="statusTpl">
                <input type="checkbox" lay-skin="switch" lay-text="正常|禁用" lay-filter="setStatus" value="{{d.id}}" {{ d.status== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="keyTpl">
                <a href="/novel/page/{{d.key}}" target="_blank">{{d.key}}</a>
            </script>
            <table id="LAY-curd-table" lay-filter="LAY-curd-table"></table>
            <script type="text/html" id="table-curd-list">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<script>
    var api_batch = '/manage/system/page/batch.json'
        , api_list = '/manage/system/page/list.json'
        , api_del = '/manage/system/page/del.json'
        , api_change = '/manage/system/page/change.json'
        , url_add = '/setting/page/add'
        , url_edit = '/setting/page/edit';
    layui.use(['admin', 'table'], function  () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table
            , form = layui.form;

        table.render({
            elem: '#LAY-curd-table'
            , url: api_list
            ,where:{access_token:layui.data('layuiAdmin').access_token}
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', width: 60, title: 'ID', align: 'center'}
                , {field: 'name', title: '单页名称'}
                , {field: 'key', title: '单页Key', templet: '#keyTpl', align: 'center'}
                , {field: 'status', title: '状态', templet: '#statusTpl', align: 'center', width: 100}
                , {field: 'last_update_name', title: '最后修改人'}
                , {field: 'last_update_time', title: '最后更新时间'}
                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-curd-list'}
            ]]
            , height: 'full-320'
        });

        //监听工具条
        table.on('tool(LAY-curd-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条记录么', function (index) {
                    admin.req({
                        url: api_del
                        , data: {id: data.id}
                        , method: 'POST'
                        , success: function (res) {
                            layer.msg('删除成功');
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                location.hash = url_edit + '/id=' + data.id;
            }
        });

        //监听搜索
        form.on('submit(LAY-front-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-curd-table', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-curd-table')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除吗？', function (index) {
                    var items = [];
                    layui.each(checkData, function (k, v) {
                        items.push(v.id)
                    });
                    admin.req({
                        url: api_batch
                        , data: {type: 'del', items: items}
                        , method: 'POST'
                        , success: function (res) {
                            table.reload('LAY-curd-table');
                            layer.msg('已删除');
                        }
                    })
                });
            }
            , batchreorder: function () {
                var checkStatus = table.checkStatus('LAY-curd-table')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var items = [], values = [];
                layui.each(checkData, function (k, v) {
                    items.push(v.id);
                    values.push(v.position)
                });
                admin.req({
                    url: api_batch
                    , data: {type: 'change', items: items, field: 'position', values: values}
                    , method: 'POST'
                    , success: function (res) {
                        table.reload('LAY-curd-table');
                        layer.msg('已更新排序');
                    }
                })
            }
            , add: function () {
                location.hash = url_add
            }
        };

        form.on('switch(setStatus)', function (obj) {
            admin.req({
                url: api_change
                , data: {field: 'status', value: obj.elem.checked ? 1 : 0, id: this.value}
                , method: 'POST'
                , success: function (res) {
                    layer.tips('修改成功', obj.othis);
                }
            })
        });

        $('.layui-btn.layuiadmin-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>