<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../common.jsp" />
    <style>
    </style>
</head>
<body>
<div class="wrapper wrapper-content mt5 right-content ml5 mr5">
    <div class="toolbar-panel bdd">
        <!--搜索条件 -->
        <div class="toolbar-box">
            <div class="toolbar-head clearfix lgraybg pt5 pb10 pl10 bdbd">
                <!-- 顶部按钮 -->
                <div class="fl">
                    查询条件
                </div>
                <div class="tools fr">
                    <a href="javascript:void(0);" class="collapse dblock f14 dgray mr5">
                        <i class="fa-2x fa fa-angle-double-up"></i>
                    </a>
                </div>
            </div>
        </div>
        <!--搜索条件 -->
        <div class="toolbar-body">
            <form class="layui-form" action="" style="margin-top: 10px" lay-filter="formCondition">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" placeholder="请输入用户ID" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">编码</label>
                        <div class="layui-input-inline">
                            <input type="text" id="code" value="1" name="code" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select id="status" name="status">
                                <option value="">请选择</option>
                                <option value="0">无效</option>
                                <option value="1" selected>有效</option>
                            </select>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<table class="layui-hide" id="table" lay-filter="table"></table>
</body>


<script>
    let layer;
    let table;
    let form;

    layui.use(['layer', 'table', 'form'], function () {
        layer = layui.layer; //弹层
        table = layui.table; //表格
        form = layui.form;

        //执行一个 table 实例
        table.render({
            elem: '#table'
            , id: 'table'
            , url: ctx + '/encryptKey/page' //数据接口
            , autoSort: false
            , where: form.val('formCondition')
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //默数据列表的字段名称，认：data
                , statusCode: 200
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , title: 'app首页菜单表'
            , page: true //开启分页
            , toolbar: '#toolBar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: ['filter']
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: '', title: '序号', type: 'numbers'}
                , {field: 'name', title: '名称', minWidth: 80}
                , {field: 'code', title: '编码', minWidth: 80}
                , {field: 'desc', title: '描述', minWidth: 150}
                , {field: 'priKey', title: '私钥', minWidth: 100}
                , {field: 'pubKey', title: '公钥', minWidth: 100}
                , {field: 'version', title: '版本', minWidth: 100}
                , {field: 'status', title: '状态', template: function(d) {
                        if (d.status == "1") {
                            return "<span class='color: #FF5722'>生效中</span>";
                        } else {
                            return "";
                        }
                    }}
                , {
                    field: 'createTime',
                    title: '创建时间',
                    width: 160,
                    hide: false,
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {
                    field: 'updateTime',
                    title: '更新时间',
                    width: 165,
                    hide: false,
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作', minWidth: 80, align: 'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'search':
                    search();
                    break;
                case 'add':
                    toAdd(data.id)
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(table)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            let itemData = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'enable') {
                let index = layer.confirm('确认生效', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    let index = layer.load(1, {shade: [0.1, '#000']});
                    $.ajax({
                        url: ctx + "/encryptKey/enable"
                        , type: 'POST'
                        , data: {
                            id: itemData.id
                        }
                        , success: function(d) {
                            if (d.code == 200) {
                                layer.closeAll()
                                table.reload('table');
                            } else {
                                layer.close(index);
                            }
                            layer.msg(d.message);
                        }
                    })
                });
            }
        });
    });

    function search() {
        table.reload("table", {
            where: form.val('formCondition')
        }, true)
    }

    function toAdd(id) {
        layer.open({
            type: 2
            , title: "添加新的秘钥信息"
            , shade: 0.5
            , maxmin: false
            , content: ctx + '/encryptKey/detail'
            , area: ['550px', '600px']
        });
    }
</script>

<script id ="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="enable">
        <i class="layui-icon layui-icon-release"></i>启用
    </a>
</script>

<script id ="toolBar" type="text/html">
    <a class="layui-btn layui-btn-sm" lay-event="search">
        <i class="layui-icon  layui-icon-search"></i>启用
    </a>
    <a class="layui-btn layui-btn-sm" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>添加
    </a>
</script>
</html>