{% extends "contacts_base.html" %}

{% block title %}Contact Management Settings - mc-webui{% endblock %}

{% block page_content %}
<div id="managePageContent" class="p-3">
    <!-- Navigation Section -->
    <div class="mb-4">
        <!-- Add Contact Card -->
        <div class="nav-card" onclick="navigateTo('/contacts/add');" style="border-left: 4px solid #198754;">
            <div>
                <h6><i class="bi bi-person-plus"></i> Add Contact</h6>
                <small class="text-muted">Add from URI, QR code, or manual entry</small>
            </div>
            <i class="bi bi-chevron-right text-muted"></i>
        </div>

        <!-- Pending Contacts Card -->
        <div class="nav-card" onclick="navigateTo('/contacts/pending');">
            <div>
                <h6><i class="bi bi-hourglass-split"></i> Pending Contacts</h6>
                <small class="text-muted">Contacts awaiting manual approval</small>
            </div>
            <span class="badge bg-primary rounded-pill" id="pendingBadge" style="font-size: 1.1rem;">
                <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
            </span>
        </div>

        <!-- Existing Contacts Card -->
        <div class="nav-card" onclick="navigateTo('/contacts/existing');">
            <div>
                <h6><i class="bi bi-person-lines-fill"></i> Existing Contacts</h6>
                <small class="text-muted">Manage all stored contacts</small>
            </div>
            <span class="badge counter-badge counter-ok rounded-pill" id="existingBadge" style="font-size: 1.1rem;">
                <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
            </span>
        </div>
    </div>

    <!-- Cleanup Contacts Section -->
    <div class="cleanup-section">
        <h6>
            <i class="bi bi-trash"></i>
            <span>Cleanup Contacts</span>
            <i class="bi bi-info-circle info-icon"
               data-bs-toggle="tooltip"
               data-bs-placement="top"
               title="Remove contacts based on various filter criteria"></i>
        </h6>

        <!-- Basic Filters (Always visible) -->
        <div class="mb-3">
            <label for="cleanupNameFilter" class="form-label">Name Filter (optional):</label>
            <input type="text" class="form-control" id="cleanupNameFilter" placeholder="Enter partial name to search...">
        </div>

        <!-- Advanced Filters (Collapsible) -->
        <div class="mb-3">
            <button class="btn btn-sm btn-outline-secondary w-100" type="button" data-bs-toggle="collapse" data-bs-target="#advancedFilters" aria-expanded="false" aria-controls="advancedFilters">
                <i class="bi bi-sliders"></i> Advanced Filters
            </button>
        </div>

        <div class="collapse" id="advancedFilters">
            <!-- Contact Types -->
            <div class="mb-3">
                <label class="form-label">Contact Types:</label>
                <div class="d-flex flex-wrap gap-2">
                    <div class="form-check">
                        <input class="form-check-input cleanup-type-filter" type="checkbox" value="1" id="cleanupTypeCOM" checked>
                        <label class="form-check-label" for="cleanupTypeCOM">COM</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input cleanup-type-filter" type="checkbox" value="2" id="cleanupTypeREP" checked>
                        <label class="form-check-label" for="cleanupTypeREP">REP</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input cleanup-type-filter" type="checkbox" value="3" id="cleanupTypeROOM" checked>
                        <label class="form-check-label" for="cleanupTypeROOM">ROOM</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input cleanup-type-filter" type="checkbox" value="4" id="cleanupTypeSENS" checked>
                        <label class="form-check-label" for="cleanupTypeSENS">SENS</label>
                    </div>
                </div>
            </div>

            <!-- Date Field -->
            <div class="mb-3">
                <label class="form-label">Date Field:</label>
                <div class="d-flex gap-3">
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="cleanupDateField" id="cleanupDateLastAdvert" value="last_advert" checked>
                        <label class="form-check-label" for="cleanupDateLastAdvert">Last Advert</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="cleanupDateField" id="cleanupDateLastMod" value="lastmod">
                        <label class="form-check-label" for="cleanupDateLastMod">Last Modified</label>
                    </div>
                </div>
            </div>

            <!-- Days of Inactivity -->
            <div class="mb-3">
                <label for="cleanupDays" class="form-label">Days of Inactivity (0 = ignore):</label>
                <input type="number" class="form-control" id="cleanupDays" value="0" min="0">
                <small class="form-text text-muted">Contacts inactive for more than this many days will be selected</small>
            </div>

            <!-- Auto-Cleanup Toggle -->
            <div class="mb-3 mt-4 pt-3 border-top">
                <div class="form-check form-switch d-flex align-items-center gap-2">
                    <input class="form-check-input" type="checkbox" role="switch" id="autoCleanupSwitch" style="cursor: pointer; min-width: 3rem; min-height: 1.5rem;">
                    <label class="form-check-label" for="autoCleanupSwitch" style="cursor: pointer; font-weight: 500;">
                        <span id="autoCleanupLabel">Enable Auto-Cleanup</span>
                    </label>
                    <i class="bi bi-info-circle info-icon"
                       data-bs-toggle="tooltip"
                       data-bs-placement="top"
                       title="When enabled, contacts matching the above criteria will be automatically deleted daily at the specified hour"></i>
                </div>
                <small class="text-muted">Status: <span id="autoCleanupStatusText">Loading...</span></small>

                <!-- Cleanup Hour Selector -->
                <div class="mt-2 d-flex align-items-center gap-2">
                    <label for="cleanupHour" class="form-label mb-0 small">Run at:</label>
                    <select class="form-select form-select-sm" id="cleanupHour" style="width: auto;" disabled>
                        <option value="0">00:00</option>
                        <option value="1" selected>01:00</option>
                        <option value="2">02:00</option>
                        <option value="3">03:00</option>
                        <option value="4">04:00</option>
                        <option value="5">05:00</option>
                        <option value="6">06:00</option>
                        <option value="7">07:00</option>
                        <option value="8">08:00</option>
                        <option value="9">09:00</option>
                        <option value="10">10:00</option>
                        <option value="11">11:00</option>
                        <option value="12">12:00</option>
                        <option value="13">13:00</option>
                        <option value="14">14:00</option>
                        <option value="15">15:00</option>
                        <option value="16">16:00</option>
                        <option value="17">17:00</option>
                        <option value="18">18:00</option>
                        <option value="19">19:00</option>
                        <option value="20">20:00</option>
                        <option value="21">21:00</option>
                        <option value="22">22:00</option>
                        <option value="23">23:00</option>
                    </select>
                    <span class="small text-muted" id="cleanupTimezoneLabel"></span>
                </div>
            </div>
        </div>

        <button class="btn btn-warning" id="cleanupPreviewBtn">
            <i class="bi bi-eye"></i> Preview Cleanup
        </button>
    </div>
</div>

<!-- Cleanup Confirmation Modal -->
<div class="modal fade" id="cleanupConfirmModal" tabindex="-1" aria-labelledby="cleanupConfirmModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header bg-warning">
                <h5 class="modal-title" id="cleanupConfirmModalLabel">
                    <i class="bi bi-exclamation-triangle"></i> Confirm Contact Cleanup
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p class="fw-bold">The following <span id="cleanupContactCount">0</span> contact(s) will be permanently deleted:</p>
                <div id="cleanupContactList" class="list-group mb-3" style="max-height: 300px; overflow-y: auto;">
                    <!-- Contact list will be populated here -->
                </div>
                <div class="alert alert-warning mb-0">
                    <i class="bi bi-exclamation-circle"></i>
                    <strong>Warning:</strong> This action cannot be undone!
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger" id="confirmCleanupBtn">
                    <i class="bi bi-trash"></i> Delete Contacts
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}
