<%-
local uci = require "luci.model.uci"
local sys = require "luci.sys"
local cel_module  = uci:get("network", "cellular", "module")
sys.exec("echo %s > /tmp/module" % cel_module)
-%>
<%+header%> 

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

function init_page()
{
	var firmware = document.getElementById("firmware");
	var length = firmware.options.length;
	firmware.selectedIndex = 0;
	for (i = 1; i < length; i++)
	{
		firmware.remove(1);
	}

	var table = document.getElementById('dot');
	for ( i=0;i<table.rows.length;i++){
		for(j=0;j<table.rows[i].cells.length;j++){
			table.rows[i].deleteCell(0);
		}
	}

	<%if cel_module:match("EM74%d+") then%>
	{
		var carrier = document.getElementById("carrier");
		var length = carrier.options.length;
		carrier.selectedIndex = 0;
		for (i = 1; i < length; i++)
		{
			carrier.remove(1);
		}
	}
	<%end%>

	var updatebtn = document.getElementById('updatebtn');
	var checkbtn = document.getElementById('checkbtn');
	var stxhr = new XHR();
	stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "radioupdate")%>/radioupdate_init' , null,
		function(x, mArray)
		{
		}
		);
	updatebtn.disabled = true;
	checkbtn.disabled = false;
	firmware.disabled = true;
	<%if cel_module:match("EM74%d+") then%>
		carrier.disabled = true;
	<%end%>	
}

function toggleupdate()
{
	var updatebtn = document.getElementById('updatebtn');
	var firmware = document.getElementById("firmware");
	<%if cel_module:match("EM74%d+") then%>
		var carrier = document.getElementById("carrier");
		if(firmware.selectedIndex!=0 && carrier.selectedIndex!=0)
		{
			updatebtn.disabled = false;	
		}
		else
		{
			updatebtn.disabled = true;
		}
	<%else%>
		if(firmware.selectedIndex!=0)
		{
			updatebtn.disabled = false;	
		}
		else
		{
			updatebtn.disabled = true;
		}
	<%end%>
} 

function check_firmware()
{
	var i=0;
	var updatebtn = document.getElementById('updatebtn');
	updatebtn.disabled=true;
	var checkbtn = document.getElementById('checkbtn');
	checkbtn.disabled = true;
	var firmware = document.getElementById("firmware");
	var length = firmware.options.length;
	firmware.selectedIndex = 0;
	for (i = 1; i < length; i++) {
		firmware.remove(1);
	}

	<%if cel_module:match("EM74%d+") then%>
		var carrier = document.getElementById("carrier");
		length = firmware.options.length;
		carrier.selectedIndex = 0;
		for (i = 1; i < length; i++) {
			carrier.remove(1);
		}
	<%end%>

	var table = document.getElementById('dot');
	for ( i=0;i<table.rows.length;i++){
		for(j=0;j<table.rows[i].cells.length;j++){
			table.rows[i].deleteCell(0);
		}
	}

	var stxhr = new XHR();
	stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "radioupdate")%>/radioupdate_update' + '/' + "M" + '/' + "check" + '/' + "NULL" + '/' + "NULL" , null,
		function(x, mArray)
		{
			// console.log("4:",mArray.length);
			if (mArray.length > 0)
			{
				if(mArray.length && mArray.length==1)
				{
					alert(mArray.firmwares[0]);
				}
				else
				{
					var min = 1;
					var max=mArray.length;
					var latest=mArray.firmwares[0];
					for (i = min; i < max; i++)
					{
						var opt = document.createElement('option');
						opt.value = mArray.firmwares[i];
						opt.innerHTML = mArray.firmwares[i];
						<%if cel_module:match("EM74%d+") then%>
							if(opt.value.includes(".nvu"))
							{
								// alert("appendCarrier")
								carrier.appendChild(opt);
							}
							else
							{
								// alert("appendFirmware")
								firmware.appendChild(opt);
							}
						<%else%>
							firmware.appendChild(opt);
						<%end%>
					}
				}
			}else{
				alert("Could not finish checking.");
			}
		}
		);
	checkbtn.disabled = false;
	firmware.disabled = false
	<%if cel_module:match("EM74%d+") then%>
		carrier.disabled = false;
	<%end%>
}

