<%+cbi/valueheader%>
<%
	local stream_id_name = string.gsub(self.value, "%W", "")
%>
<script type="text/javascript">//<![CDATA[

function act_manual_unlock_test_<%=stream_id_name%>(btn, type)
{
	var legend = document.getElementById('manual-<%=stream_id_name%>-test-state');
	var output = document.getElementById('manual-<%=stream_id_name%>-test-output');
	var r = confirm("<%:Network instability may occur during testing, Are you sure want to start test?%>")
	if (r == true) {
		if (legend && output)
		{
			output.innerHTML =
			'<span class="loading-spinner" style="vertical-align:middle"></span> ' +
				'<%:Waiting for command to complete...%>';

			legend.textContent = '<%:Collecting data...%>';
			legend.parentNode.classList.remove('oc-hidden');
			legend.style.display = 'block';
		};

		var xhr = new XMLHttpRequest();
		xhr.timeout = 0;
		var url = '<%=luci.dispatcher.build_url("admin", "services", "openclash", "manual_stream_unlock_test")%>' + '?type=' + encodeURIComponent(type);
		xhr.open('GET', url, true);
		xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
		var editorName = 'manual_stream_unlock_test';
		var editor = null, editorReady = false;
		xhr.onprogress = function() {
			if (!editorReady) {
				output.innerHTML = '';
				var ta = document.createElement('textarea');
				output.appendChild(ta);
				editor = log_editor(ta, editorName, 'true', '600px', '250px');
				editorReady = true;
			}
			if (editor && editor.setValue) {
				editor.setValue(xhr.responseText.split('\n').map(function(l) { return l.trim(); }).filter(function(l) { return l !== ''; }).join('\n'));
				editor.dispatch({ selection: { anchor: editor.state.doc.length } });
				if (editor.scrollDOM) editor.scrollDOM.scrollTop = editor.scrollDOM.scrollHeight;
			}
		};
		xhr.onload = function() {
			if (editor && editor.setValue) {
				editor.setValue(xhr.responseText.split('\n').map(function(l) { return l.trim(); }).filter(function(l) { return l !== ''; }).join('\n'));
				editor.dispatch({ selection: { anchor: editor.state.doc.length } });
				if (editor.scrollDOM) editor.scrollDOM.scrollTop = editor.scrollDOM.scrollHeight;
			}
			legend.textContent = '<%:Unlock Test Result%>';
			if (xhr.status != 200 && xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Something Wrong While Testing...%></span>';
			}
		};
		xhr.ontimeout = function() {
			if (xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Request Timeout...%></span>';
			}
		};
		xhr.onerror = function() {
			if (xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Something Wrong While Testing...%></span>';
			}
		};
		xhr.send();
	};
};

function act_all_proxies_test_<%=stream_id_name%>(btn, type)
{
	var legend = document.getElementById('all-<%=stream_id_name%>-test-state');
	var output = document.getElementById('all-<%=stream_id_name%>-test-output');
	var r = confirm("<%:Network instability may occur during testing, Are you sure want to start test?%>")
	if (r == true) {
		if (legend && output)
		{
			output.innerHTML =
			'<span class="loading-spinner" style="vertical-align:middle"></span> ' +
				'<%:Waiting for command to complete...%>';

			legend.textContent = '<%:Collecting data...%>';
			legend.parentNode.classList.remove('oc-hidden');
			legend.style.display = 'block';
		};

		var xhr = new XMLHttpRequest();
		xhr.timeout = 0;
		var url = '<%=luci.dispatcher.build_url("admin", "services", "openclash", "all_proxies_stream_test")%>' + '?type=' + encodeURIComponent(type);
		xhr.open('GET', url, true);
		xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
		var editorName = 'all_proxies_stream_test';
		var editor = null, editorReady = false;
		xhr.onprogress = function() {
			if (!editorReady) {
				output.innerHTML = '';
				var ta = document.createElement('textarea');
				output.appendChild(ta);
				editor = log_editor(ta, editorName, true, '600px', '250px');
				editorReady = true;
			}
			if (editor && editor.setValue) {
				editor.setValue(xhr.responseText.split('\n').map(function(l) { return l.trim(); }).filter(function(l) { return l !== ''; }).join('\n'));
				editor.dispatch({ selection: { anchor: editor.state.doc.length } });
				if (editor.scrollDOM) editor.scrollDOM.scrollTop = editor.scrollDOM.scrollHeight;
			}
		};
		xhr.onload = function() {
			if (editor && editor.setValue) {
				editor.setValue(xhr.responseText.split('\n').map(function(l) { return l.trim(); }).filter(function(l) { return l !== ''; }).join('\n'));
				editor.dispatch({ selection: { anchor: editor.state.doc.length } });
				if (editor.scrollDOM) editor.scrollDOM.scrollTop = editor.scrollDOM.scrollHeight;
			}
			legend.textContent = '<%:All Proxies Test Result%>';
			if (xhr.status != 200 && xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Something Wrong While Testing...%></span>';
			}
		};
		xhr.ontimeout = function() {
			if (xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Request Timeout...%></span>';
			}
		};
		xhr.onerror = function() {
			if (xhr.responseText == "") {
				output.innerHTML = '<span class="error"><%:Something Wrong While Testing...%></span>';
			}
		};
		xhr.send();
	};
};
	
//]]></script>

<input type="button" class="btn cbi-button cbi-input-reload" value="<%:Unlock Test(Select)%>" onclick="return act_manual_unlock_test_<%=stream_id_name%>(this, '<%=self.value%>')" />
<input type="button" class="btn cbi-button cbi-input-reload" value="<%:All Proxies Test%>" onclick="return act_all_proxies_test_<%=stream_id_name%>(this, '<%=self.value%>')" />
<div class="oc">
	<div class="diag-style oc-hidden">
		<div id="manual-<%=stream_id_name%>-test-state" class="diag-legend"><%:Collecting data...%></div>
		<div id="manual-<%=stream_id_name%>-test-output"></div>
	</div>
	<div class="diag-style oc-hidden">
		<div id="all-<%=stream_id_name%>-test-state" class="diag-legend"><%:Collecting data...%></div>
		<div id="all-<%=stream_id_name%>-test-output"></div>
	</div>
</div>
<%+cbi/valuefooter%>
