
<fieldset class="cbi-section">
	<table width="100%">
	  <tr><td width="100%" colspan="4">
	    <p align="center" id="update_tip">
	  		<b><%:Note: if the update fails, you can manually download and upload%></b>
	  	</p>
	  </td></tr>
	  <tr><td width="25%"><%:Compiled Version Selected%></td>
	  	<td width="25%" align="left"><select id="CORE_VERSION">
	  		<option value="linux-386"><%:linux-386%></option>
				<option value="linux-amd64"><%:linux-amd64(x86-64)%></option>
				<option value="linux-amd64-v3"><%:linux-amd64-v3(x86-64)%></option>
				<option value="linux-armv5"><%:linux-armv5%></option>
				<option value="linux-armv6"><%:linux-armv6%></option>
				<option value="linux-armv7"><%:linux-armv7%></option>
				<option value="linux-arm64"><%:linux-arm64(armv8)%></option>
				<option value="linux-mips-hardfloat"><%:linux-mips-hardfloat%></option>
				<option value="linux-mips-softfloat"><%:linux-mips-softfloat%></option>
				<option value="linux-mips64"><%:linux-mips64%></option>
				<option value="linux-mips64le"><%:linux-mips64le%></option>
				<option value="linux-mipsle-softfloat"><%:linux-mipsle-softfloat%></option>
				<option value="linux-mipsle-hardfloat"><%:linux-mipsle-hardfloat%></option>
				<option value="0"><%:Not Set%></option>
	  	</select></td>
	  	<td width="25%"><%:Release Branch Selected%></td>
	  	<td width="25%" align="left"><select id="RELEASE_BRANCH">
	  		<option value="master">Master</option>
				<option value="dev">Developer</option>
	  	</select></td>
	  	</tr>
		<tr>
			<td width="25%"><%:CPU Architecture%></td><td width="25%" align="left" id="CPU_MODEL"><%:Collecting data...%></td>
			<td width="25%"><%:Last Check Update%></td><td width="25%" align="left" id="CHECKTIME"><%:Collecting data...%></td>
		</tr>
	  <tr><td width="100%" colspan="4">
	    <p align="center">
	  		<b><%:Core path:%>/etc/openclash/core/clash</b>
	  	</p>
	  </td></tr>
		<tr><td width="25%">[Dev] <%:Current Core%></td><td width="25%" align="left" id="CORE_CV"><%:Collecting data...%></td><td width="25%">[Dev] <%:Latest Core%></td><td width="25%" align="left" id="CORE_LV"><%:Collecting data...%></td></tr>
		<tr><td width="25%"><%:Update Core%></td><td width="25%" align="left" id="core_up"><%:Collecting data...%></td><td width="25%"><%:Download Latest Core%></td><td width="25%" align="left" id="ma_core_up"><%:Collecting data...%></td></tr>
		<tr><td width="100%" colspan="4">
	    <p align="center">
	  		<b><%:Core path:%>/etc/openclash/core/clash_tun</b>
	  	</p>
	  </td></tr>
		<tr><td width="25%">[TUN] <%:Current Core%></td><td width="25%" align="left" id="CORE_TUN_CV"><%:Collecting data...%></td><td width="25%">[TUN] <%:Latest Core%></td><td width="25%" align="left" id="CORE_TUN_LV"><%:Collecting data...%></td></tr>
		<tr><td width="25%"><%:Update Core%></td><td width="25%" align="left" id="core_tun_up"><%:Collecting data...%></td><td width="25%"><%:Download Latest Core%></td><td width="25%" align="left" id="ma_core_tun_up"><%:Collecting data...%></td></tr>
		<tr><td width="100%" colspan="4">
	    <p align="center">
	  		<b><%:Core path:%>/etc/openclash/core/clash_meta</b>
	  	</p>
	  </td></tr>
		<tr><td width="25%">[Meta] <%:Current Core%></td><td width="25%" align="left" id="CORE_META_CV"><%:Collecting data...%></td><td width="25%">[Meta] <%:Latest Core%></td><td width="25%" align="left" id="CORE_META_LV"><%:Collecting data...%></td></tr>
		<tr><td width="25%"><%:Update Core%></td><td width="25%" align="left" id="core_meta_up"><%:Collecting data...%></td><td width="25%"><%:Download Latest Core%></td><td width="25%" align="left" id="ma_core_meta_up"><%:Collecting data...%></td></tr>
	</table>
