<!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-card">
            <div class="layui-card-header">
                <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
                    <a href="javascript:void(0);"><cite>环境变量(参数名唯一且只能是字母、数字、下划线与英文横线的组合。可以通过 %变量名% 的方式相互引用。)</cite></a>
                </span>
                <button id="envPlusBtn" 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="250">
                            <col>
                            <col width="250">
                            <col width="60">
                        </colgroup>
                        <thead>
                        <tr>
                            <th>变量名</th>
                            <th>变量值</th>
                            <th>描述</th>
                            <th style="text-align: center;">操作</th>
                        </tr>
                        </thead>
                        <tbody id="envBody"></tbody>
                    </table>
                </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 envs;

        // 系统参数配置
        function EnvsInit(idPrefix, classPrefix, envList) {
            var envsArray = [];
            var keyupSetTimeout;

            function getTr(index, key, value, explain) {
                return  '<tr>' +
                        '   <td>' +
                        '       <input type="text" id="'+ idPrefix +'Key'+ index +'" placeholder="输入参数名" class="layui-input '+ classPrefix +'-key" value="'+ (key ? key : '') +'" data-index="'+ index +'">' +
                        '   </td>' +
                        '   <td>' +
                        '       <input type="text" id="'+ idPrefix +'Value'+ index +'" placeholder="输入参数值" class="layui-input '+ classPrefix +'-value" value="'+ (value ? value : '') +'" data-index="'+ index +'">' +
                        '   </td>' +
                        '   <td>' +
                        '       <input type="text" id="'+ idPrefix +'Explain'+ index +'" placeholder="输入描述" class="layui-input '+ classPrefix +'-explain" value="'+ (explain ? explain : '') +'" 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 < envsArray.length; i++) {
                    var item = envsArray[i];
                    trs += getTr(i, item.envKey, item.envValue, item.envExplain);
                }
                $('#'+ idPrefix +'Body').empty();
                $('#'+ idPrefix +'Body').append(trs);
            }

            function isKeyExist(index, key) {
                if(!key) {
                    return false;
                }
                for(var i = 0, item; item = envsArray[i++];) {
                    if(key == item.envKey && index != (i - 1)) {
                        return true;
                    }
                }
                return false;
            }

            function isKeyEffective(key) {
                if((/^[a-zA-Z0-9\_\-]+$/g.test(key))) {
                    return true;
                }
                return false;
            }

            $(document).on('keyup', '.'+ classPrefix +'-key', function() {
                var self = this;
                var index = this.dataset.index;
                this.value = this.value.replace(/\s/g, '');
                var value = this.value;
                if(keyupSetTimeout) {
                    clearTimeout(keyupSetTimeout);
                }
                keyupSetTimeout = setTimeout(function() {
                    if(!isKeyEffective(value)) {
                        top.dialog.msg('参数名只能是字母、数字、下划线与英文横线组合');
                        self.focus();
                        self.value = '';
                        return;
                    }
                    if(isKeyExist(index, value)) {
                        top.dialog.msg('参数名重复');
                        self.focus();
                        self.value = '';
                        return;
                    } else {
                        envsArray[index].envKey = value;
                    }
                }, 500);
            });

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

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

            $(document).on('click', '#'+ idPrefix +'PlusBtn', function() {
                envsArray.push({
                    envKey: '',
                    envValue: '',
                    envExplain: '',
                });
                refreshTr();
            })

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

            this.init = function() {
                if(!envList) {
                    return;
                }
                for(var i = 0, item; item = envList[i++];) {
                    envsArray.push({
                        envKey: item.envKey,
                        envValue: item.envValue,
                        envExplain: item.envExplain
                    })
                }
                refreshTr();
            }
            this.listEnvs = function() {
                return envsArray;
            }
        };

        // 初始化
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/env/list', []), {}, null, function(code, data) {
                envs = new EnvsInit('env', 'env', data);
                envs.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;
                top.restAjax.put(top.restAjax.path('api/env/update', []), {
                    envs: envs.listEnvs()
                }, 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;
        });

        form.verify({
            positiveNumber: function(value, item) {
                if(value < 0) {
                    return '必须是正数';
                }
            }
        });
    });
</script>
</body>
</html>