<%+cbi/valueheader%>

<script type="text/javascript">//<![CDATA[
	var catch_num;
	var catch_timeout;
	var catch_out;
	
	function act_download_disney_rule(btn)
	{
		btn.disabled = true;
		btn.value    = '<%:Downloading Rule...%> ';
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash","download_disney_domains")%>',
			null,
			function(x,status)
			{
				if ( x && x.status == 200 ) {
					if(status.rule_download_status=="0")
					{
						btn.value = '<%:Downloading Fail%>';
					}
					else if (status.rule_download_status=="1")
					{
						btn.value = '<%:Downloading Successful%>';
					}
					else if (status.rule_download_status=="2")
					{
						btn.value = '<%:Rule No Change%>';
					}
				}
				else {
					btn.value = '<%:Downloading Timeout%>';
				}
			}
		);
		btn.disabled = false;
		return false;
	};
	
	function act_download_netflix_rule(btn)
	{
		btn.disabled = true;
		btn.value    = '<%:Downloading Rule...%> ';
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash","download_netflix_domains")%>',
			null,
			function(x,status)
			{
				if ( x && x.status == 200 ) {
					if(status.rule_download_status=="0")
					{
						btn.value = '<%:Downloading Fail%>';
					}
					else if (status.rule_download_status=="1")
					{
						btn.value = '<%:Downloading Successful%>';
					}
					else if (status.rule_download_status=="2")
					{
						btn.value = '<%:Rule No Change%>';
					}
				}
				else {
					btn.value = '<%:Downloading Timeout%>';
				}
			}
		);
		btn.disabled = false;
		return false;
	};
	
	function catch_netflix_domains()
	{
		var legend = document.getElementById('catch-netflix-state');
		var output = document.getElementById('catch-netflix-output');
		var r = confirm("<%:Attention:%>\n<%:The catch result will be automatically saved%>\n\n1. <%:Please make sure the OpenClash works normally%>\n2. <%:The domains catch time is one minute%>\n3. <%:About to open fast.com%>\n4. <%:You can also try to catch while unlocking device playing%>");
    if (r == true) {
    	
			winOpen("https://fast.com/");
			
    	if (legend && output)
			{
				output.innerHTML =
					'<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' +
					'<%:Waiting for command to complete...%>';

				legend.parentNode.style.display = 'block';
				legend.style.display = 'inline';
				catch_num = 0;
				catch_out = "";
				get_netflix_domains();
			}
		}
	};
	
	function strUnique(str){
    var ret = [];
    str.replace(/[^,]+/g, function($1, $2) { 
    	(str.indexOf($1) == $2) && ret.push($1);
    });
    return ret.join('\n');
	}
	
	function get_netflix_domains()
	{
		var legend = document.getElementById('catch-netflix-state');
		var output = document.getElementById('catch-netflix-output');
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "catch_netflix_domains")%>', null, function(x, status) {
				if (x && x.status == 200 && x.responseText != "")
				{
					if (catch_out != "") {
						catch_out = catch_out + x.responseText;
					}
					else
					{
						catch_out = x.responseText;
					}
				}
			}
		);
		
		catch_num = catch_num + 1;
		
		if ( catch_num < 20 ) {
			catch_timeout = setTimeout("get_netflix_domains()", 3000);
		}
		else {
			clearTimeout(catch_timeout);
			if (catch_out != "")
			{
				legend.style.display = 'none';
				output.innerHTML = '<textarea class="cbi-input-textarea" style="width: 100%;display:inline" data-update="change" rows="10" cols="50" readonly="readonly" >'+strUnique(catch_out)+'</textarea>';
				XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "write_netflix_domains")%>', {domains: strUnique(catch_out)}, function(x, status) {});
			}
			else
			{
				legend.style.display = 'none';
				output.innerHTML = '<span class="error"><%:No domain names were catched...%></span>';
			}
		}
	};
	
//]]></script>


<input type="button" class="btn cbi-button cbi-input-reload" value="<%:Netflix%>" onclick="return act_download_netflix_rule(this)" />
<input type="button" class="btn cbi-button cbi-input-reload" value="<%:Disney Plus%>" onclick="return act_download_disney_rule(this)" />
<input type="button" class="btn cbi-button cbi-input-reload" value="<%:Catch Netflix%>" onclick="return catch_netflix_domains(this)" />
<fieldset style="display: none;margin: 0 auto;">
	<legend id="catch-netflix-state"><%:Collecting data...%></legend>
	<br />
	<span id="catch-netflix-output"></span>
</fieldset>
<%+cbi/valuefooter%>
