<%#
 Copyright 2008 Steven Barth <steven@midlink.org>
 Copyright 2008-2016 Jo-Philipp Wich <jow@openwrt.org>
 Copyright 2012 David Menting <david@nut-bolt.nl>
 Licensed to the public under the Apache License 2.0.
-%>

<%
	local sys  = require "luci.sys"
	local util = require "luci.util"
	local http = require "luci.http"
	local disp = require "luci.dispatcher"

	local boardinfo = util.ubus("system", "board")

	local request  = disp.context.path
	local request2 = disp.context.request

	local category = request[1]
	local cattree  = category and disp.node(category)

	local leaf = request2[#request2]

	local tree = disp.node()
	local node = disp.context.dispatched

	local categories = disp.node_childs(tree)

	local c = tree
	local i, r

	-- tag all nodes leading to this page
	for i, r in ipairs(request) do
		if c.nodes and c.nodes[r] then
			c = c.nodes[r]
			c._menu_selected = true
		end
	end

	-- send as HTML5
	http.prepare_content("text/html")

	local function nodeurl(prefix, name, query)
		local u = url(prefix, name)
		if query then
			u = u .. http.build_querystring(query)
		end
		return pcdata(u)
	end

	local function render_tabmenu(prefix, node, level)
		if not level then
			level = 1
		end

		local childs = disp.node_childs(node)
		if #childs > 0 then
			if level > 2 then
				write('<ul class="tabs">')
			end

			local selected_node
			local selected_name
			local i, v

			for i, v in ipairs(childs) do
				local nnode = node.nodes[v]
				if nnode._menu_selected then
					selected_node = nnode
					selected_name = v
				end

				if level > 2 then
					write('<li class="tabmenu-item-%s %s"><a href="%s">%s</a></li>' %{
						v, (nnode._menu_selected or (node.leaf and v == leaf)) and 'active' or '',
						nodeurl(prefix, v, nnode.query),
						striptags(translate(nnode.title))
					})
				end
			end

			if level > 2 then
				write('</ul>')
			end

			if selected_node then
				render_tabmenu(prefix .. "/" .. selected_name, selected_node, level + 1)
			end
		end
	end

	local function render_submenu(prefix, node)
		local childs = disp.node_childs(node)
		if #childs > 0 then
			write('<ul class="dropdown-menu">')

			for i, r in ipairs(childs) do
				local nnode = node.nodes[r]
				write('<li><a href="%s">%s</a></li>' %{
					nodeurl(prefix, r, nnode.query),
					pcdata(striptags(translate(nnode.title)))
				})
			end

			write('</ul>')
		end
	end

	local function render_topmenu()
		local childs = disp.node_childs(cattree)
		if #childs > 0 then
			write('<ul class="nav">')

			for i, r in ipairs(childs) do
				local nnode = cattree.nodes[r]
				local grandchildren = disp.node_childs(nnode)

				if #grandchildren > 0 then
					write('<li class="dropdown"><a class="menu" href="#">%s</a>' % pcdata(striptags(translate(nnode.title))))
					render_submenu(category .. "/" .. r, nnode)
					write('</li>')
				else
					write('<li><a href="%s">%s</a></li>' %{
						nodeurl(category, r, nnode.query),
						pcdata(striptags(translate(nnode.title)))
					})
				end
			end

			write('</ul>')
		end
	end

	local function render_changes()
		-- calculate the number of unsaved changes
		if tree.nodes[category] and tree.nodes[category].ucidata then
			local ucichanges = 0

			for i, j in pairs(require("luci.model.uci").cursor():changes()) do
				for k, l in pairs(j) do
					for m, n in pairs(l) do
						ucichanges = ucichanges + 1;
					end
				end
			end

			if ucichanges > 0 then
				write('<a class="label notice" href="%s?redir=%s">%s: %d</a>' %{
					url(category, 'uci/changes'),
					http.urlencode(http.formvalue('redir') or REQUEST_URI),
					translate('Unsaved Changes'),
					ucichanges
				})
			end
		end
	end
-%>
<!DOCTYPE html>
<html lang="<%=luci.i18n.context.lang%>">
	<head>
		<meta charset="utf-8">
		<title><%=striptags( (boardinfo.hostname or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %> - LuCI</title>
		<!--[if lt IE 9]><script src="<%=media%>/html5.js"></script><![endif]-->
		<meta name="viewport" content="initial-scale=1.0">
		<link rel="stylesheet" href="<%=media%>/cascade.css">
		<link rel="stylesheet" media="only screen and (max-device-width: 854px)" href="<%=media%>/mobile.css" type="text/css" />
		<link rel="shortcut icon" href="<%=media%>/favicon.ico">
		<% if node and node.css then %>
			<link rel="stylesheet" href="<%=resource%>/<%=node.css%>">
		<% end -%>
		<% if css then %>
			<style title="text/css"><%= css %></style>
		<% end -%>
		<script src="<%=resource%>/xhr.js"></script>
	</head>

	<body class="lang_<%=luci.i18n.context.lang%> <%- if node then %><%= striptags( node.title ) %><%- end %>">
		<header>
			<div class="fill">
				<div class="container">
					<a class="brand" href="#"><%=boardinfo.hostname or "?"%></a>
					<% render_topmenu() %>
					<div class="pull-right">
						<% render_changes() %>
						<span id="xhr_poll_status" style="display:none" onclick="XHR.running() ? XHR.halt() : XHR.run()">
							<span class="label success" id="xhr_poll_status_on"><%:Auto Refresh%> <%:on%></span>
							<span class="label" id="xhr_poll_status_off" style="display:none"><%:Auto Refresh%> <%:off%></span>
						</span>
					</div>
				</div>
			</div>
		</header>

		<%- if luci.sys.process.info("uid") == 0 and luci.sys.user.getuser("root") and not luci.sys.user.getpasswd("root") then -%>
			<div class="container">
				<div class="alert-message warning">
					<h4><%:No password set!%></h4>
					<%:There is no password set on this router. Please configure a root password to protect the web interface and enable SSH.%><br>
					<a href="<%=pcdata(luci.dispatcher.build_url("admin/system/admin"))%>"><%:Go to password configuration...%></a>
				</div>
			</div>
		<%- end -%>

		<div id="maincontent" class="container">
			<% if category then render_tabmenu(category, cattree) end %>
