<style>
.NewsTdHeight{ line-height:20px; }
</style>
<fieldset class="cbi-section">
	<table width="100%" class="NewsTdHeight">
		<tr><td width="35%" align="right"><%:Select the device model:%></td>
			<td  width="65%" align="left">
				<select name="amlogic_soc" id="amlogic_soc" style="width:auto" onchange="sel_dtb_input(this.options[this.selectedIndex].value)">
				<option value="0"><%:Select List%></option>
				<option value="99"><%:Enter the dtb file name%></option>
				</select>
			</td>
		</tr>
		<tr id="tr_sel_dtb" style="display:none;"><td width="30%" align="right"><%:Enter the dtb file name:%></td><td  width="70%" align="left"><input class="cbi-input-file" style="width: 235px" type="text" id="amlogic_dtb" name="amlogic_dtb" /></td></tr>
		<tr id="tr_sel_soc" style="display:none;"><td width="30%" align="right"><%:Enter the soc name:%></td><td  width="70%" align="left"><input class="cbi-input-file" style="width: 235px" type="text" id="amlogic_socname" name="amlogic_socname" /></td></tr>
		<tr id="tr_sel_uboot" style="display:none;"><td width="30%" align="right"><%:Enter the uboot_overload name:%></td><td  width="70%" align="left"><input class="cbi-input-file" style="width: 235px" type="text" id="amlogic_uboot" name="amlogic_uboot" /></td></tr>
		<tr>
			<td width="30%" align="right"><%:Install OpenWrt:%></td>
			<td  width="70%" align="left">
				<input type="button" class="cbi-button cbi-button-reload" value="<%:Install%>" onclick="return amlogic_install(this, '<%=self:cfgvalue(section)%>')"/>　<span id="_check_install"></span>
			</td>
		</tr>
	</table>
</fieldset>
<script type="text/javascript">//<![CDATA[

	// Show custom dtb input box
	function sel_dtb_input(soc_value){
		if ( soc_value == "99" )
		{
			document.getElementById('tr_sel_dtb').style = '';
			document.getElementById('tr_sel_soc').style = '';
			document.getElementById('tr_sel_uboot').style = '';
		}
		else
		{
			document.getElementById('tr_sel_dtb').style = 'display:none;';
			document.getElementById('tr_sel_soc').style = 'display:none;';
			document.getElementById('tr_sel_uboot').style = 'display:none;';
		}
	}

	// Show the status of the install button
	function amlogic_install(btn,amlogic_install_sel)
	{
		var amlogic_soc_id = document.getElementById('amlogic_soc');
		var amlogic_soc_index = amlogic_soc_id.selectedIndex;
		var amlogic_soc_value = amlogic_soc_id.options[amlogic_soc_index].value;
		var amlogic_soc_text = amlogic_soc_id.options[amlogic_soc_index].text;

		var amlogic_dtb_id = document.getElementById('amlogic_dtb');
		var amlogic_dtb_value = amlogic_dtb_id.value;

		var amlogic_soc_name = document.getElementById('amlogic_socname');
		var amlogic_socname_value = amlogic_socname.value;

		var amlogic_ubootname = document.getElementById('amlogic_uboot');
		var amlogic_ubootname_value = amlogic_ubootname.value;

		var amlogic_confirm_info = amlogic_soc_text;
		if (confirm('<%:You have chosen:%>' + amlogic_confirm_info + ', <%:Start install?%>') != true) { return false; }

		btn.disabled = true;
		btn.value    = '<%:Installing...%> ';

		XHR.get('<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_amlogic_install")%>',
			{
				amlogic_install_sel: amlogic_soc_value + '@' + amlogic_dtb_value + ':' + amlogic_socname_value + ':' + amlogic_ubootname_value
			},
			function(x,status)
			{
				if ( x && x.status == 200 ) {
					if(status.rule_install_status!="0")
					{
						btn.value = '<%:Install Failed%>';
					}
					else
					{
						btn.value = '<%:Successful Install%>';
					}
				}
				else {
					btn.value = '<%:Install%>';
				}
			}
		);
		btn.disabled = false;
		return false;
	}

	// Read openwrt install log
	var start_check_install = document.getElementById('_check_install');
	XHR.poll(1, '<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_check_install")%>', status.start_check_install, function(x, status) {
	if ( x && x.status == 200 ) {
		if ( status.start_check_install != "\n" && status.start_check_install != "" ) {
			start_check_install.innerHTML = '<font color="blue"> '+status.start_check_install+'</font>';
		}
		if ( status.start_check_install == "\n" || status.start_check_install == "" ) {
			start_check_install.innerHTML = '';
		}
	}
	});

	// Show external modal database: /etc/model_database.txt
	var mymodel_arrlen = 0;
	var _option_codes = '<option value="0"><%:Select List%></option>';
	var _check_my_amlogic_soc = document.getElementById('amlogic_soc');
	XHR.get('<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_model_database")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {
			if ( status.my_model_database != "" ) {
				let userModelList = status.my_model_database;
				var user_arry = userModelList.split('@@@');
				var mymodel_arrlen = user_arry.length - 1;
				for ( j = 0; j < mymodel_arrlen; j++ ) {
					my_box_mode = user_arry[j].split('###');
					my_box_mode_id = my_box_mode[0];
					my_box_mode_name = my_box_mode[1];
					if ( my_box_mode_id != "" && my_box_mode_name != "" ) {
						_option_codes = _option_codes + '<option value="' + my_box_mode_id + '">[ ' + my_box_mode_id + ' ] ' + my_box_mode_name.replace(/~/g, " ") + '</option>';
					}
				}
			}
		}
		_option_codes = _option_codes + '<option value="99"><%:Enter the dtb file name%></option>';
		_check_my_amlogic_soc.innerHTML = _option_codes;
	});

//]]></script>
