<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>日志列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</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">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
<!--                        <div class="layui-inline layui-show-xs-block">-->
<!--                            <input type="text" class="layui-input" autocomplete="off" placeholder="请输入所属运营商名称"-->
<!--                                   name="operatorName" id="start">-->
<!--                        </div>-->
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input"  autocomplete="off" placeholder="开始日" name="startTime" id="startTime">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input"  autocomplete="off" placeholder="截止日" name="endTime" id="endTime">
                        </div>
<!--                        <div class="layui-inline layui-show-xs-block">-->
<!--                            <label for="startTime" class="layui-form-label">-->
<!--                                开始时间：-->
<!--                            </label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="datetime-local" class="layui-input" autocomplete="off" placeholder="开始时间"-->
<!--                                       name="startTime" id="startTime">-->
<!--                            </div>-->
<!--                        </div>-->
<!--                        <div class="layui-inline layui-show-xs-block">-->
<!--                            <label for="endTime" class="layui-form-label">-->
<!--                                结束时间：-->
<!--                            </label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="datetime-local" name="endTime" placeholder="结束时间" autocomplete="off"-->
<!--                                       class="layui-input" id="endTime">-->
<!--                            </div>-->
<!--                        </div>-->
                        <div class="layui-inline layui-show-xs-block">
                            <a class="layui-btn" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i>
                            </a>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除
                    </button>
<!--                    <button class="layui-btn" onclick="xadmin.open('添加项目','./project-add',600,550)"><i-->
<!--                            class="layui-icon"></i>添加-->
<!--                    </button>-->
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form" id="table" lay-filter="table">
                        <thead>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate', 'form'], function () {
        var laydate = layui.laydate;
        var form = layui.form;


        // 监听全选
        form.on('checkbox(checkall)', function (data) {

            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });

        laydate.render({
            elem: "#startTime",
            type: "datetime"
        });

        laydate.render({
            elem: "#endTime",
            type: "datetime"
        });

    });


    function delAll(argument) {
        var ids = [];

        // 获取选中的id 
        $('tbody input').each(function (index, el) {
            if ($(this).prop('checked')) {
                // console.log($(this).val())
                ids.push($(this).val())
            }
        });

        layer.confirm('确认要删除吗？' + ids.toString(), function (index) {
            console.log(JSON.stringify(ids))
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $.ajax({
                url: "[[${#request.contextPath}]]" + "/alarmRecords/batch",
                method: "delete",
                data: JSON.stringify(ids),
                contentType: "application/json;charset=UTF-8" //定义数据格式为JSON
            });
            $(".layui-form-checked").not('.header').parents('tr').remove();

            //表单重载
            tableReload.reload('tableId', {
                where: {},
                page: {
                    curr: 1
                }
            });
        });
    }

    //用于表单重载
    var tableReload;

    layui.use('table', function () {
        var table = layui.table;
        var form = layui.form;

        //第一个实例
        table.render({
            elem: '#table',
            url: '/alarmRecords/page', //数据接口
            page: true, //开启分页
            id: 'tableId',
            cols: [
                [ //表头
                    {type: 'checkbox', title: 'ID'},
                    {field: 'id', title: 'id', width: 250, sort: true},
                    {field: 'time', title: '时间', width: 160},
                    {field: 'alarmContent', title: '报警内容', width: 450, sort: true},
                    {field: 'status', title: '状态', width: 250},
                    {field: 'lastingTime', title: '持续时间（分钟）', width: 250},
                    {field: 'tool', toolbar: '#barDemo', title: '操作', width: 180}
                ]
            ],
            done: function () {
                $("tbody td[data-field='id'] div").each(function () {
                    var id = $(this).text();
                    // console.log(id)
                    $(this).parent().prev().children().find("input").attr("value", id);
                })
            }
        });


        //工具条事件
        table.on('tool(table)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    console.log(data)
                    $.ajax({
                        url: "[[${#request.contextPath}]]" + '/alarmRecords/delete',
                        data: JSON.stringify(data),
                        method: "post",
                        contentType: "application/json;charset=UTF-8" //定义数据格式为JSON
                    });
                    //表单重载
                    tableReload.reload('tableId', {
                        where: {},
                        page: {
                            curr: 1
                        }
                    });
                });
            } else if (layEvent === 'handle') { //编辑
                layer.confirm('真的修改为已处理状态吗', function (index) {
                    layer.close(index);
                    console.log(data)
                    $.ajax({
                        url: "[[${#request.contextPath}]]" + '/alarmRecords/status',
                        data: JSON.stringify(data),
                        method: "post",
                        contentType: "application/json;charset=UTF-8" //定义数据格式为JSON
                    });
                    //表单重载
                    tableReload.reload('tableId', {
                        where: {},
                        page: {
                            curr: 1
                        }
                    });
                });
            } else if (layEvent === 'LAYTABLE_TIPS') {
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });


        form.on('submit(search)',
            function (data) {

                console.log(data.field)

                var dataUrl;

                if (data.field.operatorName == '' && data.field.startTime == '' && data.field.endTime == '') {
                    dataUrl = '/alarmRecords/page';
                } else {
                    dataUrl = '/alarmRecords/search';
                }

                table.render({
                    elem: '#table',
                    url: dataUrl, //数据接口
                    page: true, //开启分页
                    id: 'tableId',
                    where: data.field,
                    cols: [
                        [ //表头
                            {type: 'checkbox', title: 'ID'},
                            {field: 'id', title: 'id', width: 250, sort: true},
                            {field: 'time', title: '时间', width: 160},
                            {field: 'alarmContent', title: '报警内容', width: 450, sort: true},
                            {field: 'status', title: '状态', width: 250},
                            {field: 'lastingTime', title: '持续时间（分钟）', width: 250},
                            {field: 'tool', toolbar: '#barDemo', title: '操作', width: 180}
                        ]
                    ],
                    done: function () {
                        $("tbody td[data-field='id'] div").each(function () {
                            var id = $(this).text();
                            // console.log(id)
                            $(this).parent().prev().children().find("input").attr("value", id);
                        })
                    }
                });
            });

        tableReload = table;

    });


</script>
<!-- 工具条 -->
<script type="text/html" id="barDemo">
<!--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="handle">标记为已处理</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</html>