{{- /*gotype: allowance_planner.ViewModel*/}}
<html lang="en">
<head>
	<title>Allowance Planner 2000</title>
	<style>
		tr:hover {
			background-color: #f0f0f0;
		}
	</style>
</head>
<body>
<h1>Allowance Planner 2000</h1>

{{if ne .Error ""}}
	<h2>Error</h2>
	<p>{{.Error}}</p>
{{else}}
	<h2>Users</h2>
	{{range .Users}}
		{{if eq $.CurrentUser .ID}}
			<strong>{{.Name}}</strong>
		{{else}}
			<a href="/login?user={{.ID}}">{{.Name}}</a>
		{{end}}
	{{end}}

	{{if ne .CurrentUser 0}}
		<h2>Allowances</h2>
		<form action="/createAllowance" method="post">
			<table border="1">
				<thead>
				<tr>
					<th>Name</th>
					<th>Progress</th>
					<th>Target</th>
					<th>Weight</th>
					<th>Actions</th>
				</tr>
				</thead>
				<tbody>
					<tr>
						<td><label><input type="text" name="name" placeholder="Name"></label></td>
						<td></td>
						<td><label><input type="number" name="target" placeholder="Target"></label></td>
						<td><label><input type="number" name="weight" placeholder="Weight"></label></td>
						<td><button>Create</button></td>
					</tr>
				{{range .Allowances}}
					{{if eq .ID 0}}
						<tr>
							<td>Total</td>
							<td>{{.Progress}}</td>
							<td></td>
							<td>{{.Weight}}</td>
						</tr>
					{{else}}
						<tr>
							<td>{{.Name}}</td>
							<td><progress max="{{.Target}}" value="{{.Progress}}"></progress> ({{.Progress}})</td>
							<td>{{.Target}}</td>
							<td>{{.Weight}}</td>
                            {{if ge .Progress .Target}}
								<td>
									<a href="/completeAllowance?allowance={{.ID}}">Mark as completed</a>
								</td>
                            {{end}}
						</tr>
					{{end}}
				{{end}}
				</tbody>
			</table>
		</form>

		<h2>Tasks</h2>
		<form method="post" action="/createTask">
			<table border="1">
				<thead>
				<tr>
					<th>Name</th>
					<th>Assigned</th>
					<th>Reward</th>
					<th>Actions</th>
				</tr>
				</thead>
				<tbody>
				{{range .Tasks}}
					<tr>
						<td>{{.Name}}</td>
						<td>
							{{if eq .Assigned nil}}
								None
							{{else}}
								{{.Assigned}}
							{{end}}
						</td>
						<td>{{.Reward}}</td>
						<td>
							<a href="/completeTask?task={{.ID}}">Mark as completed</a>
						</td>
					</tr>
				{{end}}
						<tr>
							<td><label><input type="text" name="name" placeholder="Name"></label></td>
							<td></td>
							<td><label><input type="number" name="reward" placeholder="Reward"></label></td>
							<td><button>Create</button></td>
						</tr>
				</tbody>
			</table>
		</form>

		<h2>History</h2>
		<table border="1">
			<thead>
			<tr>
				<th>Timestamp</th>
				<th>Allowance</th>
			</tr>
			</thead>
			<tbody>
			{{range .History}}
				<tr>
					<td>{{.Timestamp}}</td>
					<td>{{.Allowance}}</td>
				</tr>
			{{end}}
			</tbody>
		</table>
	{{end}}
{{end}}
</body>
</html>