{% extends "contacts_base.html" %}

{% block title %}Existing Contacts - mc-webui{% endblock %}

{% block page_content %}
<div id="existingPageContent" class="p-3">
    <!-- Page Header -->
    <div class="mb-3">
        <h4 class="mb-2">
            <i class="bi bi-person-lines-fill"></i> Existing Contacts
            <span class="badge counter-badge counter-ok rounded-pill" id="contactsCounter" style="display: none; font-size: 0.8rem;">0 / 350</span>
        </h4>
    </div>

    <!-- Action Buttons -->
    <div class="d-flex gap-2 mb-3">
        <button class="btn btn-outline-secondary btn-sm" onclick="navigateTo('/contacts/manage');">
            <i class="bi bi-arrow-left"></i> Back
        </button>
        <button class="btn btn-outline-primary btn-sm" id="refreshExistingBtn">
            <i class="bi bi-arrow-clockwise"></i> Refresh
        </button>
    </div>

    <!-- Search Toolbar -->
    <div class="search-toolbar">
        <input type="text" class="form-control" id="searchInput" placeholder="Search by name or public key...">
    </div>

    <!-- Filter and Sort Toolbar -->
    <div class="filter-sort-toolbar">
        <!-- Source Filter -->
        <select class="form-select" id="sourceFilter">
            <option value="ALL">All sources</option>
            <option value="DEVICE">On device</option>
            <option value="CACHE">Cache only</option>
            <option value="IGNORED">Ignored</option>
            <option value="BLOCKED">Blocked</option>
        </select>

        <!-- Type Filter -->
        <select class="form-select" id="typeFilter">
            <option value="ALL">All Types</option>
            <option value="COM">COM</option>
            <option value="REP">REP</option>
            <option value="ROOM">ROOM</option>
            <option value="SENS">SENS</option>
        </select>

        <!-- Sort Dropdown -->
        <select class="form-select" id="sortSelect">
            <option value="last_advert_desc">Last advert ↓</option>
            <option value="last_advert_asc">Last advert ↑</option>
            <option value="name_asc">Name A→Z</option>
            <option value="name_desc">Name Z→A</option>
        </select>
    </div>

    <!-- Loading State -->
    <div id="existingLoading" class="text-center py-5" style="display: none;">
        <div class="spinner-border text-primary" role="status">
            <span class="visually-hidden">Loading...</span>
        </div>
        <p class="mt-3 text-muted">Loading contacts...</p>
    </div>

    <!-- Empty State -->
    <div id="existingEmpty" class="empty-state" style="display: none;">
        <i class="bi bi-inbox"></i>
        <p class="mb-0">No contacts found</p>
        <small class="text-muted">Contacts will appear here once added</small>
    </div>

    <!-- Existing Contacts List (Full-screen) -->
    <div id="existingList" class="contacts-list-fullscreen">
        <!-- Dynamically populated by contacts.js -->
    </div>

    <!-- Error State -->
    <div id="existingError" class="alert alert-danger" style="display: none;" role="alert">
        <i class="bi bi-exclamation-triangle"></i>
        <span id="existingErrorMessage">Failed to load contacts</span>
    </div>
</div>
{% endblock %}
