@using MineTec.ProManager.Authorization
@using MineTec.ProManager.Admin.ViewResources
@{
    ViewBag.CurrentPageName = PageNames.Maintenance;
}

@section styles{
    <style type="text/css">
        #webSiteLogsContent {
            border: 1px solid #ccc;
            overflow: auto;
            padding: 5px;
            margin-top: 5px;
            height: -webkit-calc(100vh - 150px);
            height: -moz-calc(100vh - 150px);
            height: calc(100vh - 150px);
        }

            #webSiteLogsContent::-webkit-scrollbar {
                width: 0px;
                height: 0px;
            }

            #webSiteLogsContent::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }

        .layui-table-body::-webkit-scrollbar-track {
            background: white;
            border-radius: 2px;
        }

        #webSiteLogsContent::-webkit-scrollbar-thumb {
            background: #E6E6E6;
            border-radius: 2px;
        }

            #webSiteLogsContent::-webkit-scrollbar-thumb:hover {
                background: #E6E6E6;
            }

        #webSiteLogsContent::-webkit-scrollbar-corner {
            background: #f6f6f6;
        }
    </style>
}

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">系统日志</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Maintenance_Logs_DownLoad))
                    {
                        <button class="pear-btn pear-btn-primary pear-btn-md" data-type="downloadLog">
                            <i class="layui-icon layui-icon-download-circle"></i> 日志下载
                        </button>
                    }
                    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Maintenance_Logs_Refresh))
                    {
                        <button class="pear-btn pear-btn-danger pear-btn-md" data-type="refresh">
                            <i class="layui-icon layui-icon-refresh-3"></i> 刷新
                        </button>
                    }
                    <div id="webSiteLogsContent"></div>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script type="text/javascript">
        layui.use(['abp', 'layer'], function () {
            var $ = layui.jquery;
            var abp = layui.abp;
            var layer = layui.layer;

            //加载日志信息
            function loadLogs() {
                var waitIndex = parent.layer.load(2);
                abp.ajax({
                    type:"Get",
                    url: "@Url.Action("GetLatestWebLogs", "Maintenance")",
                    abpHandleError: false
                }).done(function (data) {
                    $("#webSiteLogsContent").html(getFormattedLogs(data.latestWebLogLines));
                }).fail(function (jqXHR) {
                    parent.layer.msg(jqXHR.message, { icon: 5 });
                }).always(function () {
                    parent.layer.close(waitIndex);
                });
            }
            loadLogs();

            //信息加工
            function getFormattedLogs(logLines) {
                var resultHtml = '';
                $.each(logLines, function (index, logLine) {
                    resultHtml += '<span>' + logLine
                        .replace('DEBUG', '<span class="layui-badge layui-bg-gray">DEBUG</span>')
                        .replace('INFO', '<span class="layui-badge layui-bg-green">INFO</span>')
                        .replace('WARN', '<span class="layui-badge layui-bg-orange">WARN</span>')
                        .replace('ERROR', '<span class="layui-badge">ERROR</span>')
                        .replace('FATAL', '<span class="layui-badge">FATAL</span>') + '</span><br/>';
                });
                return resultHtml;
            }

            //监听页面主按钮操作
            var active = {
                downloadLog: function () {
                    var waitIndex = parent.layer.load(2);
                    abp.ajax({
                        type:"Get",
                        url: "@Url.Action("DownloadWebLogs", "Maintenance")",
                        abpHandleError: false
                    }).done(function (file) {
                        location.href = '@Url.Action("DownloadTempFile", "File")' + abp.utils.formatString("?fileToken={0}&fileType={1}&fileName={2}", file.fileToken, file.fileType, file.fileName);
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                },
                refresh: function () {
                    loadLogs();
                }
            };

            //点击事件转发器
            $('.pear-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
}