<!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Restray Web Editor</title>
		<style>
			:root { color-scheme: light dark; }
			body { margin: 0; font-family: system-ui, sans-serif; font-size: 13px; line-height: 1.5; }
			input[type=text], select, textarea { width: 100%; box-sizing: border-box; }
			textarea { min-height: 6rem; }
			main label, main small { display: block; }
			fieldset { background: light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.05)); }
			fieldset > fieldset { margin-block: 1rem; }

			.layout { display: flex; min-height: 100vh; }
			nav { flex: 0 0 10rem; padding: 1rem; border-inline-end: 2px groove threedface; background: light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.05)); display: flex; flex-direction: column; gap: .5rem; }
			nav h1, nav h2 { font-size: 1rem; margin: 0; }
			nav ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
			nav label { display: block; padding-inline: .5rem; cursor: pointer; background: light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.05)); border: 1px solid light-dark(rgba(0, 0, 0, 0.25), rgba(255, 255, 255, 0.25)); }
			nav button { width: 100%; }
			main { flex: 1; min-width: 0; padding: 1rem; }

			input.tab { display: none; }
			.pane { display: none; flex-direction: column; gap: 1rem; }

			#tab-gui:checked ~ .layout #pane-gui { display: flex; }
			#tab-gui:checked ~ .layout label[for=tab-gui] { background: light-dark(rgba(0, 0, 0, 0.25), rgba(255, 255, 255, 0.25)); }
			{{range $i, $p := .Profiles}}
			#tab-profile-{{$i}}:checked ~ .layout #pane-profile-{{$i}} { display: flex; }
			#tab-profile-{{$i}}:checked ~ .layout label[for=tab-profile-{{$i}}] { background: light-dark(rgba(0, 0, 0, 0.25), rgba(255, 255, 255, 0.25)); }
			{{end}}
		</style>
	</head>
	<body>
		{{$isGUITab := eq .ActiveTab "gui"}}
		<input
			type="radio"
			name="tab"
			id="tab-gui"
			class="tab"
			{{if
			$isGUITab}}checked{{end}}
		/>
		{{range $i, $p := .Profiles}}{{$isThisTab := eq $.ActiveTab (printf
		"profile-%d" $i)}}<input
			type="radio"
			name="tab"
			id="tab-profile-{{$i}}"
			class="tab"
			{{if
			$isThisTab}}checked{{end}}
		/>
		{{end}}

		<div class="layout">
			<nav>
				<h1>Restray Web Editor</h1>
				{{if not .Writable}}
				<p>Your config file is read-only, so editing is disabled.</p>
				{{end}}
				<ul>
					<li><label for="tab-gui">GUI</label></li>
				</ul>
				<h2>Profiles</h2>
				<ul>
					{{range $i, $p := .Profiles}}
					<li><label for="tab-profile-{{$i}}">{{$p.DisplayName}}</label></li>
					{{end}}
				</ul>
				{{if .Writable}}
				<form method="post" action="/profile/add">
					<button type="submit">Add Profile</button>
				</form>
				{{end}}
			</nav>

			<main>
				<div class="pane" id="pane-gui">
					<form method="post" action="/save/gui">
						<fieldset {{if not .Writable}}disabled{{end}}>
							<p>
								<label
									><input
										type="checkbox"
										name="check_updates"
										{{if
										.GUI.UpdatesEnabled}}checked{{end}}
									/>
									Check Updates</label
								>
								<small
									>MacOS/Windows only. Check for new Restray versions on startup
									and periodically.</small
								>
							</p>

							<p>
								<label
									><input
										type="checkbox"
										name="manage_backend"
										{{if
										.GUI.BackendManagementEnabled}}checked{{end}}
									/>
									Manage Backend</label
								>
								<small
									>Windows/Linux only. If the selected backend is not found in
									PATH, automatically self-manages it's binary from
									GitHub.</small
								>
							</p>

							<p>
								<label for="notifications">Notifications</label>
								<small
									>"all" (success + errors), "errors" (errors only), or "none"
									(silent)</small
								>
								<select name="notifications" id="notifications">
									{{$val := .GUI.Notifications}} {{range .NotificationOptions}}
									<option value="{{.}}" {{if eq . $val}}selected{{end}}>
										{{.}}
									</option>
									{{end}}
								</select>
							</p>

							<p>
								<label for="icon">Icon</label>
								<small
									>"color", "mono", "white", or "black". Default is "mono" on
									MacOS/Windows and "color" elsewhere. "mono" follows OS
									light/dark mode.</small
								>
								<select name="icon" id="icon">
									{{$val := .GUI.Icon}} {{range .IconOptions}}
									<option value="{{.}}" {{if eq . $val}}selected{{end}}>
										{{if eq . ""}}(default){{else}}{{.}}{{end}}
									</option>
									{{end}}
								</select>
							</p>

							<p>
								<label for="schedule_display">Schedule Display</label>
								<small
									>"description" (human-readable, e.g. "every 6 hours"), "cron"
									(raw expression), "last" (time since last backup), or
									"none"</small
								>
								<select name="schedule_display" id="schedule_display">
									{{$val := .GUI.ScheduleDisplay}} {{range
									.ScheduleDisplayOptions}}
									<option value="{{.}}" {{if eq . $val}}selected{{end}}>
										{{.}}
									</option>
									{{end}}
								</select>
							</p>

							<p>
								<label for="terminal">Terminal</label>
								<small
									>MacOS/Linux only. Terminal emulator used in "Shell" and "View
									Log".</small
								>
								<input
									type="text"
									name="terminal"
									id="terminal"
									value="{{.GUI.Terminal}}"
								/>
							</p>

							<button type="submit">Save GUI</button>
						</fieldset>
					</form>
				</div>

				{{range $i, $p := .Profiles}}
				<div class="pane" id="pane-profile-{{$i}}">
					<form method="post" action="/save/profile">
						<fieldset {{if not $.Writable}}disabled{{end}}>
							<legend>Profile</legend>
							<input type="hidden" name="index" value="{{$i}}" />

							<p>
								<label for="name-{{$i}}">Name</label>
								<small>Name for the profile</small>
								<input
									type="text"
									name="name"
									id="name-{{$i}}"
									value="{{$p.Name}}"
								/>
							</p>
							<p>
								<label for="backend-{{$i}}">Backend</label>
								<small
									>Backup program used by this profile. Do not change unless you
									know what this does.</small
								>
								<select name="backend" id="backend-{{$i}}">
									{{if eq $p.Backend "restic"}}
									<option value="restic" selected>restic</option>
									{{else}}
									<option value="restic">restic</option>
									{{end}} {{if eq $p.Backend "rustic"}}
									<option value="rustic" selected>rustic (experimental)</option>
									{{else}}
									<option value="rustic">rustic (experimental)</option>
									{{end}}
								</select>
							</p>

							<p>
								<label for="env_file-{{$i}}">Env File</label>
								<small
									>Path to the environment file with RESTIC_*, RUSTIC_*,
									etc.</small
								>
								<input
									type="text"
									name="env_file"
									id="env_file-{{$i}}"
									value="{{$p.EnvFile}}"
								/>
							</p>

							<p>
								<label for="rclone_config_file-{{$i}}"
									>Rclone Config File</label
								>
								<small
									>Path to an rclone config file, set as RCLONE_CONFIG. Edit its
									contents with your regular editor/rclone; this only stores the
									path.</small
								>
								<input
									type="text"
									name="rclone_config_file"
									id="rclone_config_file-{{$i}}"
									value="{{$p.RcloneConfigFile}}"
								/>
							</p>

							<p>
								<label for="retry_lock-{{$i}}">Retry Lock</label>
								<small
									>How long to retry acquiring a repository lock (e.g. "5m",
									"30s"). Set to "0" to disable.</small
								>
								<input
									type="text"
									name="retry_lock"
									id="retry_lock-{{$i}}"
									value="{{$p.RetryLock}}"
								/>
							</p>

							<p>
								<label for="pre_hook-{{$i}}">Pre-Hook</label>
								<small>Shell command to run before a scheduled run.</small>
								<input
									type="text"
									name="pre_hook"
									id="pre_hook-{{$i}}"
									value="{{$p.PreHook}}"
								/>
							</p>

							<p>
								<label for="post_hook-{{$i}}">Post-Hook</label>
								<small>Shell command to run after a scheduled run.</small>
								<input
									type="text"
									name="post_hook"
									id="post_hook-{{$i}}"
									value="{{$p.PostHook}}"
								/>
							</p>

							<fieldset>
								<legend>Schedule</legend>
								<p>
									<label for="cron-{{$i}}">Cron</label>
									<small
										>Empty to disable schedule. Cron expression (see
										crontab.guru).</small
									>
									<input
										type="text"
										name="cron"
										id="cron-{{$i}}"
										value="{{$p.Schedule.Cron}}"
									/>
								</p>
								<p>
									<label
										><input
											type="checkbox"
											name="on_battery"
											{{if
											$p.Schedule.OnBattery}}checked{{end}}
										/>
										On Battery</label
									>
									<label
										><input
											type="checkbox"
											name="sched_backup"
											{{if
											$p.Schedule.BackupEnabled}}checked{{end}}
										/>
										Backup</label
									>
									<label
										><input
											type="checkbox"
											name="sched_prune"
											{{if
											$p.Schedule.Prune}}checked{{end}}
										/>
										Prune</label
									>
									<label
										><input
											type="checkbox"
											name="sched_check"
											{{if
											$p.Schedule.Check}}checked{{end}}
										/>
										Check</label
									>
								</p>
							</fieldset>

							<fieldset>
								<legend>Backup</legend>
								<p>
									<label for="backup_paths-{{$i}}">Paths</label>
									<small>Paths to back up, one per line.</small>
									<textarea name="backup_paths" id="backup_paths-{{$i}}">
{{joinLines $p.Backup.Paths}}</textarea>
								</p>
								<p>
									<label for="backup_args-{{$i}}">Args</label>
									<small>Extra arguments passed to `backup`.</small>
									<textarea name="backup_args" id="backup_args-{{$i}}">
{{joinLines $p.Backup.Args}}</textarea>
								</p>
								<p>
									<label for="backup_args_scheduled-{{$i}}"
										>Scheduled Args</label
									>
									<small
										>Extra arguments appended (not replaced) to `backup` when
										running from schedule.</small
									>
									<textarea
										name="backup_args_scheduled"
										id="backup_args_scheduled-{{$i}}"
									>
{{joinLines $p.Backup.ArgsScheduled}}</textarea>
								</p>
							</fieldset>

							<fieldset>
								<legend>Prune</legend>
								<p>
									<label for="prune_args-{{$i}}">Args</label>
									<small>Extra arguments passed to `forget --prune`.</small>
									<textarea name="prune_args" id="prune_args-{{$i}}">
{{joinLines $p.Prune.Args}}</textarea>
								</p>
							</fieldset>

							<fieldset>
								<legend>Check</legend>
								<p>
									<label for="check_args-{{$i}}">Args</label>
									<small>Extra arguments passed to `check`.</small>
									<textarea name="check_args" id="check_args-{{$i}}">
{{joinLines $p.Check.Args}}</textarea>
								</p>
							</fieldset>

							<fieldset>
								<legend>Mount</legend>
								<p>
									<label for="mount_args-{{$i}}">Args</label>
									<small>Extra arguments passed to `mount`.</small>
									<textarea name="mount_args" id="mount_args-{{$i}}">
{{joinLines $p.Mount.Args}}</textarea>
								</p>
							</fieldset>

							<button type="submit">Save Profile</button>
							<button
								type="submit"
								formaction="/profile/delete"
								onclick="return confirm('Delete profile {{$p.DisplayName}}?');"
							>
								Delete Profile
							</button>
						</fieldset>
					</form>

					<form method="post" action="/profile/env">
						<fieldset {{if not $.Writable}}disabled{{end}}>
							<legend>Env File</legend>
							<input type="hidden" name="index" value="{{$i}}" />
							<textarea name="env_contents" rows="10">
{{$p.EnvContents}}</textarea>
							<button type="submit">Save Env File</button>
						</fieldset>
					</form>
				</div>
				{{end}}
			</main>
		</div>
	</body>
</html>
