{{define "webmail_message_pane.html"}}
<div class="pane-toolbar d-flex justify-content-between align-items-center mb-3">
    <div class="btn-group btn-group-sm">
        <button type="button" onclick="openCompose('/webmail/mail/compose?reply={{.uid}}&folder={{.active_folder}}')" class="btn btn-outline-primary" title="Reply"><i class="bi bi-reply"></i></button>
        <button type="button" onclick="openCompose('/webmail/mail/compose?replyall={{.uid}}&folder={{.active_folder}}')" class="btn btn-outline-primary" title="Reply All"><i class="bi bi-reply-all"></i></button>
        <button type="button" onclick="openCompose('/webmail/mail/compose?forward={{.uid}}&folder={{.active_folder}}')" class="btn btn-outline-primary" title="Forward"><i class="bi bi-arrow-right"></i></button>
        <a href="{{.message_url}}" target="_blank" rel="noopener" class="btn btn-outline-primary" title="Open in new tab"><i class="bi bi-box-arrow-up-right"></i></a>
    </div>
    <div class="d-flex align-items-center gap-2">
        <form method="post" action="/webmail/mail/{{.active_folder}}/{{.uid}}/move" class="d-flex align-items-center gap-1">
            <input type="hidden" name="csrf_token" value="{{.csrf_token}}">
            <select name="target_folder" class="form-select form-select-sm" style="width: auto;">
                <option value="">Move to&hellip;</option>
                {{$folder := .active_folder}}
                {{range .folders}}{{if ne . $folder}}<option value="{{.}}">{{.}}</option>{{end}}{{end}}
            </select>
            <button type="submit" class="btn btn-outline-secondary btn-sm" title="Move"><i class="bi bi-folder-symlink"></i></button>
        </form>
        {{if .under_trash}}
        <form method="post" action="/webmail/mail/{{.active_folder}}/{{.uid}}/restore">
            <input type="hidden" name="csrf_token" value="{{.csrf_token}}">
            <button type="submit" class="btn btn-outline-secondary btn-sm" title="Restore"><i class="bi bi-arrow-counterclockwise"></i></button>
        </form>
        {{end}}
        <form method="post" action="/webmail/mail/{{.active_folder}}/{{.uid}}/delete" data-confirm="{{if .under_trash}}Permanently delete this message? This cannot be undone.{{else}}Move this message to Trash?{{end}}">
            <input type="hidden" name="csrf_token" value="{{.csrf_token}}">
            <button type="submit" class="btn btn-outline-danger btn-sm" title="{{if .under_trash}}Delete Permanently{{else}}Move to Trash{{end}}"><i class="bi bi-trash"></i></button>
        </form>
    </div>
</div>

<h5 class="mb-2">{{if .parsed.Header.Subject}}{{.parsed.Header.Subject}}{{else}}<span class="text-muted">(no subject)</span>{{end}}</h5>
{{if or .smime.Signed .smime.Encrypted}}
<div class="mb-2">
    {{if .smime.Encrypted}}
        {{if .smime.Decrypted}}<span class="badge bg-success"><i class="bi bi-unlock-fill me-1"></i>Encrypted &amp; decrypted</span>
        {{else}}<span class="badge bg-danger" title="{{.smime.DecryptErr}}"><i class="bi bi-lock-fill me-1"></i>Encrypted — could not decrypt</span>{{end}}
    {{end}}
    {{if .smime.Signed}}
        {{if .smime.SignatureOK}}<span class="badge bg-success" title="{{.smime.SignerEmail}}"><i class="bi bi-patch-check-fill me-1"></i>Signature verified{{if .smime.SignerEmail}} ({{.smime.SignerEmail}}){{end}}</span>
        {{else}}<span class="badge bg-danger" title="{{.smime.SignatureErr}}"><i class="bi bi-exclamation-triangle-fill me-1"></i>Signature invalid</span>{{end}}
    {{end}}