</fieldset>
<fieldset class="cbi-section">
	<table width="100%">
		<tr><td width="100%" colspan="4">
	    <p align="center">
	  		<b><%:Client Update%></b>
	  	</p>
	  </td></tr>
		<tr><td width="25%"><%:Current Client%></td><td width="25%" align="left" id="OP_CV"><%:Collecting data...%></td><td width="25%"><%:Latest Client%></td><td width="25%" align="left" id="OP_LV"><%:Collecting data...%></td></tr>
	  <tr><td width="25%"><%:Update Client%></td><td width="25%" align="left" id="op_up"><%:Collecting data...%></td><td width="25%"><%:Download Latest Client%></td><td width="25%" align="left" id="ma_op_up"><%:Collecting data...%></td></tr>
	</table>
</fieldset>
<fieldset class="cbi-section">
	<table width="100%">
	  <tr>
	  	<td width="25%">
	  	   <p align="center" id="restore">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="25%">
	  	   <p align="center" id="remove_core">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	    <td width="25%">
	  	   <p align="center" id="one_key_update">
	  		   <%:Collecting data...%>
	  	   </p>
	    </td>
	  </tr>
	</table>
</fieldset>
<fieldset class="cbi-section">
	<table width="100%">
		<tr><td width="100%" colspan="4">
	    <p align="center">
	  		<b><%:Backup Section%></b>
	  	</p>
		</td></tr>
		<tr>
	  	<td width="25%">
	  	   <p align="center" id="backup">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="25%">
	  	   <p align="center" id="backup_ex_core">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="25%">
	  	   <p align="center" id="backup_core_only">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
		</tr>
		<tr>
	  	<td width="25%">
	  	   <p align="center" id="backup_config_only">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="25%">
	  	   <p align="center" id="backup_rule_only">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="25%">
	  	   <p align="center" id="backup_proxy_only">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
		</tr>
	</table>
</fieldset>

