<%
	local fw = require "luci.model.firewall".init()
	local nw = require "luci.model.network".init()
	local zones = fw:get_zones()

	local keys, vals, a, k, v = {}, {}
	for k, v in ipairs(nw:get_interfaces()) do
		for k, a in ipairs(v:ipaddrs()) do
			keys[#keys+1] = a:host():string()
			vals[#vals+1] = '%s (%s)' %{ a:host(), v:shortname() }
		end
	end
%>

<% if #zones > 1 then %>
	<h4><%:New source NAT%></h4>
	<div class="table">
		<div class="tr cbi-section-table-titles">
			<div class="th"><%:Name%></div>
			<div class="th"><%:Source zone%></div>
			<div class="th"><%:Destination zone%></div>
			<div class="th"><%:To source IP%></div>
			<div class="th"><%:To source port%></div>
			<div class="th"></div>
		</div>
		<div class="tr">
			<div class="td">
				<input type="text" class="cbi-input-text" id="_newsnat.name" name="_newsnat.name" placeholder="<%:New SNAT rule%>" />
			</div>
			<div class="td">
				<select class="cbi-input-text" id="_newsnat.src" name="_newsnat.src">
					<% local k, v; for k, v in ipairs(fw:get_zones()) do -%>
						<option<%=ifattr(v:name() == "lan", "selected", "selected")%> value="<%=v:name()%>"><%=v:name()%></option>
					<%- end %>
				</select>
			</div>
			<div class="td">
				<select class="cbi-input-text" id="_newsnat.dest" name="_newsnat.dest">
					<% local k, v; for k, v in ipairs(fw:get_zones()) do -%>
						<option<%=ifattr(v:name() == "wan", "selected", "selected")%> value="<%=v:name()%>"><%=v:name()%></option>
					<%- end %>
				</select>
			</div>
			<div class="td">
				<input type="text" class="cbi-input-text" id="_newsnat.dip" name="_newsnat.dip" placeholder="<%:Do not rewrite%>" data-type="ip4addr" data-optional="true"<%=
					ifattr(#keys > 0, "data-choices", { keys, vals })
				%> />
			</div>
			<div class="td">
				<input type="text" class="cbi-input-text" id="_newsnat.dport" name="_newsnat.dport" placeholder="<%:Do not rewrite%>" data-type="portrange" data-optional="true" />
			</div>
			<div class="td bottom">
				<input type="submit" class="cbi-button cbi-button-link" name="_newsnat.submit" value="<%:Add and edit...%>" />
			</div>
		</div>
	</div>
<% else %>
	<input type="submit" class="cbi-button cbi-button-add" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" value="<%:Add%>" />
<% end %>
