<script type="text/javascript">//<![CDATA[
	function duid2mac(duid) {
		// DUID-LLT / Ethernet
		if (duid.length === 28 && duid.substr(0, 8) === '00010001')
			return duid.substr(16).replace(/(..)(?=..)/g, '$1:').toUpperCase();

		// DUID-LL / Ethernet
		if (duid.length === 20 && duid.substr(0, 8) === '00030001')
			return duid.substr(8).replace(/(..)(?=..)/g, '$1:').toUpperCase();

		return null;
	}

	var hosts = <%=luci.http.write_json(luci.sys.net.host_hints())%>;

	XHR.poll(5, '<%=url('admin/network/dhcplease_status')%>', null,
		function(x, st)
		{
			var tb = document.getElementById('lease_status_table');
			if (st && st[0] && tb)
			{
				/* clear all rows */
				while( tb.rows.length > 1 )
					tb.deleteRow(1);

				for( var i = 0; i < st[0].length; i++ )
				{
					var timestr;

					if (st[0][i].expires === false)
						timestr = '<em><%:unlimited%></em>';
					else if (st[0][i].expires <= 0)
						timestr = '<em><%:expired%></em>';
					else
						timestr = String.format('%t', st[0][i].expires);

					var tr = tb.insertRow(-1);
						tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);

					tr.insertCell(-1).innerHTML = st[0][i].hostname ? st[0][i].hostname : '?';
					tr.insertCell(-1).innerHTML = st[0][i].ipaddr;
					tr.insertCell(-1).innerHTML = st[0][i].macaddr;
					tr.insertCell(-1).innerHTML = timestr;
				}

				if( tb.rows.length == 1 )
				{
					var tr = tb.insertRow(-1);
						tr.className = 'cbi-section-table-row';

					var td = tr.insertCell(-1);
						td.colSpan = 4;
						td.innerHTML = '<em><br /><%:There are no active leases.%></em>';
				}
			}

			var tb6 = document.getElementById('lease6_status_table');
			if (st && st[1] && tb6)
			{
				tb6.parentNode.style.display = 'block';

				/* clear all rows */
				while( tb6.rows.length > 1 )
					tb6.deleteRow(1);

				for( var i = 0; i < st[1].length; i++ )
				{
					var timestr;

					if (st[1][i].expires === false)
						timestr = '<em><%:unlimited%></em>';
					else if (st[1][i].expires <= 0)
						timestr = '<em><%:expired%></em>';
					else
						timestr = String.format('%t', st[1][i].expires);

					var tr = tb6.insertRow(-1);
						tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);

					var host = hosts[duid2mac(st[1][i].duid)];
					if (!st[1][i].hostname)
						tr.insertCell(-1).innerHTML =
							(host && (host.name || host.ipv4 || host.ipv6))
								? '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space: nowrap">? (%h)</div>'.format(host.name || host.ipv4 || host.ipv6)
								: '?';
					else
						tr.insertCell(-1).innerHTML =
							(host && host.name && st[1][i].hostname != host.name)
								? '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space: nowrap">%h (%h)</div>'.format(st[1][i].hostname, host.name)
								: st[1][i].hostname;

					tr.insertCell(-1).innerHTML = st[1][i].ip6addr;
					tr.insertCell(-1).innerHTML = st[1][i].duid;
					tr.insertCell(-1).innerHTML = timestr;
				}

				if( tb6.rows.length == 1 )
				{
					var tr = tb6.insertRow(-1);
						tr.className = 'cbi-section-table-row';

					var td = tr.insertCell(-1);
						td.colSpan = 4;
						td.innerHTML = '<em><br /><%:There are no active leases.%></em>';
				}
			}
		}
	);
//]]></script>

<fieldset class="cbi-section">
	<legend><%:Active DHCP Leases%></legend>
	<table class="cbi-section-table" id="lease_status_table">
		<tr class="cbi-section-table-titles">
			<th class="cbi-section-table-cell"><%:Hostname%></th>
			<th class="cbi-section-table-cell"><%:IPv4-Address%></th>
			<th class="cbi-section-table-cell"><%:MAC-Address%></th>
			<th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
		</tr>
		<tr class="cbi-section-table-row">
			<td colspan="4"><em><br /><%:Collecting data...%></em></td>
		</tr>
	</table>
</fieldset>

<fieldset class="cbi-section" style="display:none">
	<legend><%:Active DHCPv6 Leases%></legend>
	<table class="cbi-section-table" id="lease6_status_table">
		<tr class="cbi-section-table-titles">
			<th class="cbi-section-table-cell"><%:Host%></th>
			<th class="cbi-section-table-cell"><%:IPv6-Address%></th>
			<th class="cbi-section-table-cell"><%:DUID%></th>
			<th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
		</tr>
		<tr class="cbi-section-table-row">
			<td colspan="4"><em><br /><%:Collecting data...%></em></td>
		</tr>
	</table>
</fieldset>