<%#
 Copyright 2008 Steven Barth <steven@midlink.org>
 Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
 Licensed to the public under the Apache License 2.0.
-%>

<%
local i = 1
require("luci.model.uci")
local uci = luci.model.uci.cursor_state()

uci:foreach("olsrd", "olsrd", function(s)
        if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw  = true end
end)


if luci.http.formvalue("status") == "1" then
	local rv = {}
	for k, gw in ipairs(gws) do
		gw.tcPathCost = tonumber(gw.tcPathCost)/1024 or 0
                if gw.tcPathCost == 4096 then
                        gw.tcPathCost = 0
                end

		rv[#rv+1] = {
			proto = gw.proto,
			ipAddress = gw.ipAddress,
			status = gw.ipv4Status or gw.ipv6Status,
			tcPathCost = string.format("%.3f", gw.tcPathCost),
			hopCount = gw.hopCount,
			uplinkSpeed = gw.uplinkSpeed,
			downlinkSpeed = gw.downlinkSpeed,
			v4 = gw.ipv4 and luci.i18n.translate('yes') or luci.i18n.translate('no'),
			v6 = gw.ipv6 and luci.i18n.translate('yes') or luci.i18n.translate('no'),
			externalPrefix = gw.externalPrefix
			}
	end
	luci.http.prepare_content("application/json")
	luci.http.write_json(rv)
	return
end
%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">//<![CDATA[
XHR.poll(10, '<%=REQUEST_URI%>', { status: 1 },
	function(x, info)
	{
	var smartgwdiv = document.getElementById('olsrd_smartgw');
	if (smartgwdiv)
	{
		var s = '';
		for (var idx = 0; idx < info.length; idx++)
		{
			var smartgw = info[idx];
			s += '<tr class="cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+' proto-' + smartgw.proto + '">'
			if (smartgw.proto == '6') {
				linkgw = '<a href="http://[' + smartgw.ipAddress + ']/cgi-bin-status.html">' + smartgw.ipAddress + '</a>'
			} else {
				linkgw = '<a href="http://' + smartgw.ipAddress + '/cgi-bin-status.html">' + smartgw.ipAddress + '</a>'
			}

			s += String.format(
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>' +
                                '<td class="cbi-section-table-cell">%s</td>',
				linkgw, smartgw.status, smartgw.tcPathCost, smartgw.hopCount, smartgw.uplinkSpeed, smartgw.downlinkSpeed, smartgw.v4, smartgw.v6, smartgw.externalPrefix
                                )
			s += '</tr>'
		}
		smartgwdiv.innerHTML = s;
	}
}
);
//]]></script>


<%+header%>

<h2 name="content"><%:SmartGW announcements%></h2>

<div id="togglebuttons"></div>

<% if has_smartgw then %>

	<fieldset class="cbi-section">
		<legend><%:Overview of smart gateways in this network%></legend>
		<table class="cbi-section-table">
			<thead>
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell"><%:Gateway%></th>
				<th class="cbi-section-table-cell"><%:Status%></th>
				<th class="cbi-section-table-cell"><%:ETX%></th>
				<th class="cbi-section-table-cell"><%:Hops%></th>
				<th class="cbi-section-table-cell"><%:Uplink%></th>
				<th class="cbi-section-table-cell"><%:Downlink%></th>
				<th class="cbi-section-table-cell"><%:IPv4%></th>
				<th class="cbi-section-table-cell"><%:IPv6%></th>
				<th class="cbi-section-table-cell"><%:Prefix%></th>

			</tr>
			</thead>

			<tbody id="olsrd_smartgw">
			<% for k, gw in ipairs(gws) do 

			gw.tcPathCost = tonumber(gw.tcPathCost)/1024 or 0
               		if gw.tcPathCost == 4096 then
	                        gw.tcPathCost = 0
               		end
			%>

			<tr class="cbi-section-table-row cbi-rowstyle-<%=i%> proto-<%=proto%>">
				<% if gw.proto == '6' then %>
					<td class="cbi-section-table-cell"><a href="http://[<%=gw.ipAddress%>]/cgi-bin-status.html"><%=gw.ipAddress%></a></td>
				<% else %>
					<td class="cbi-section-table-cell"><a href="http://<%=gw.ipAddress%>/cgi-bin-status.html"><%=gw.ipAddress%></a></td>
				<% end %>

				<td class="cbi-section-table-cell"><%=gw.ipv4Status or gw.ipv6Status or '-' %></td>
				<td class="cbi-section-table-cell"><%=string.format("%.3f", gw.tcPathCost)%></td>
				<td class="cbi-section-table-cell"><%=gw.hopCount%></td>
				<td class="cbi-section-table-cell"><%=gw.uplinkSpeed%></td>
				<td class="cbi-section-table-cell"><%=gw.downlinkSpeed%></td>
				<td class="cbi-section-table-cell"><%=gw.ipv4 and luci.i18n.translate('yes') or luci.i18n.translate('no')%></td>
				<td class="cbi-section-table-cell"><%=gw.ipv6 and luci.i18n.translate('yes') or luci.i18n.translate('no')%></td>
				<td class="cbi-section-table-cell"><%=gw.externalPrefix%></td>
			</tr>

			<% i = ((i % 2) + 1)
			end %>
			</tbody>
		</table>
	</fieldset>

<% else %>

	<%:SmartGateway is not configured on this system.%>

<% end %>

<%+status-olsr/common_js%>
<%+footer%>
