{{/* Wide enough for credentials and DMARC settings side by side (see .split
     in panel.css, as on a delivery's page). */}}
{{define "wide"}}wide{{end}}

{{/* The username/password fields are identical for a global administrator
     (split card, DMARC alongside) and a domain administrator (narrow card,
     no DMARC card) — only the surrounding form and card differ. */}}
{{define "credentials_fields"}}
<label for="username">Username</label>
<input id="username" name="username" autocomplete="username"
       autocapitalize="none" spellcheck="false" value="{{.FormUsername}}" required>

<label for="current_password">Current password</label>
<input id="current_password" name="current_password" type="password"
       autocomplete="current-password" required>

<label for="new_password">New password</label>
<input id="new_password" name="new_password" type="password" autocomplete="new-password">

<label for="new_password_confirm">Confirm new password</label>
<input id="new_password_confirm" name="new_password_confirm" type="password" autocomplete="new-password">
{{end}}

{{define "content"}}
<h1>Settings</h1>

{{if .Flash}}<div class="flash">{{.Flash}}</div>{{end}}
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}

{{if .ShowDMARC}}
<form method="post" action="/settings">
<div class="split">
  <div class="card">
    <h2>Panel credentials</h2>
    <p class="muted">These are the credentials for this control panel only.
    Applications keep their own logins and passwords, which are not affected.</p>

    {{template "credentials_fields" .}}
  </div>

  <div class="card">
    <h2>DMARC aggregate reports</h2>
    <p class="muted">Optional default <code>rua=</code> address for every sending
    domain (can be overridden per domain).{{if .DMARCIngestEnabled}} When set to
    an address on <code>{{.HostedReportAddress}}</code>, SelfPost receives and
    parses aggregate reports on port&nbsp;25.{{else}} Use a mailbox on a domain
    that receives inbound mail elsewhere.{{end}}</p>

    <label for="dmarc_report_email">Default report address</label>
    <input id="dmarc_report_email" name="dmarc_report_email" type="email"
           autocomplete="email" autocapitalize="none" spellcheck="false"
           value="{{.FormDMARCEmail}}" placeholder="{{if .DMARCIngestEnabled}}{{.HostedReportAddress}}{{else}}reports@your-mail-domain.com{{end}}">

    {{if .FormDMARCEmail}}
    <p class="muted">When <code>rua=</code> points at another domain, that hub
    domain must publish a report-authorisation record so receivers will deliver
    the XML aggregates.</p>

    <label>Report authorization — host / name</label>
    <div class="code-row">
      <span class="code">{{.ReportAuthName}}</span>
      <button type="button" class="copy">Copy</button>
    </div>

    <label>Report authorization — value</label>
    <div class="code-row">
      <span class="code">{{.ReportAuthExample}}</span>
      <button type="button" class="copy">Copy</button>
    </div>

    {{if .ReportAuthDNS.Status}}
    <label>Report authorization DNS <span class="st st-{{.ReportAuthDNS.Status}}">{{.ReportAuthDNS.Status}}</span></label>
    <p class="{{if eq .ReportAuthDNS.Status "ok"}}muted{{else}}error{{end}}">{{.ReportAuthDNS.Detail}}</p>
    {{if .ReportAuthDNS.Records}}<span class="code">{{range .ReportAuthDNS.Records}}{{.}}
{{end}}</span>{{end}}
    {{end}}
    {{end}}
  </div>
</div>

<div class="card" id="deliveries-retention">
  <h2>Deliveries</h2>
  <p class="muted">How long rows on <a href="/deliveries">Deliveries</a> are
  kept before the background sweep deletes them — the main driver of
  <code>/data</code> growth over time. Does not affect <code>mail.log</code>
  rotation (fourteen daily files).</p>

  <label for="send_log_retention_days">Send log retention (days)</label>
  <input id="send_log_retention_days" name="send_log_retention_days" type="number"
         min="7" max="365" step="1" required
         value="{{.FormSendLogRetentionDays}}">
</div>

<button type="submit">Save changes</button>
<p class="muted">Leave both new-password fields empty to change the username,
DMARC address, or send log retention only. Changing the password signs out
every other session; this one stays signed in.</p>
</form>
{{else}}
<div class="card narrow">
  <h2>Panel credentials</h2>
  <p class="muted">These are the credentials for this control panel only.
  Applications keep their own logins and passwords, which are not affected.</p>
  <form method="post" action="/settings">
    {{template "credentials_fields" .}}

    <button type="submit">Save changes</button>
  </form>
  <p class="muted">Leave both new-password fields empty to change the username
  only. Changing the password signs out every other session;
  this one stays signed in.</p>
</div>
{{end}}

<div class="card" id="rate-limits">
  <h2>Sending rate limits</h2>
  <p class="muted">Configured in <code>.env</code> / Compose; restart the
  container to change level&nbsp;1. Domain and application ceilings are set on
  each domain's page.</p>

  <label>Level 1 — per client IP (Postfix)</label>
  <p class="code-row"><span class="code">{{.L1Messages}} messages / {{.L1Window}} seconds</span></p>
  <p class="muted"><code>RATE_LIMIT_MESSAGES_PER_IP</code> /
  <code>RATE_LIMIT_WINDOW_SECONDS</code>. Hard ceiling for every connecting IP;
  the panel cannot raise a domain or application limit above this.</p>

  <label>Level 2 — domain</label>
  <p class="muted">Optional ceiling for <em>all</em> senders on a domain. When
  unset, only level&nbsp;1 applies. Must be ≤ level&nbsp;1.</p>

  <label>Level 2 — application</label>
  <p class="muted">Overrides the domain limit for one application. The ceiling
  may be <em>above or below</em> the domain setting (≤ level&nbsp;1). When unset,
  the domain limit or level&nbsp;1 applies. Independent of the client IP
  allow-list.</p>
</div>
{{end}}
