<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Template Management — Admin</title>
    <style nonce="{{ csp_nonce }}">
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: system-ui, -apple-system, sans-serif; background: #f5f5f5; color: #333; padding: 20px; }
        h1 { font-size: 20px; margin-bottom: 16px; }
        .card { background: white; border-radius: 8px; padding: 24px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
        .btn { display: inline-block; padding: 6px 14px; background: #1a1a2e; color: white; text-decoration: none; border-radius: 4px; border: none; cursor: pointer; font-size: 13px; }
        .btn-sm { padding: 4px 10px; font-size: 12px; }
        .btn-danger { background: #c0392b; }
        .btn-outline { background: transparent; border: 1px solid #1a1a2e; color: #1a1a2e; }
        .btn:hover { opacity: 0.9; }
        table { width: 100%; border-collapse: collapse; font-size: 13px; }
        th { text-align: left; padding: 8px; border-bottom: 2px solid #ddd; font-weight: 600; }
        td { padding: 8px; border-bottom: 1px solid #eee; vertical-align: top; }
        .badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
        .badge-featured { background: #fff3cd; color: #856404; }
        .badge-cat { background: #e8f5e9; color: #2e7d32; }
        .actions { display: flex; gap: 4px; }
        .flash { padding: 10px 14px; border-radius: 4px; margin-bottom: 16px; font-size: 13px; }
        .flash-success { background: #d4edda; color: #155724; }
        .flash-error { background: #f8d7da; color: #721c24; }
        .modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: none; align-items: center; justify-content: center; z-index: 100; }
        .modal.active { display: flex; }
        .modal-content { background: white; border-radius: 8px; padding: 24px; width: 500px; max-width: 90vw; max-height: 80vh; overflow-y: auto; }
        .modal-content h2 { font-size: 16px; margin-bottom: 16px; }
        .form-group { margin-bottom: 12px; }
        .form-group label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px; }
        .form-group input, .form-group select, .form-group textarea { width: 100%; padding: 6px 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 13px; }
        .form-group textarea { font-family: monospace; font-size: 12px; min-height: 120px; }
        .modal-actions { display: flex; gap: 8px; margin-top: 16px; }
    </style>
</head>
<body>
    <h1>Template Management <span style="font-weight:400; color:#888;">({{ templates|length }} templates)</span></h1>
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% for cat, msg in messages %}
            <div class="flash flash-{{ cat }}">{{ msg }}</div>
        {% endfor %}
    {% endwith %}

    <div class="card">
        <div style="margin-bottom:12px;">
            <button class="btn" data-onclick="showCreateModal()">+ New Template</button>
        </div>
        <table>
            <thead>
                <tr>
                    <th>Slug</th>
                    <th>Name</th>
                    <th>Category</th>
                    <th>Fields</th>
                    <th>Featured</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for t in templates %}
                <tr>
                    <td><code style="font-size:12px;">{{ t.slug }}</code></td>
                    <td>{{ t.name }}</td>
                    <td><span class="badge badge-cat">{{ t.category }}</span></td>
                    <td>{{ t.field_count }}</td>
                    <td>{% if t.is_featured %}<span class="badge badge-featured">★ Featured</span>{% else %}—{% endif %}</td>
                    <td class="actions">
                        <button class="btn btn-sm btn-outline" onclick='showEditModal({{ t|tojson }})'>Edit</button>
                        <button class="btn btn-sm btn-danger" data-onclick="deleteTemplate('{{ t.slug }}')">Delete</button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Create/Edit Modal -->
    <div class="modal" id="templateModal">
        <div class="modal-content">
            <h2 id="modalTitle">New Template</h2>
            <div class="form-group">
                <label>Slug</label>
                <input type="text" id="tmplSlug" placeholder="e.g. contact-us">
            </div>
            <div class="form-group">
                <label>Name</label>
                <input type="text" id="tmplName" placeholder="e.g. Contact Us">
            </div>
            <div class="form-group">
                <label>Description</label>
                <input type="text" id="tmplDesc" placeholder="Brief description">
            </div>
            <div class="form-group">
                <label>Category</label>
                <select id="tmplCategory">
                    <option>Business</option>
                    <option>Events</option>
                    <option>Hiring</option>
                    <option>Support</option>
                    <option>E-commerce</option>
                    <option>Onboarding</option>
                </select>
            </div>
            <div class="form-group">
                <label>Success Message</label>
                <input type="text" id="tmplSuccessMsg" placeholder="Thank you for your submission!">
            </div>
            <div class="form-group">
                <label>Featured</label>
                <select id="tmplFeatured">
                    <option value="0">No</option>
                    <option value="1">Yes</option>
                </select>
            </div>
            <div class="form-group">
                <label>Field Config (JSON)</label>
                <textarea id="tmplFieldConfig" placeholder='[{"name":"email","label":"Email","type":"email","required":true}]'></textarea>
            </div>
            <div class="modal-actions">
                <button class="btn" data-onclick="saveTemplate()">Save</button>
                <button class="btn btn-outline" data-onclick="closeModal()">Cancel</button>
            </div>
        </div>
    </div>

    <script src="/static/js/admin-templates-inline.js"></script>
</body>
</html>