<script type="text/javascript">//<![CDATA[
	var core_version = document.getElementById('CORE_VERSION');
 	var checktime = document.getElementById('CHECKTIME');
	var cpu_model = document.getElementById('CPU_MODEL');
	var core_cv = document.getElementById('CORE_CV');
	var core_lv = document.getElementById('CORE_LV');
	var core_tun_cv = document.getElementById('CORE_TUN_CV');
	var core_tun_lv = document.getElementById('CORE_TUN_LV');
	var core_meta_cv = document.getElementById('CORE_META_CV');
	var core_meta_lv = document.getElementById('CORE_META_LV');
	var op_cv = document.getElementById('OP_CV');
	var op_lv = document.getElementById('OP_LV');
	var core_up = document.getElementById('core_up');
	var core_tun_up = document.getElementById('core_tun_up');
	var core_meta_up = document.getElementById('core_meta_up');
	var op_up = document.getElementById('op_up');
	var update_tip = document.getElementById('update_tip');
	var ma_core_up  = document.getElementById('ma_core_up');
	var ma_core_tun_up  = document.getElementById('ma_core_tun_up');
	var ma_core_meta_up  = document.getElementById('ma_core_meta_up');
	var ma_op_up  = document.getElementById('ma_op_up');
	var restore  = document.getElementById('restore');
	var backup  = document.getElementById('backup');
	var backup_ex_core  = document.getElementById('backup_ex_core');
	var backup_core_only = document.getElementById('backup_core_only');
	var backup_config_only = document.getElementById('backup_config_only');
	var backup_rule_only = document.getElementById('backup_rule_only');
	var backup_proxy_only = document.getElementById('backup_proxy_only');
	var one_key_update  = document.getElementById('one_key_update');
	var remove_core  = document.getElementById('remove_core');
	var release_branch  = document.getElementById('RELEASE_BRANCH');
	core_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_update(this,\'Dev\')"/>';
	core_tun_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_update(this,\'TUN\')"/>';
	core_meta_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_update(this,\'Meta\')"/>';
	op_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return op_update(this)"/>';
	ma_core_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_core_update(this,\'Dev\')"/>';
	ma_core_tun_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_core_update(this,\'TUN\')"/>';
	ma_core_meta_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_core_update(this,\'Meta\')"/>';
	ma_op_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_op_update(this)"/>';
	restore.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Restore Default Config%>" onclick="return restore_config(this)"/>';
	one_key_update.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:One Click Check Update%>" onclick="return all_one_key_update(this)"/>';
	remove_core.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Remove Core%>" onclick="return remove_all_core(this)"/>';
	backup.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup OpenClash%>" onclick="return backup_all_file(this)"/>';
	backup_ex_core.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Exclude Cores%>" onclick="return backup_no_core(this)"/>';
	backup_core_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Core%>" onclick="return backup_only_core(this)"/>';
	backup_config_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Config%>" onclick="return backup_only_config(this)"/>';
	backup_rule_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Rule Provider%>" onclick="return backup_only_rule(this)"/>';
	backup_proxy_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Proxy Provider%>" onclick="return backup_only_proxy(this)"/>';

	XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "update_info")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {
			if ( status.corever != "0" && status.corever != "" ) {
		  		core_version.value = status.corever;
			}
			else {
		  		core_version.value = "0";
			}
			if ( status.release_branch != "" ) {
		  		release_branch.value = status.release_branch;
			}
			else {
		  		release_branch.value = "master";
			}
		}
		else {
			core_version.value = "0";
			release_branch.value = "master";
		}
	});
	
	XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "update")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {
			cpu_model.innerHTML = status.coremodel ? "<b style=color:green>"+status.coremodel+"</b>" : "<b style=color:red><%:Model Not Found%></b>";
			if ( status.upchecktime != "1" ) {
				checktime.innerHTML = "<b style=color:green>"+status.upchecktime+"</b>";
			}
			else {
				checktime.innerHTML = "<b style=color:red><%:Check Failed%></b>";
			}
			if ( status.corecv == "0" ) {
				core_cv.innerHTML = "<b style=color:red><%:File Not Exist%></b>";
			}
			else if (status.corecv != "") {
				core_cv.innerHTML = "<b style=color:green>"+status.corecv+"</b>";
			}
			else {
				core_cv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			if ( status.coretuncv == "0" ) {
				core_tun_cv.innerHTML = "<b style=color:red><%:File Not Exist%></b>";
			}
			else if (status.coretuncv != "") {
				core_tun_cv.innerHTML = "<b style=color:green>"+status.coretuncv+"</b>";
			}
			else {
				core_tun_cv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			if ( status.coremetacv == "0" ) {
				core_meta_cv.innerHTML = "<b style=color:red><%:File Not Exist%></b>";
			}
			else if (status.coremetacv != "") {
				core_meta_cv.innerHTML = "<b style=color:green>"+status.coremetacv+"</b>";
			}
			else {
				core_meta_cv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			var corelv = status.corelv;
			var arr_core = corelv.split(",");
			var corelvis = arr_core[0];
			var coretunlvis = arr_core[1];
			var coremetalvis = arr_core[2];
			if (corelvis != status.corecv && corelvis != "" && corelvis != "\n") {
			   core_lv.innerHTML = "<b style=color:green>"+corelvis+"<%:<New>%></b>";
			}
			else if (corelvis != "" && corelvis == status.corecv && corelvis != "\n") {
			   core_lv.innerHTML = "<b style=color:green>"+corelvis+"</b>";
			}
			else {
			   core_lv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			if (coretunlvis != status.coretuncv && coretunlvis != "" && coretunlvis != "\n") {
			   core_tun_lv.innerHTML = "<b style=color:green>"+coretunlvis+"<%:<New>%></b>";
			}
			else if (coretunlvis != "" && coretunlvis == status.coretuncv && coretunlvis != "\n") {
			   core_tun_lv.innerHTML = "<b style=color:green>"+coretunlvis+"</b>";
			}
			else {
			   core_tun_lv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			if (coremetalvis != status.coremetacv && coremetalvis != "" && coremetalvis != "\n") {
			   core_meta_lv.innerHTML = "<b style=color:green>"+coremetalvis+"<%:<New>%></b>";
			}
			else if (coremetalvis != "" && coremetalvis == status.coremetacv && coremetalvis != "\n") {
			   core_meta_lv.innerHTML = "<b style=color:green>"+coremetalvis+"</b>";
			}
			else {
			   core_meta_lv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
			var oplv = status.oplv;
			var arr_op = oplv.split(",");
			var oplvis = arr_op[0];
			var new_op = arr_op[1];
			op_cv.innerHTML = status.opcv ? "<b style=color:green>"+status.opcv+"</b>" : "<b style=color:red><%:Unknown%></b>";
			if ( new_op == "2" && oplvis != "" && oplvis != "\n") {
			   op_lv.innerHTML = "<b style=color:green>"+oplvis+"<%:<New>%></b>";
			}
			else if (oplvis != "" && oplvis != "\n") {
			   op_lv.innerHTML = "<b style=color:green>"+oplvis+"</b>";
			}
			else {
			   op_lv.innerHTML = "<b style=color:red><%:Unknown%></b>";
			}
		}
	});

	XHR.poll(7, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "startlog")%>', status.startlog, function(x, status) {
		if ( x && x.status == 200 ) {
			if ( status.startlog == "\n" || status.startlog == "" ) {
		    	var rdmdl=Math.floor(Math.random()*2)+1;
				if(rdmdl==1)
				{
					update_tip.innerHTML = '<b><font><%:Note: if the update fails, you can manually download and upload%></font></b>';
				}
				if(rdmdl==2)
				{
					update_tip.innerHTML = '<b><font><%:Note: the client may not support update, because the firmware with squashfs format will not release flash space after updating%></font></b>';
				}
      		 }
   		}
	});

	XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "startlog")%>', null, function(x, status) {
	  if ( x && x.status == 200 ) {
		if ( status.startlog.match("level=fatal") || status.startlog.indexOf("FTL [Config]") != "-1" ) {
	    	XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "del_start_log")%>', null, function(x) {});
			if ( status.startlog.match("level=fatal") ) {
				alert('<%:OpenClash Start Failed%> :\n\n' + status.startlog.split('msg=')[1]);
			}
	    	else {
				alert('<%:OpenClash Start Failed%> :\n\n' + status.startlog.split('FTL [Config] ')[1]);
			};
		}
		else if ( status.startlog != "\n" && status.startlog != "" ) {
	    	update_tip.innerHTML = '<b style=color:green>'+status.startlog+'</b>';
		};
	  };
	});
	
	function core_update(btn,type)
    {
    	var v = core_version.value;
    	var r = release_branch.value;
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "save_corever_branch")%>', {core_ver: v, release_branch: r}, function(x, status) {
				if (x && x.status == 200) {
					XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "coreupdate")%>', {core_type: type}, function(x, status) {
        	btn.value    = '<%:Check And Update%>';
        	btn.disabled = false;
        	return false;
        	});
				}
		});
    }

	function op_update(btn)
    {
        var r = release_branch.value;
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "save_corever_branch")%>', {release_branch: r}, function(x, status) {
		if (x && x.status == 200) {
           XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "opupdate")%>', null, function(x, status) {
           btn.value    = '<%:Check And Update%>';
           btn.disabled = false;
           return false;
           });
        }
		});
    }
    
    function ma_core_update(btn,type)
    {
    	var v = core_version.value;
		var r = release_branch.value;
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "save_corever_branch")%>', {core_ver: v, release_branch: r}, function(x, status) {
		if (x && x.status == 200) {
			btn.value    = '<%:Download%>';
			btn.disabled = false;
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "update_ma")%>', status.corever, function(x, status) {
			if ( x && x.status == 200 ) {
				if ( status.corever != "0" ) {
					if (type == "Dev") {
						url1='https://raw.githubusercontent.com/vernesong/OpenClash/core/'+r+'/dev/clash-'+status.corever+'.tar.gz';
						window.location.href=url1;
					}
					if (type == "TUN") {
						var corelv = status.corelv;
						var arr_core = corelv.split(",");
						var coretunlvis = arr_core[1];
					if ( coretunlvis != "" ) {
						url3='https://raw.githubusercontent.com/vernesong/OpenClash/core/'+r+'/premium/clash-'+status.corever+'-'+coretunlvis+'.gz';
					window.location.href=url3;
					}
					else {
					alert('<%:Failed to get the latest version. Please try again later!%>')
					}
					}
					if (type == "Meta") {
						url4='https://raw.githubusercontent.com/vernesong/OpenClash/core/'+r+'/meta/clash-'+status.corever+'.tar.gz';
				window.location.href=url4;
					}
				}
				else {
					alert('<%:No Compiled Version is Selected, Please Select on The Top and Try Again!%>')
				}
			}
       		});
        return false;
    	}
      });
    }
    
    function ma_op_update(btn)
    {
        btn.value    = '<%:Download%>';
        btn.disabled = false;
        var r = release_branch.value;
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "save_corever_branch")%>', {release_branch: r}, function(x, status) {
			if (x && x.status == 200) {
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "update_ma")%>', status.oplv, function(x, status) {
				if ( x && x.status == 200 ) {
					var oplv = status.oplv;
					var oplvis = oplv.substring(oplv.indexOf("v") + 1,oplv.indexOf(","));
					if ( oplvis != "" ) {
						url2='https://raw.githubusercontent.com/vernesong/OpenClash/package/'+r+'/luci-app-openclash_'+oplvis+'_all.ipk';
						window.location.href=url2;
					}
					else {
						alert('<%:Failed to get the latest version. Please try again later!%>')
					}
				}
			});
			}
        });
        return false; 
    }
    
    function remove_all_core(btn)
    {
        btn.value    = '<%:Remove Core%>';
        btn.disabled = true;
        var r = confirm("<%:Are you sure want to remove all core files?%>")
        if (r == true) {
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "remove_all_core")%>', null, function(x, status) {
        	if ( x && x.status == 200 ) {
        		alert('<%:Remove succeeded!%>')
        		window.location.href='<%="settings?tab.openclash.config=version_update"%>';
        	}
          	else {
            alert('<%:Remove failed!%>')
           }
        });
        } else {
        }
        btn.disabled = false;
        return false; 
    }
    
    function backup_all_file(btn)
    {
		btn.value    = '<%:Backup OpenClash%>';
		btn.disabled = true;
		window.location.href='<%="backup"%>';
		btn.disabled = false;
		return false; 
    }

    function backup_no_core(btn)
    {
		btn.value    = '<%:Backup Exclude Cores%>';
		btn.disabled = true;
		window.location.href='<%="backup_ex_core"%>';
		btn.disabled = false;
		return false; 
    }

    function backup_only_core(btn)
    {
		btn.value    = '<%:Backup Core%>';
		btn.disabled = true;
		window.location.href='<%="backup_only_core"%>';
		btn.disabled = false;
		return false; 
    }

    function backup_only_config(btn)
    {
		btn.value    = '<%:Backup Config%>';
		btn.disabled = true;
		window.location.href='<%="backup_only_config"%>';
		btn.disabled = false;
		return false; 
    }

    function backup_only_rule(btn)
    {
		btn.value    = '<%:Backup Rule Provider%>';
		btn.disabled = true;
		window.location.href='<%="backup_only_rule"%>';
		btn.disabled = false;
		return false; 
    }

    function backup_only_proxy(btn)
    {
		btn.value    = '<%:Backup Proxy Provider%>';
		btn.disabled = true;
		window.location.href='<%="backup_only_proxy"%>';
		btn.disabled = false;
		return false; 
    }

    function all_one_key_update(btn)
    {
    	var v = core_version.value;
		var r = release_branch.value;
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "save_corever_branch")%>', {core_ver: v, release_branch: r}, function(x, status) {
			if (x && x.status == 200) {
				btn.value    = '<%:One Click Check Update%>';
				btn.disabled = true;
				var r = confirm("<%:Check and update all Cores and plug-ins?%>")
				if (r == true) {
					XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "one_key_update_check")%>', null, function(x, status) {
						if ( x && x.status == 200 ) {
							if ( status.corever != "0" ) {
								XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "one_key_update")%>', null, function(x, status) {
									if ( x && x.status != 200 ) {
										alert('<%:Check failed, Please try again later!%>')
									}
								});
								}
							else {
								alert('<%:No Compiled Version is Selected, Please Select on The Top and Try Again!%>')
							}
						}
						else {
							alert('<%:Check failed, Please try again later!%>')
							}
					});
				}
				btn.disabled = false;
				return false;
			}
		});
    }

//]]></script>
