<%
local dsp = require "luci.dispatcher"
-%>
<script type="text/javascript">
	//<![CDATA[
	function clearlog(btn) {
		XHR.get('<%=dsp.build_url("admin/services/shadowsocksr/clear_log")%>', null,
			function(x, data) {
				if (x && x.status == 200) {
					var log_textarea = document.getElementById('log_textarea');
					log_textarea.innerHTML = "";
					log_textarea.scrollTop = log_textarea.scrollHeight;
				}
			}
		);
	}

	XHR.poll(5, '<%=dsp.build_url("admin/services/shadowsocksr/get_log")%>', null,
		function(x, data) {
			if (x && x.status == 200) {
				var log_textarea = document.getElementById('log_textarea');
				// 将日志分行处理，移除最后一行空行但保留中间空行
				var logs = x.responseText.split("\n");
				if (logs[logs.length - 1].trim() === "") {
					logs.pop(); // 删除最后的空行
				}
				logs = logs.reverse().join("\n"); // 倒序排列
				log_textarea.innerHTML = logs;
			}
		}
	);
	//]]>
</script>
<fieldset class="cbi-section" id="_log_fieldset">
	<input class="btn cbi-button cbi-button-remove" type="button" onclick="clearlog()" value="<%:Clear logs%>" />
	<textarea id="log_textarea" class="cbi-input-textarea" style="width: 100%;margin-top: 10px;" data-update="change" rows="20" wrap="off" readonly="readonly"></textarea>
</fieldset>
