<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header(#{system.cache.title})" />
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <input type="hidden" id="cacheName">
    <div class="col-sm-12">
        <div class="row">
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5><i class="fa fa-bars"></i> <span th:text="#{system.cache.list.title}"></span></h5>
                        <div class="ibox-tools">
                            <a href="javascript:getCacheNames()"><i class="fa fa-refresh"></i></a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-hover no-margins">
                            <thead>
                            <tr>
                                <th></th>
                                <th th:text="#{system.cache.table.cacheName}"></th>
                                <th th:text="#{system.cache.table.operation}"></th>
                            </tr>
                            </thead>
                            <tbody id="cacheNames">
                            <tr th:fragment="fragment-cache-names" th:each="cacheName, stat : ${cacheNames}">
                                <td>[[${stat.index + 1}]]</td>
                                <td style="word-wrap:break-word;word-break:break-all;" th:onclick="getCacheKeys([[${cacheName}]])">[[${cacheName}]]</td>
                                <td style="width: 50px"><a href="javascript:;" th:onclick="clearCacheName([[${cacheName}]])" th:title="#{system.cache.btn.clear}"><i class="fa fa-trash-o text-danger"></i></a></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5><i class="fa fa-key"></i> <span th:text="#{system.cache.keys.title}"></span></h5>
                        <div class="ibox-tools">
                            <a href="javascript:getCacheKeys('', true)"><i class="fa fa-refresh"></i></a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-hover no-margins">
                            <thead>
                            <tr>
                                <th></th>
                                <th th:text="#{system.cache.table.cacheKey}"></th>
                                <th th:text="#{system.cache.table.operation}"></th>
                            </tr>
                            </thead>
                            <tbody id="cacheKeys">
                            <tr th:fragment="fragment-cache-kyes" th:each="cacheKey, stat : ${cacheKeys}">
                                <td>[[${stat.index + 1}]]</td>
                                <td style="word-wrap:break-word;word-break:break-all;" th:onclick="getCacheValue([[${cacheName}]], [[${cacheKey}]])">[[${cacheKey}]]</td>
                                <td style="width: 50px"><a href="javascript:;" th:onclick="clearCacheKey([[${cacheName}]], [[${cacheKey}]])" th:title="#{system.cache.btn.clear}"><i class="fa fa-trash-o text-danger"></i></a></td>
                            </tr>
                            </tbody>
                    </div>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5><i class="fa fa-file-text"></i> <span th:text="#{system.cache.value.title}"></span></h5>
                    <div class="ibox-tools">
                        <a href="javascript:clearAll()"><i class="fa fa-refresh"></i> <span th:text="#{system.cache.btn.clearAll}"></span></a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row" id="cacheValue">
                        <div class="col-sm-12" th:fragment="fragment-cache-value">
                            <div class="form-group">
                                <label th:text="#{system.cache.label.cacheName}"></label>
                                <input type="text" class="form-control" th:value="${cacheName}">
                            </div>
                            <div class="form-group">
                                <label th:text="#{system.cache.label.cacheKey}"></label>
                                <input type="text"class="form-control" th:value="${cacheKey}">
                            </div>
                            <div class="form-group">
                                <label th:text="#{system.cache.label.cacheValue}"></label>
                                <textarea class="form-control" style="height: 100px">[[${cacheValue}]]</textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "monitor/cache";

    function getCacheNames() {
        $.ajax({
            type: "post",
            url: prefix + "/getNames",
            data: {
                "fragment": 'fragment-cache-names'
            },
            success: function(data) {
                $("#cacheNames").html(data);
                $.modal.msgSuccess(i18n("system.cache.msg.refreshListSuccess"));
            }
        });
    }

    function getCacheKeys(cacheName, isMsg) {
        var _cacheName = $.common.isNotEmpty(cacheName) ? cacheName : $("#cacheName").val();
        $.ajax({
            type: "post",
            url: prefix + "/getKeys",
            data: {
                "cacheName": _cacheName,
                "fragment": 'fragment-cache-kyes'
            },
            success: function(data) {
                $("#cacheKeys").html(data);
                $("#cacheName").val(_cacheName);
                if (isMsg) {
                    $.modal.msgSuccess(i18n("system.cache.msg.refreshKeysSuccess"));
                }
            }
        });
    }

    function getCacheValue(cacheName, cacheKey) {
        $.ajax({
            type: "post",
            url: prefix + "/getValue",
            data: {
                "cacheName": cacheName,
                "cacheKey": cacheKey,
                "fragment": 'fragment-cache-value'
            },
            success: function(data) {
                $("#cacheValue").html(data);
            }
        });
    }

    function clearCacheName(cacheName){
        $.post(prefix + "/clearCacheName", {cacheName: cacheName}, function(result) {
            if (result.code == web_status.SUCCESS) {
                $.modal.msgSuccess("清理缓存[" + cacheName + "]成功")
                getCacheKeys(cacheName);
            } else {
                $.modal.msgError(result.msg);
            }
        });
    }

    function clearCacheKey(cacheName, cacheKey) {
        $.post(prefix + "/clearCacheKey", {cacheName: cacheName, cacheKey: cacheKey}, function(result) {
            if (result.code == web_status.SUCCESS) {
                $.modal.msgSuccess("清理缓存[" + cacheKey + "]成功")
                getCacheKeys(cacheName);
            } else {
                $.modal.msgError(result.msg);
            }
        });
    }

    function clearAll(){
        $.get(prefix + "/clearAll", function(result) {
            if (result.code == web_status.SUCCESS) {
                $.modal.msgSuccess(i18n("system.cache.msg.clearAllSuccess"));
            } else {
                $.modal.msgError(result.msg);
            }
        });
    }
</script>
</html>
