<%
    local fs = require "luci.openclash"
    local plugin_version = fs.oc_version()
%>
<head>
<script src="/luci-static/resources/openclash/js/common.js?v=<%=plugin_version%>"></script>
<link rel="stylesheet" href="/luci-static/resources/openclash/css/oc.css?v=<%=plugin_version%>">
</head>

<li id="tool_label" class="tool_label">
	<span class="tool_label_span">
	&nbsp;&nbsp;<%:Current Config%>:&nbsp;
	<select class="tool_label_select" id="cfg_name">
	</select>&nbsp;&nbsp;
	<input type="button" class="btn cbi-button cbi-button-apply" value="<%:SwiTch%>" onclick="return switch_config(this)" />
	&nbsp;
	</span>
</li>

<script type="text/javascript">//<![CDATA[

		var config_name = document.getElementById('cfg_name');
		var tool_label = document.getElementById('tool_label');

		setTimeout("get_header()",100);

		if (tool_label.style.display != "none") {
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "config_name")%>', null, function(x, status) {
				if (x && x.status == 200 && status.config_name != "") {
					for(var i in status.config_name){
     	 	  			config_name.options.add(new Option(status.config_name[i].name,status.config_name[i].name));
    				}
    				if (status.config_path != "") {
    					config_name.value = status.config_path;
    				}
    			else
    			{
    				config_name.options.add(new Option("<%:Not Select%>",""));
    				config_name.value = "";
    			}
				}
				else if (x && x.status == 200 && status.config_path != "") {
					config_name.options.add(new Option(status.config_path,status.config_path));
					config_name.value = status.config_path;
				}
				else {
					tool_label.style.display = "none";
				}
			});
		};
	
	function get_header() {
		var header = document.getElementsByClassName("tabmenu-item-log ")[0];
		if (header) {
			insertAfter(tool_label,header);
		}
		else {
			setTimeout("get_header()",100);
		}
	}
		
	function insertAfter(newElement, targetElement) {
		var parent = targetElement.parentNode;
		if (parent.lastChild == targetElement) {
			parent.appendChild(newElement, targetElement); 
		}
		else {
			parent.insertBefore(newElement, targetElement.nextSibling);
		};
	};
	
	function switch_config(btn)
	{
		if (config_name.value && config_name.value != "") {
			btn.disabled = true;
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "switch_config")%>', {config_name: config_name.value}, function(x, status) {
				if (x && x.status == 200) {
					btn.disabled = false;
					alert(' <%:Config File%>: ' + config_name.value + ' <%:switching succeeded!%>')
					window.location.href=location.pathname;
				}
				else {
					alert(' <%:Config File%>: ' + config_name.value + ' <%:switching failed!%>')
				}
			});
			return false;
		}
	}

	function restore_config(btn)
    {
        btn.value    = '<%:Restore Default%>';
        btn.disabled = true;
        var r = confirm("<%:Are you sure want to restore the default config?%>")
        if (r == true) {
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "restore")%>', null, function(x, status) {
        	if ( x && x.status == 200 ) {
        		alert('<%:Restore succeeded!%>')
        		window.location.href='<%="settings"%>';
        	}
        	else {
				alert('<%:Restore failed!%>')
				window.location.href='<%="settings"%>';
           }
        });
        }
        btn.disabled = false;
        return false; 
    }
	
//]]></script>