{{define "webmail_signatures.html"}}
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Settings - Webmail</title>
    <link href="/webmail/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/webmail/static/vendor/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    <link href="/webmail/static/vendor/quill/quill.snow.css" rel="stylesheet">
    <style>
        body { background-color: #1a1a1a; color: #e0e0e0; }
        .table-dark { --bs-table-bg: #2d2d2d; --bs-table-border-color: #404040; }
    </style>
    {{template "webmail_settings_style" .}}
</head>
<body>
    {{template "csrf_script" .}}
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <span class="navbar-brand mb-0 h1"><i class="bi bi-inbox-fill me-2"></i>Webmail <small class="text-muted">{{.mailbox.Email}}</small></span>
            <div class="navbar-nav flex-row gap-2 ms-auto">
                <a href="/webmail/mail/INBOX" class="btn btn-outline-light btn-sm"><i class="bi bi-envelope me-1"></i>Mail</a>
                <button type="button" onclick="openCompose('/webmail/mail/compose')" class="btn btn-outline-light btn-sm"><i class="bi bi-pencil-square me-1"></i>Compose</button>
                <form method="post" action="/webmail/logout" class="d-inline">
                    <button type="submit" class="btn btn-outline-light btn-sm"><i class="bi bi-box-arrow-right me-1"></i>Sign out</button>
                </form>
            </div>
        </div>
    </nav>

    <div class="toast-container position-fixed top-0 end-0 p-3" style="z-index: 1090;">
        {{range .flashes}}
        <div class="toast align-items-center text-bg-{{if eq .Category "error"}}danger{{else}}{{.Category}}{{end}} border-0" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="d-flex">
                <div class="toast-body">
                    <i class="bi bi-{{if eq .Category "error"}}exclamation-triangle{{else if eq .Category "success"}}check-circle{{else}}info-circle{{end}} me-2"></i>
                    {{.Message}}
                </div>
                <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>
        {{end}}
    </div>

    <div id="settingsPanelRoot">
    <div class="settings-card" id="settingsPanel">
        <div class="settings-card-header">
            <h5 class="mb-0"><i class="bi bi-sliders me-2"></i>Settings</h5>
            <button type="button" class="btn-close btn-close-white" onclick="closeSettings()" aria-label="Close"></button>
        </div>
        <div class="settings-card-body">
            {{template "webmail_settings_nav" .}}
            <div class="settings-body">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h6 class="mb-0">Your signatures</h6>
                    <button type="button" class="btn btn-primary btn-sm" id="sigAddBtn"><i class="bi bi-plus-lg me-1"></i>Add signature</button>
                </div>

                {{if .signatures}}
                {{range .signatures}}
                <div class="card mb-3 sig-card" data-sig-id="{{.ID}}" data-sig-name="{{.Name}}">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <div>
                                <strong>{{.Name}}</strong>
                                {{if .IsDefaultNew}}<span class="badge bg-primary ms-1">Default: New</span>{{end}}
                                {{if .IsDefaultReply}}<span class="badge bg-info text-dark ms-1">Default: Reply</span>{{end}}
                            </div>
                            <div class="btn-group btn-group-sm">
                                <button type="button" class="btn btn-outline-secondary sig-edit-btn" title="Edit"><i class="bi bi-pencil"></i></button>
                                <form method="post" action="/webmail/signatures/{{.ID}}/delete" class="d-inline" onsubmit="return confirm('Delete signature &quot;{{.Name}}&quot;?');">
                                    <button type="submit" class="btn btn-outline-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                        </div>
                        <div class="sig-preview mb-2">{{.ContentHTML | safe}}</div>
                        <div class="d-flex flex-wrap gap-1 align-items-center">
                            <div class="btn-group btn-group-sm">
                                <form method="post" action="/webmail/signatures/{{if .IsDefaultNew}}0{{else}}{{.ID}}{{end}}/default?which=new" class="d-inline">
                                    <button type="submit" class="btn {{if .IsDefaultNew}}btn-primary{{else}}btn-outline-secondary{{end}}">{{if .IsDefaultNew}}✓ Default for new{{else}}Use for new{{end}}</button>
                                </form>
                                <form method="post" action="/webmail/signatures/{{if .IsDefaultReply}}0{{else}}{{.ID}}{{end}}/default?which=reply" class="d-inline">
                                    <button type="submit" class="btn {{if .IsDefaultReply}}btn-info text-dark{{else}}btn-outline-secondary{{end}}">{{if .IsDefaultReply}}✓ Default for reply/forward{{else}}Use for reply/forward{{end}}</button>
                                </form>
                            </div>
                            {{$sigID := .ID}}
                            {{range $.alias_defaults}}
                            {{if eq .SignatureID $sigID}}
                            <span class="badge bg-secondary d-inline-flex align-items-center gap-1">
                                {{if .ForReply}}Reply{{else}}New{{end}} for {{.ForEmail}}
                                <form method="post" action="/webmail/signatures/alias-default/remove" class="d-inline">
                                    <input type="hidden" name="for_email" value="{{.ForEmail}}">
                                    {{if .ForReply}}<input type="hidden" name="for_reply" value="1">{{end}}
                                    <button type="submit" class="btn btn-link btn-sm p-0 text-white" style="text-decoration:none;" title="Remove"><i class="bi bi-x"></i></button>
                                </form>
                            </span>
                            {{end}}
                            {{end}}
                        </div>
                        <div class="sig-content-data" style="display:none;">{{.ContentHTML | safe}}</div>
                    </div>
                </div>
                {{end}}
                {{else}}
                <p class="text-muted mb-0">No signatures yet — add one to have it available in compose.</p>
                {{end}}
            </div>
        </div>
    </div>
    </div>

    <div class="modal fade" id="sigModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="POST" action="/webmail/signatures/save" id="sigForm">
                    <div class="modal-header">
                        <h5 class="modal-title" id="sigModalTitle">New signature</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="id" id="sigFormId" value="">
                        <div class="mb-3">
                            <label class="form-label">Name</label>
                            <input type="text" class="form-control" name="name" id="sigFormName" placeholder="e.g. Work, Personal" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Content</label>
                            <div id="sigEditor"></div>
                            <textarea name="content_html" style="display: none;"></textarea>
                            <div class="form-text">Plain text works fine too — just don't use the formatting toolbar. A blank signature is also valid (e.g. to temporarily disable one without deleting it).</div>
                        </div>
                        {{if .send_as_options}}
                        <div class="border rounded p-2" style="border-color: #404040 !important;">
                            <label class="form-label mb-1">Also set as default for a specific alias</label>
                            <div class="d-flex gap-2 align-items-center flex-wrap">
                                <select class="form-select form-select-sm" name="default_for_email" style="width: auto;">
                                    <option value="">— choose an alias —</option>
                                    {{range .send_as_options}}<option value="{{.}}">{{.}}</option>{{end}}
                                </select>
                                <div class="form-check mb-0">
                                    <input class="form-check-input" type="checkbox" name="default_new" value="1" id="sigDefaultNewAlias">
                                    <label class="form-check-label" for="sigDefaultNewAlias">New messages</label>
                                </div>
                                <div class="form-check mb-0">
                                    <input class="form-check-input" type="checkbox" name="default_reply" value="1" id="sigDefaultReplyAlias">
                                    <label class="form-check-label" for="sigDefaultReplyAlias">Replies/forwards</label>
                                </div>
                            </div>
                        </div>
                        {{end}}
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg me-1"></i>Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    {{template "compose_widget" .}}

    <script src="/webmail/static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="/webmail/static/vendor/quill/quill.js"></script>
    {{template "webmail_settings_script" .}}
</body>
</html>
{{end}}