</div>
{{end}}
{{if .pgp.Encrypted}}
<div class="mb-2">
    {{if .pgp.Decrypted}}<span class="badge bg-success"><i class="bi bi-unlock-fill me-1"></i>PGP encrypted &amp; decrypted</span>
    {{else if .pgp.NeedsUnlock}}<span class="badge bg-warning text-dark"><i class="bi bi-lock-fill me-1"></i>PGP encrypted — enter your passphrase to decrypt</span>
    {{else}}<span class="badge bg-danger" title="{{.pgp.DecryptErr}}"><i class="bi bi-lock-fill me-1"></i>PGP encrypted — could not decrypt</span>{{end}}
</div>
{{if .pgp.NeedsUnlock}}
<form method="post" action="/webmail/pgp/unlock" class="row g-2 align-items-end mb-2">
    <input type="hidden" name="csrf_token" value="{{.csrf_token}}">
    <input type="hidden" name="next" value="{{.message_url}}">
    <div class="col-auto">
        <select class="form-select form-select-sm" name="identity_id">
            {{range .pgp.Identities}}<option value="{{.ID}}">{{if .Label}}{{.Label}}{{else}}Key{{end}} ({{.Fingerprint}})</option>{{end}}
        </select>
    </div>
    <div class="col-auto">
        <input type="password" class="form-control form-control-sm" name="passphrase" placeholder="Passphrase" required>
    </div>
    <div class="col-auto">
        <button type="submit" class="btn btn-warning btn-sm">Unlock &amp; Decrypt</button>
    </div>
</form>
{{end}}
{{end}}
<div class="small text-muted mb-3">
    <div><strong>From:</strong> {{.parsed.Header.From}}</div>
    <div><strong>To:</strong> {{.parsed.Header.To}}</div>
    {{if .parsed.Header.Cc}}<div><strong>Cc:</strong> {{.parsed.Header.Cc}}</div>{{end}}
    <div><strong>Date:</strong> {{.parsed.Header.Date}}</div>
</div>

{{if .images_blocked}}
<div class="alert alert-secondary d-flex justify-content-between align-items-center py-2 mb-3">
    <span><i class="bi bi-shield-lock me-1"></i>Remote images were blocked to protect your privacy.</span>
    <span class="d-flex gap-2">
        <a href="{{.message_url}}?show_images=1" class="btn btn-sm btn-outline-secondary">Show images</a>
        {{if .sender_email}}
        <form method="post" action="/webmail/mail/{{.active_folder}}/{{.uid}}/always-allow-images" class="d-inline">
            <input type="hidden" name="csrf_token" value="{{.csrf_token}}">
            <button type="submit" class="btn btn-sm btn-outline-secondary">Always show images from {{.sender_email}}</button>
        </form>
        {{end}}
    </span>
</div>
{{end}}

{{if .html_body}}
<div class="msg-body-html">{{.html_body}}</div>
{{else if .parsed.TextBody}}
<div class="msg-body-text">{{.parsed.TextBody}}</div>
{{else}}
<p class="text-muted mb-0">(empty message body)</p>
{{end}}

{{if .parsed.Attachments}}
<hr>
<div class="d-flex justify-content-between align-items-center">
    <h6><i class="bi bi-paperclip me-1"></i>Attachments</h6>
    {{if gt (len .parsed.Attachments) 1}}
    <a href="/webmail/mail/{{.active_folder}}/{{.uid}}/attachments.zip" class="btn btn-sm btn-outline-secondary"><i class="bi bi-file-earmark-zip me-1"></i>Download all</a>
    {{end}}
</div>
<div class="list-group">
    {{$folder := .active_folder}}
    {{$uid := .uid}}
    {{range $i, $att := .parsed.Attachments}}
    <a href="/webmail/mail/{{$folder}}/{{$uid}}/attachment/{{$i}}" class="list-group-item list-group-item-action bg-transparent text-white d-flex justify-content-between align-items-center">
        <span><i class="bi bi-file-earmark me-2"></i>{{$att.Filename}}</span>
        <i class="bi bi-download"></i>
    </a>
    {{end}}
</div>
{{end}}
{{end}}
