
<fieldset class="cbi-section">
	<table>
	  <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" id="CORE_VERSION"><%:Collecting data...%></td><td width="25%"><%:Last Check Update%></td><td width="25%" align="left" id="CHECKTIME"><%:Collecting data...%></td></tr>
		<tr><td width="25%"><%:CPU Architecture (OS)%></td><td width="25%" align="left" id="CPU_MODEL"><%:Collecting data...%></td><td width="25%"><%:CPU Architecture (OPKG)%></td><td width="25%" align="left" id="CPU_MODEL2"><%: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_game </b>
	  	</p>
	  </td></tr>
		<tr><td width="25%">[Game] <%:Current Core%> </td><td width="25%" align="left" id="CORE_GAME_CV"><%:Collecting data...%></td><td width="25%">[Game] <%:Latest Core%></td><td width="25%" align="left" id="CORE_GAME_LV"><%:Collecting data...%></td></tr>
		<tr><td width="25%"><%:Update Core%></td><td width="25%" align="left" id="core_game_up"><%:Collecting data...%></td><td width="25%"><%:Download Latest Core%></td><td width="25%" align="left" id="ma_core_game_up"><%:Collecting data...%></td></tr>
	</table>
</fieldset>
<fieldset class="cbi-section">
	<table>
		<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="33%" colspan="2">
	  	   <p align="center" id="restore">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	  	<td width="33%" colspan="2">
	  	   <p align="center" id="remove_core">
	  		   <%:Collecting data...%>
	  	   </p>
	  	</td>
	    <td width="33%" colspan="2">
	  	   <p align="center" id="one_key_update">
	  		   <%: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 cpu_model2 = document.getElementById('CPU_MODEL2');
	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_game_cv = document.getElementById('CORE_GAME_CV');
	var core_game_lv = document.getElementById('CORE_GAME_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_game_up = document.getElementById('core_game_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_game_up  = document.getElementById('ma_core_game_up');
	var ma_op_up  = document.getElementById('ma_op_up');
	var restore  = document.getElementById('restore');
	var one_key_update  = document.getElementById('one_key_update');
	var remove_core  = document.getElementById('remove_core');
  
	XHR.poll(6, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "update")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {
		  var cpu_models = status.coremodel;
			var arr_model = cpu_models.split(",");
      var cpu_model1 = arr_model[0];
      var cpu_model22 = arr_model[1];
		  cpu_model.innerHTML = cpu_model1 ? "<b><font color=green>"+cpu_model1+"</font></b>" : "<b><font color=red><%:Model Not Found%></font></b>";
      cpu_model2.innerHTML = cpu_model22 ? "<b><font color=green>"+cpu_model22+"</font></b>" : "<b><font color=red><%:Model Not Found%></font></b>";
		  if ( status.corever != "0\n" && status.corever != "" ) {
		     core_version.innerHTML = "<b><font color=green>"+status.corever+"</font></b>";
		  }
		  else {
		     core_version.innerHTML = "<b><font color=red><%:No compiled version is selected. Please configure it in the General Settings tab!%></font></b>";
		  }
		  if ( status.upchecktime != "1" ) {
		     checktime.innerHTML = "<b><font color=green>"+status.upchecktime+"</font></b>";
		  }
		  else {
		     checktime.innerHTML = "<b><font color=red><%:Check Failed%></font></b>";
		  }
      if ( status.corecv == "0" ) {
         core_cv.innerHTML = "<b><font color=red><%:File Not Exist%></font></b>";
      }
      else if (status.corecv != "") {
         core_cv.innerHTML = "<b><font color=green>"+status.corecv+"</font></b>";
      }
      else {
         core_cv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
      }
      if ( status.coretuncv == "0" ) {
         core_tun_cv.innerHTML = "<b><font color=red><%:File Not Exist%></font></b>";
      }
      else if (status.coretuncv != "") {
         core_tun_cv.innerHTML = "<b><font color=green>"+status.coretuncv+"</font></b>";
      }
      else {
         core_tun_cv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
      }
      if ( status.coregamecv == "0" ) {
         core_game_cv.innerHTML = "<b><font color=red><%:File Not Exist%></font></b>";
      }
      else if (status.coregamecv != "") {
         core_game_cv.innerHTML = "<b><font color=green>"+status.coregamecv+"</font></b>";
      }
      else {
         core_game_cv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
      }
      var corelv = status.corelv;
      var arr_core = corelv.split(",");
      var corelvis = arr_core[0];
      var coretunlvis = arr_core[1];
      var coregamelvis = arr_core[2];
			if (corelvis != status.corecv && corelvis != "" && corelvis != "\n") {
			   core_lv.innerHTML = "<b><font color=green>"+corelvis+"<%:<New>%></font></b>";
			}
			else if (corelvis != "" && corelvis != "\n" && corelvis == status.corecv) {
			   core_lv.innerHTML = "<b><font color=green>"+corelvis+"</font></b>";
			}
			else {
			   core_lv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
			}
			if (coretunlvis != status.coretuncv && coretunlvis != "" && coretunlvis != "\n") {
			   core_tun_lv.innerHTML = "<b><font color=green>"+coretunlvis+"<%:<New>%></font></b>";
			}
			else if (coretunlvis != "" && coretunlvis != "\n" && coretunlvis == status.coretuncv) {
			   core_tun_lv.innerHTML = "<b><font color=green>"+coretunlvis+"</font></b>";
			}
			else {
			   core_tun_lv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
			}
			if (coregamelvis != status.coregamecv && coregamelvis != "" && coregamelvis != "\n") {
			   core_game_lv.innerHTML = "<b><font color=green>"+coregamelvis+"<%:<New>%></font></b>";
			}
			else if (coregamelvis != "" && coregamelvis != "\n" && coregamelvis == status.coregamecv) {
			   core_game_lv.innerHTML = "<b><font color=green>"+coregamelvis+"</font></b>";
			}
			else {
			   core_game_lv.innerHTML = "<b><font color=red><%:Unknown%></font></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><font color=green>"+status.opcv+"</font></b>" : "<b><font color=red><%:Unknown%></font></b>";
			if ( new_op == "2" ) {
			   op_lv.innerHTML = "<b><font color=green>"+oplvis+"<%:<New>%></font></b>";
			}
			else if (oplvis != "" && oplvis != "\n") {
			   op_lv.innerHTML = "<b><font color=green>"+oplvis+"</font></b>";
			}
			else {
			   op_lv.innerHTML = "<b><font color=red><%:Unknown%></font></b>";
			}
		}
	});
	
	core_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_update(this)"/>';
	core_tun_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_tun_update(this)"/>';
	core_game_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return core_game_update(this)"/>';
	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)"/>';
	ma_core_tun_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_core_tun_update(this)"/>';
	ma_core_game_up.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Download%>" onclick="return ma_core_game_update(this)"/>';
	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-remove" value="<%:Remove Core%>" onclick="return remove_all_core(this)"/>';
	
	function winOpen(url)
	  {
	  	var winOpen = window.open(url);
      if(winOpen == null || typeof(winOpen) == 'undefined'){
         window.location.href=url;
      }
	  }
	  
	function core_update(btn)
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "coreupdate")%>', null, function(x, status) {
        btn.value    = '<%:Check And Update%>';
        btn.disabled = false;
        return false;
        });
    }
    
	function core_tun_update(btn)
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "coretunupdate")%>', null, function(x, status) {
        btn.value    = '<%:Check And Update%>';
        btn.disabled = false;
        return false;
        });
    }
    
	function core_game_update(btn)
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "coregameupdate")%>', null, function(x, status) {
        btn.value    = '<%:Check And Update%>';
        btn.disabled = false;
        return false;
        });
    }
	
	function op_update(btn)
    {

        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)
    {
        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\n" ) {
              url1='https://github.com/vernesong/OpenClash/releases/download/Clash/clash-'+status.corever+'.tar.gz';
              winOpen(url1);
           }
           else {
              alert('<%:No compiled version is selected. Please configure it in the General Settings tab!%>')
           }
        }
        });
        return false; 
    }
    
    function ma_core_tun_update(btn)
    {
        btn.value    = '<%:Download%>';
        btn.disabled = false;
        XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "update_ma")%>', null, function(x, status) {
        if ( x && x.status == 200 ) {
        	 var corelv = status.corelv;
           var arr_core = corelv.split(",");
           var coretunlvis = arr_core[1];
           if ( status.corever != "0\n" && coretunlvis != "\n" && coretunlvis != "" ) {
              url3='https://github.com/vernesong/OpenClash/releases/download/TUN-Premium/clash-'+status.corever+'-'+coretunlvis+'.gz';
              winOpen(url3);
           }
           else if ( status.corever == "0\n" ) {
              alert('<%:No compiled version is selected. Please configure it in the General Settings tab!%>')
           }
           else if ( coretunlvis == "\n" || coretunlvis == "" ) {
              alert('<%:Failed to get the latest version. Please try again later!%>')
           }
        }
        });
        return false; 
    }
    
    function ma_core_game_update(btn)
    {
        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\n" ) {
              url4='https://github.com/vernesong/OpenClash/releases/download/TUN/clash-'+status.corever+'.tar.gz';
              winOpen(url4);
           }
           else {
              alert('<%:No compiled version is selected. Please configure it in the General Settings tab!%>')
           }
        }
        });
        return false; 
    }
    
    function ma_op_update(btn)
    {
        btn.value    = '<%:Download%>';
        btn.disabled = false;
        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 != "" && oplvis != "\n" ) {
              url2='https://github.com/vernesong/OpenClash/releases/download/v'+oplvis+'/luci-app-openclash_'+oplvis+'_all.ipk';
              winOpen(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"%>';
        	}
          else {
            alert('<%:Remove failed!%>')
            window.location.href='<%="settings"%>';
           }
        });
        } else {
        }
        btn.disabled = false;
        return false; 
    }
    
    function restore_config(btn)
    {
        btn.value    = '<%:Restore Default Config%>';
        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"%>';
           }
        });
        } else {
        }
        btn.disabled = false;
        return false; 
    }
    
    function all_one_key_update(btn)
    {
        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\n" ) {
        			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 configure it in the General Settings tab!%>')
           	}
        	}
          else {
            alert('<%:Check failed, Please try again later!%>')
          }
        });
				}
				else {
        }
        btn.disabled = false;
        return false; 
    }
    
    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(2, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "startlog")%>', status.startlog, function(x, status) {
	  if ( x && x.status == 200 ) {
		   if ( status.startlog != "\n" && status.startlog != "" ) {
	     update_tip.innerHTML = '<b><font color="green">'+status.startlog+'</font></b>';
	     }
	  }
	});

//]]></script>
      