function update_firmware()
{
	var firmware = document.getElementById('firmware');
	var filename_fw=firmware.value;
	var updatebtn = document.getElementById('updatebtn');
	var checkbtn = document.getElementById('checkbtn');

  <%if cel_module:match("EM74%d+") then%>
		var carrier = document.getElementById('carrier');
		var filename_cr=carrier.value;		
		if (filename_fw != "NULL" && filename_cr != "NULL")
		{
			carrier.disabled = true;
	<%else%>
		if (filename_fw != "NULL")
		{
	<%end%>
			firmware.disabled = true;
			checkbtn.disabled = true;
			updatebtn.disabled= true;
			upgrade="update"
			var stxhr = new XHR();

			<%if cel_module:match("EM74%d+") then%>
			    stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "radioupdate")%>/radioupdate_update' + '/' + "M" + '/' + upgrade  + '/' + filename_fw + '/' + filename_cr , null,
			<%else%>
				stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "radioupdate")%>/radioupdate_update' + '/' + "M" + '/' + upgrade + '/' + filename_fw + '/' + "NULL" , null,
			<%end%>
					function(x, mArray)
					{
						if (mArray && mArray.length && mArray.length > 0)
						{
							var min = 0;
							var data="";
							var max=mArray.length;
							for (i = min; i < max; i++) {
								data = data + mArray.firmwares[i] + ".";
							}
							alert(data);
						}
						else if (mArray && mArray.length)
						{
							alert("Could not finish updating.");
						}
					}
					);
		}
		else
		{
			alert("Please select Proper firmware to upload");
		}
}

XHR.poll(10, '<%=luci.dispatcher.build_url("admin", "services", "radioupdate","radioupdate_status" )%>', { status: 1 },
	function(x, mArray)
	{
		// console.log("2:",mArray.length);
		var data="";
		if (mArray && mArray.length && mArray.length > 0)
		{
			var min = 0;
			var max=mArray.length;
			var max_p=0;
			var table = document.getElementById('dot');
			for(var i=0;i<table.rows.length;i++) {
				max_p += table.rows[i].cells.length;
			}
			for (i = max_p; i < max ; i++) {
				var row = table.insertRow(0);
				var cell = row.insertCell(0);
				cell.innerHTML = mArray.data[i];
			}
		}
		else
		{
			var table = document.getElementById('dot');
			for ( i=0;i<table.rows.length;i++){
				for(j=0;j<table.rows[i].cells.length;j++){
					table.rows[i].deleteCell(0);
				}
			}
		}
	}
	); 
	//]]></script> 
	<body onload="init_page()">
		<div id="buttoncss" align="center">
			<table class="cbi-section-table" style="width:940px; margin-left:5px">
				<tr class="cbi-section-table-descr">
					<%if cel_module:match("WP7%d+") then%>
						<th class="cbi-section-table-cell" style="width:100px"><%:Type of upgrade%></th>
					<%end%>
					<th class="cbi-section-table-cell" style="width:100px"><%:Check for update%></th>
					<th class="cbi-section-table-cell" style="width:350px"><%:Available Firmwares%></th>
					<%if cel_module:match("EM74%d+") then%>
						<th class="cbi-section-table-cell" style="width:350px"><%:Available Carrier Configs%></th>
					<%end%>
					<!-- <th class="cbi-section-table-cell" style="width:50px"><%:Force Upgrade%></th> -->
					<!-- <th class="cbi-section-table-cell" style="width:100px"><%:Upgrade%></th> -->
				</tr>
				<tr>
					<%if not cel_module:match("WP7%d+") then%>
						<td>
							<input  id="checkbtn" onclick="check_firmware()" type="button" style="width:200px" value="<%:Check for update%>" class="cbi-button cbi-button-apply"/>
						</td>
					<%else%>
						<td>
							<select id="channel" onchange="init_page()" class="cbi-input-select" name="channel">
								<option value="sierra_fw" selected="selected">Firmware</option>
								<option value="volte_patches">VOLTE Patch</option>
							</select>
						</td>
						<td>
							<input  id="checkbtn" onclick="check_firmware()" type="button" value="<%:Check for update%>" class="cbi-button cbi-button-apply"/>
						</td>
					<%end%>
					<td>
						<select id="firmware" onchange="toggleupdate()" disabled  style="width:350px" class="cbi-input-select" name="firmware">
							<option value="NULL" selected="selected">Please Select file</option>
						</select>
					</td>
					<%if cel_module:match("EM74%d+") then%>
						<td>
							<select id="carrier" onchange="toggleupdate()" disabled  style="width:350px" class="cbi-input-select" name="carrier">
								<option value="NULL" selected="selected">Please Select file</option>
							</select>
						</td>
					<%end%>
				</tr>
			</table>
		</div>

		<div id="buttoncss" align="center">
			<table class="cbi-section-table" style="width:200px; margin-left:5px">
					<td>
						<input id="updatebtn" onclick="update_firmware()" type="button" disabled value="<%:Update now%>" class="cbi-button cbi-button-apply"/>
					</td>
			</table>
		</div>	
		<fieldset class="cbi-section">
			<table width="100%" cellspacing="10" id="dot">
				<tr></tr>
			</table>
		</fieldset>
	</body>

<%+footer%>
