{{ define "title"}}{{ t "page.settings.title" }}{{ end }}

{{ define "page_header"}}
<section class="page-header" aria-labelledby="page-header-title">
    <h1 id="page-header-title">{{ t "page.settings.title" }}</h1>
    {{ template "settings_menu" dict "user" .user }}
</section>
{{ end }}

{{ define "content"}}
<form method="post" autocomplete="off" action="{{ route "updateSettings" }}">
    <input type="hidden" name="csrf" value="{{ .csrf }}">

    {{ if .errorMessage }}
        <div role="alert" class="alert alert-error">{{ .errorMessage }}</div>
    {{ end }}

    {{ if not disableLocalAuth }}
    <fieldset>
        <legend>{{ t "form.prefs.fieldset.authentication_settings" }}</legend>

        <label for="form-username">{{ t "form.user.label.username" }}</label>
        <input type="text" name="username" id="form-username" value="{{ .form.Username }}" autocomplete="username" required>

        <label for="form-password">{{ t "form.user.label.password" }}</label>
        <input type="password" name="password" id="form-password" value="{{ .form.Password }}" autocomplete="new-password">

        <label for="form-confirmation">{{ t "form.user.label.confirmation" }}</label>
        <input type="password" name="confirmation" id="form-confirmation" value="{{ .form.Confirmation }}" autocomplete="new-password">

        {{ if hasOAuth2Provider "google" }}
        <div class="panel">
            {{ if .user.GoogleID }}
                <a href="{{ route "oauth2Unlink" "provider" "google" }}">{{ t "page.settings.unlink_google_account" }}</a>
            {{ else }}
                <a href="{{ route "oauth2Redirect" "provider" "google" }}">{{ t "page.settings.link_google_account" }}</a>
            {{ end }}
        </div>
        {{ else if hasOAuth2Provider "oidc" }}
        <div class="panel">
            {{ if .user.OpenIDConnectID }}
                <a href="{{ route "oauth2Unlink" "provider" "oidc" }}">{{ t "page.settings.unlink_oidc_account" oidcProviderName }}</a>
            {{ else }}
                <a href="{{ route "oauth2Redirect" "provider" "oidc" }}">{{ t "page.settings.link_oidc_account" oidcProviderName }}</a>
            {{ end }}
        </div>
        {{ end }}

        <div class="buttons">
            <button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
        </div>
    </fieldset>
    {{ end }}

    {{ if .webAuthnEnabled }}
    <fieldset>
        <legend>{{ t "page.settings.webauthn.passkeys" }}</legend>

        <div role="alert" class="alert alert-error hidden" id="webauthn-error">
            {{ t "page.settings.webauthn.register.error" }}
        </div>

        {{ if .webAuthnCerts}}
        <table>
            <tr>
                <th>{{ t "page.settings.webauthn.passkey_name" }}</th>
                <th>{{ t "page.settings.webauthn.added_on" }}</th>
                <th>{{ t "page.settings.webauthn.last_seen_on" }}</th>
                <th>{{ t "page.settings.webauthn.actions" }}</th>
            </tr>
            {{ range .webAuthnCerts }}
            <tr>
                <td>{{ .Name }}</td>
                <td>{{ elapsed $.user.Timezone .AddedOn }}</td>
                <td>{{ elapsed $.user.Timezone .LastSeenOn }}</td>
                <td>
                    <a href="#"
                        data-confirm="true"
                        data-label-question="{{ t "confirm.question" }}"
                        data-label-yes="{{ t "confirm.yes" }}"
                        data-label-no="{{ t "confirm.no" }}"
                        data-label-loading="{{ t "confirm.loading" }}"
                        data-url="{{ route "webauthnDelete" "credentialHandle" .HandleEncoded }}">{{ icon "delete" }}{{ t "action.remove" }}</a>
                    <a href="{{ route "webauthnRename" "credentialHandle" .HandleEncoded }}">{{ icon "edit" }} {{ t "action.edit" }}</a>
                </td>
            </tr>
            {{ end }}
        </table>
        {{ end }}

        <div class="buttons">
            <button class="button button-primary" id="webauthn-register" disabled>
                {{ t "page.settings.webauthn.register" }}
            </button>
            {{ if gt .countWebAuthnCerts 0}}
            <button class="button button-danger" id="webauthn-delete">
                {{ plural "page.settings.webauthn.delete" .countWebAuthnCerts .countWebAuthnCerts }}
            </button>
            {{ end }}
        </div>
    </fieldset>
    {{ end }}

    <fieldset>
        <legend>{{ t "form.prefs.fieldset.reader_settings" }}</legend>

        <label for="form-cjk-reading-speed">{{ t "form.prefs.label.cjk_reading_speed" }}</label>
        <input type="number" name="cjk_reading_speed" id="form-cjk-reading-speed" value="{{ .form.CJKReadingSpeed }}" min="1">

        <label for="form-default-reading-speed">{{ t "form.prefs.label.default_reading_speed" }}</label>
        <input type="number" name="default_reading_speed" id="form-default-reading-speed" value="{{ .form.DefaultReadingSpeed }}" min="1">

        <label for="form-media-playback-rate">{{ t "form.prefs.label.media_playback_rate" }}</label>
        <input type="number" name="media_playback_rate" id="form-media-playback-rate" value="{{ .form.MediaPlaybackRate }}" min="0.25" max="4" step="any" />

        <label><input type="checkbox" name="show_reading_time" value="1" {{ if .form.ShowReadingTime }}checked{{ end }}> {{ t "form.prefs.label.show_reading_time" }}</label>

        <label><input type="radio" name="mark_read_behavior" value="{{ .readBehaviors.NoAutoMarkAsRead }}"
                      {{ if eq .form.MarkReadBehavior .readBehaviors.NoAutoMarkAsRead }}checked{{end}}                          > {{ t "form.prefs.label.mark_read_manually" }}</label>
        <label><input type="radio" name="mark_read_behavior" value="{{ .readBehaviors.MarkAsReadOnView }}"
                      {{ if eq .form.MarkReadBehavior .readBehaviors.MarkAsReadOnView }}checked{{end}}                          > {{ t "form.prefs.label.mark_read_on_view" }}</label>
        <label><input type="radio" name="mark_read_behavior" value="{{ .readBehaviors.MarkAsReadOnViewButWaitForPlayerCompletion }}"
                      {{ if eq .form.MarkReadBehavior .readBehaviors.MarkAsReadOnViewButWaitForPlayerCompletion }}checked{{end}}> {{ t "form.prefs.label.mark_read_on_view_or_media_completion" }}</label>
        <label><input type="radio" name="mark_read_behavior" value="{{ .readBehaviors.MarkAsReadOnlyOnPlayerCompletion }}"
                      {{ if eq .form.MarkReadBehavior .readBehaviors.MarkAsReadOnlyOnPlayerCompletion }}checked{{end}}          > {{ t "form.prefs.label.mark_read_on_media_completion" }}</label>

        <div class="buttons">
            <button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
        </div>
    </fieldset>

    <fieldset>
        <legend>{{ t "form.prefs.fieldset.application_settings" }}</legend>

        <label for="form-language">{{ t "form.prefs.label.language" }}</label>
        <select id="form-language" name="language">
        {{ range $key, $value := .languages }}
            <option value="{{ $key }}" {{ if eq $key $.form.Language }}selected="selected"{{ end }}>{{ $value }}</option>
        {{ end }}
        </select>

        <label for="form-timezone">{{ t "form.prefs.label.timezone" }}</label>
        <select id="form-timezone" name="timezone">
        {{ range $key, $value := .timezones }}
            <option value="{{ $key }}" {{ if eq $key $.form.Timezone }}selected="selected"{{ end }}>{{ $value }}</option>
        {{ end }}
        </select>

        <label for="form-theme">{{ t "form.prefs.label.theme" }}</label>
        <select id="form-theme" name="theme">
        {{ range $key, $value := .themes }}
            <option value="{{ $key }}" {{ if eq $key $.form.Theme }}selected="selected"{{ end }}>{{ $value }}</option>
        {{ end }}
        </select>

        <div class="form-label-row">
            <label for="form-display-mode">{{ t "form.prefs.label.display_mode" }}</label>
            &nbsp;
            <a href="https://developer.mozilla.org/en-US/docs/Web/Manifest/display" target="_blank">
                {{ icon "external-link" }}
            </a>
        </div>
        <select id="form-display-mode" name="display_mode">
            <option value="fullscreen" {{ if eq "fullscreen" $.form.DisplayMode }}selected="selected"{{ end }}>{{ t "form.prefs.select.fullscreen" }}</option>
            <option value="standalone" {{ if eq "standalone" $.form.DisplayMode }}selected="selected"{{ end }}>{{ t "form.prefs.select.standalone" }}</option>
            <option value="minimal-ui" {{ if eq "minimal-ui" $.form.DisplayMode }}selected="selected"{{ end }}>{{ t "form.prefs.select.minimal_ui" }}</option>
            <option value="browser" {{ if eq "browser" $.form.DisplayMode }}selected="selected"{{ end }}>{{ t "form.prefs.select.browser" }}</option>
        </select>

        <label for="form-default-home-page">{{ t "form.prefs.label.default_home_page" }}</label>
        <select id="form-default-home-page" name="default_home_page">
        {{ range $key, $value := .default_home_pages }}
            <option value="{{ $key }}" {{ if eq $key $.form.DefaultHomePage }}selected="selected"{{ end }}>{{ t $value }}</option>
        {{ end }}
        </select>

        <label for="form-entry-direction">{{ t "form.prefs.label.entry_sorting" }}</label>
        <select id="form-entry-direction" name="entry_direction">
            <option value="asc" {{ if eq "asc" $.form.EntryDirection }}selected="selected"{{ end }}>{{ t "form.prefs.select.older_first" }}</option>
            <option value="desc" {{ if eq "desc" $.form.EntryDirection }}selected="selected"{{ end }}>{{ t "form.prefs.select.recent_first" }}</option>
        </select>

        <label for="form-entry-order">{{ t "form.prefs.label.entry_order" }}</label>
        <select id="form-entry-order" name="entry_order">
            <option value="published_at" {{ if eq "published_at" $.form.EntryOrder }}selected="selected"{{ end }}>{{ t "form.prefs.select.publish_time" }}</option>
            <option value="created_at" {{ if eq "created_at" $.form.EntryOrder }}selected="selected"{{ end }}>{{ t "form.prefs.select.created_time" }}</option>
        </select>

        <label for="form-categories-sorting-order">{{ t "form.prefs.label.categories_sorting_order" }}</label>
        <select id="form-categories-sorting-order" name="categories_sorting_order">
        {{ range $key, $value := .categories_sorting_options }}
            <option value="{{ $key }}" {{ if eq $key $.form.CategoriesSortingOrder }}selected="selected"{{ end }}>{{ t $value }}</option>
        {{ end }}
        </select>

        <label for="form-gesture-nav">{{ t "form.prefs.label.gesture_nav" }}</label>
        <select id="form-gesture-nav" name="gesture_nav">
            <option value="none" {{ if eq "none" $.form.GestureNav }}selected="selected"{{ end }}>{{ t "form.prefs.select.none" }}</option>
            <option value="tap" {{ if eq "tap" $.form.GestureNav }}selected="selected"{{ end }}>{{ t "form.prefs.select.tap" }}</option>
            <option value="swipe" {{ if eq "swipe" $.form.GestureNav }}selected="selected"{{ end }}>{{ t "form.prefs.select.swipe" }}</option>
        </select>

        <label for="form-entries-per-page">{{ t "form.prefs.label.entries_per_page" }}</label>
        <input type="number" name="entries_per_page" id="form-entries-per-page" value="{{ .form.EntriesPerPage }}" min="1">

        <label><input type="checkbox" name="keyboard_shortcuts" value="1" {{ if .form.KeyboardShortcuts }}checked{{ end }}> {{ t "form.prefs.label.keyboard_shortcuts" }}</label>

        <label><input type="checkbox" name="entry_swipe" value="1" {{ if .form.EntrySwipe }}checked{{ end }}> {{ t "form.prefs.label.entry_swipe" }}</label>

        <label for="form-custom-css">{{t "form.prefs.label.custom_css" }}</label>
        <textarea id="form-custom-css" name="custom_css" cols="40" rows="10" spellcheck="false">{{ .form.CustomCSS }}</textarea>

        <label for="form-external-font-hosts">{{t "form.prefs.label.external_font_hosts" }}</label>
        <input type="text" id="form-external-font-hosts" name="external_font_hosts" spellcheck="false" value="{{ .form.ExternalFontHosts }}">
        <div class="form-help">{{t "form.prefs.help.external_font_hosts" }}</div>

        <label for="form-custom-js">{{t "form.prefs.label.custom_js" }}</label>
        <textarea id="form-custom-js" name="custom_js" cols="40" rows="10" spellcheck="false">{{ .form.CustomJS }}</textarea>

        <div class="buttons">
            <button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
        </div>
    </fieldset>

    <fieldset>
        <legend>{{ t "form.prefs.fieldset.global_feed_settings" }}</legend>
        <div class="form-label-row">
            <label for="form-blocklist-rules">
                {{ t "form.feed.label.blocklist_rules" }}
            </label>
            &nbsp;
            <a href=" https://miniflux.app/docs/rules.html#global-filtering-rules" target="_blank">
                {{ icon "external-link" }}
            </a>
        </div>
        <textarea id="form-blocklist-rules" name="block_filter_entry_rules" cols="40" rows="10" spellcheck="false">{{ .form.BlockFilterEntryRules }}</textarea>

        <div class="form-label-row">
            <label for="form-keeplist-rules">
                {{ t "form.feed.label.keeplist_rules" }}
            </label>
            <a href=" https://miniflux.app/docs/rules.html#global-filtering-rules" target="_blank">
                {{ icon "external-link" }}
            </a>
        </div>
        <textarea id="form-keeplist-rules" name="keep_filter_entry_rules" cols="40" rows="10" spellcheck="false">{{ .form.KeepFilterEntryRules }}</textarea>

        <div class="buttons">
            <button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
        </div>
    </fieldset>
</form>

{{ end }}
