<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
    <form class="layui-form" lay-filter="dataForm">
        <div class="layui-row">
            <div class="layui-col-sm8 layui-col-xs12">
                <div class="layui-card">
                    <div class="layui-card-header">
                <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
                    <a href="javascript:void(0);"><cite>白名单（符合AndMatcher规则）</cite></a>
                </span>
                        <button id="whiteListPlusBtn" type="button" class="layui-btn layui-btn-xs" style="float: right; margin-top: 12px;">
                            <i class="fa fa-plus" aria-hidden="true"></i>
                        </button>
                    </div>
                    <div id="cardBody" class="layui-card-body" style="padding: 15px; overflow: auto">
                        <div class="layui-form-item">
                            <table class="layui-table">
                                <colgroup>
                                    <col width="60">
                                    <col>
                                    <col width="60">
                                </colgroup>
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>开放路径</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="whiteListBody"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交更新</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'laytpl'], function(){
        var $ = layui.$;
        var form = layui.form;
        var $win = $(window);
        var windowHeight = $(window).height();

        $('#cardBody').css({
            height: (windowHeight - 170) +'px'
        })

        var whiteList;

        // 系统参数配置
        function WhiteListInit(idPrefix, classPrefix, pathMatchers) {
            var pathMatcherArray = [];

            function getTr(index, pathMatcher) {
                return  '<tr>' +
                    '   <td style="text-align: center;">'+ (index + 1) +'</td>'+
                    '   <td>' +
                    '       <input type="text" id="'+ idPrefix +'Key'+ index +'" placeholder="输入放行路径" class="layui-input '+ classPrefix +'-key" value="'+ (pathMatcher ? pathMatcher : '') +'" data-index="'+ index +'">' +
                    '   </td>' +
                    '   <td style="text-align: center;">' +
                    '       <button type="button" class="layui-btn layui-btn-xs layui-btn-danger '+ classPrefix +'-remove-btn" data-index="'+ index +'">' +
                    '           <i class="fa fa-times" aria-hidden="true"></i>' +
                    '       </button>' +
                    '   </td>' +
                    '</tr>';
            }

            function refreshTr() {
                var trs = '';
                for(var i = 0; i < pathMatcherArray.length; i++) {
                    var item = pathMatcherArray[i];
                    trs += getTr(i, item);
                }
                $('#'+ idPrefix +'Body').empty();
                $('#'+ idPrefix +'Body').append(trs);
            }

            $(document).on('keyup', '.'+ classPrefix +'-key', function() {
                var index = this.dataset.index;
                this.value = this.value.replace(/\s/g, '');
                pathMatcherArray[index] = this.value;
            });

            $(document).on('click', '#'+ idPrefix +'PlusBtn', function() {
                pathMatcherArray.push('');
                refreshTr();
            })

            $(document).on('click', '.'+ classPrefix +'-remove-btn', function() {
                var index = this.dataset.index;
                pathMatcherArray.splice(index, 1);
                refreshTr();
            })

            this.init = function() {
                if(!pathMatchers) {
                    return;
                }
                for(var i = 0, item; item = pathMatchers[i++];) {
                    pathMatcherArray.push(item)
                }
                refreshTr();
            }
            this.list = function() {
                return pathMatcherArray;
            }
        };

        // 初始化
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/access/white-list/list', []), {}, null, function(code, data) {
                whiteList = new WhiteListInit('whiteList', 'white-list', data);
                whiteList.init();
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                console.log(whiteList.list())
                top.restAjax.put(top.restAjax.path('api/access/white-list/update', []), {
                    pathMatchers: whiteList.list()
                }, null, function(code, data) {
                    var layerIndex = top.dialog.msg('更新成功', {
                        time: 0,
                        btn: [top.dataMessage.button.yes],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });
    });
</script>
</body>
</html>