{{define "webmail_contacts.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">Contacts</h6>
                    <button type="button" class="btn btn-primary btn-sm" id="contactAddBtn"><i class="bi bi-plus-lg me-1"></i>Add contact</button>
                </div>

                {{if .contacts}}
                <div class="card">
                    <div class="table-responsive">
                        <table class="table table-dark table-hover mb-0">
                            <thead><tr><th>Name</th><th>Email</th><th>Phone</th><th></th></tr></thead>
                            <tbody>
                                {{range .contacts}}
                                <tr class="contact-row" data-contact-id="{{.ID}}" data-contact-name="{{.Name}}" data-contact-email="{{.Email}}" data-contact-phone="{{.Phone}}">
                                    <td>{{.Name}}</td>
                                    <td>{{.Email}}</td>
                                    <td>{{if .Phone}}{{.Phone}}{{else}}<span class="text-muted">—</span>{{end}}</td>
                                    <td class="d-flex gap-1 justify-content-end">
                                        <button type="button" class="btn btn-outline-secondary btn-sm contact-edit-btn" title="Edit"><i class="bi bi-pencil"></i></button>
                                        <form method="post" action="/webmail/contacts/{{.ID}}/delete" class="d-inline">
                                            <button type="submit" class="btn btn-outline-danger btn-sm" title="Delete" data-confirm="Delete contact &quot;{{.Name}}&quot;?"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </td>
                                </tr>
                                {{end}}
                            </tbody>
                        </table>
                    </div>
                </div>
                {{else}}
                <p class="text-muted mb-0">No contacts yet — add one to have it available in compose autocomplete.</p>
                {{end}}
            </div>
        </div>
    </div>
    </div>

    <div class="modal fade" id="contactModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="POST" action="/webmail/contacts/save" id="contactForm">
                    <div class="modal-header">
                        <h5 class="modal-title" id="contactModalTitle">New contact</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="contactFormId" value="">
                        <div class="mb-3">
                            <label class="form-label">Name</label>
                            <input type="text" class="form-control" name="name" id="contactFormName" placeholder="Jane Doe" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Email</label>
                            <input type="email" class="form-control" name="email" id="contactFormEmail" placeholder="jane@example.com" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Phone <span class="text-muted">(optional)</span></label>
                            <input type="text" class="form-control" name="phone" id="contactFormPhone" placeholder="+1 555 123 4567">
                        </div>
                    </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}}
