<%#
Opennet Firmware

Copyright 2010 Rene Ejury <opennet@absorb.it>
Copyright 2017 Martin Garbe <monomartin@on-i.de>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

-%>
<%+header%>
<h2><a id="content" name="content"><%:Opennet ID%></a></h2>
<%+cbi/apply_xhr%>

<%
local new_opennet_id = luci.http.formvalue("new_opennet_id")
local show_invalid_id_warning = false


if (new_opennet_id) then
	function split_numbers(str)
		local t = { }
		str:gsub("%d+", function (w) table.insert(t, tonumber(w)) end)
		return t
	end
	local numbers = split_numbers(new_opennet_id)

	local major
	local minor
	local replace_id = true
	if #numbers == 1 then
		-- nur eine Zahl: Netzgruppe "1"
		major = 1
		minor = numbers[1]
	elseif #numbers == 2 then
		-- zwei Zahlen: beide verwenden
		major = numbers[1]
		minor = numbers[2]
	elseif #numbers == 4 then
		-- vier Zahlen (192.168.x.y): die letzten beiden verwenden
		major = numbers[3]
		minor = numbers[4]
	else
		replace_id = false
	end

	if replace_id then
		if (0 <= major) and (major <= 255) and (1 <= minor) and (minor <= 255) then
			local id_string = major .. "." .. minor
			on_function("set_opennet_id", {id_string})
			luci.sys.call("sleep 3")
		else
			show_invalid_id_warning = true
		end
	end
end
%>

<h3><%:Opennet-ID / Network ID%></h3>
	<div class="cbi-map">
		<% if show_invalid_id_warning then %>
			<fieldset class="cbi-section"><div class="cbi-section-comment"><div class="alert-message warning">
				<p><%:The specified opennet ID is invalid. Please try again (input format: 'X.YYY').%></p>
			</div></div></fieldset>
		<% end %>
		<fieldset class="cbi-section">
			<form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data">
				<p><%=luci.i18n.translatef('Great that you decided to take part in Opennet. Register the IP of your Access-Point in the %sOpennet-Wiki%s and acquire a free IP-Address. Please read the information %sin the wiki%s for further details.', '<a href="https://wiki.opennet-initiative.de/wiki/Opennet_Nodes">', '</a>', '<a href="https://wiki.opennet-initiative.de/wiki/Mitmachen">', '</a>')%></p>
			<p><%:Please enter your Opennet-ID (1.X, 2.X or 3.X etc.) or the IP-Address of your first Opennet-Network-Interface, any change will reconfigure your network settings immediately.%></p>
			<fieldset class="cbi-section-node">
				<div class="cbi-value">
					<label class="cbi-value-title">
						<input type="value" name="new_opennet_id" value="<%= opennet_id %>" />
					</label>
					<div class="cbi-value-field">
						<input type="submit" class="cbi-button cbi-button-apply" value="<%:use%>" />
					</div>
				</div>
			</fieldset>
			</form>
		</fieldset>
	</div>

<h3><%:Mesh Interface Addresses%></h3>
	<div class="cbi-map">
		<fieldset class="cbi-section">
			<fieldset class="cbi-section-node">
				<table class="cbi-section-table">
					<% for net_name, net in pairs(mesh_interfaces) do %>
					<tr id="cbi-network-lan" class="cbi-section-table-row">
						<th><h3><%=net_name%> (<%:device%> <%=net.ifname%>)</h3></th>
						<td class="cbi-value-field"><div id="cbi-network-lan-ipaddr"><%=net.ipaddr%> / <%=net.netmask%></div></td>
					</tr>
				<% end %>
				</table>
			</fieldset>
		</fieldset>
	</div>

<%+footer%>
