{% extends "common/base.html" %}
{% from "components/header.html" import header %}

{% block head %}
    {{ super() }}
    <style>
      body {
        background: #f7f9fb;
      }
    </style>
{% endblock %}

{% block content %}
{{ header(
    title="Contacts",
    description="Manage your contacts and their information",
    button_text="Add New Contact",
    button_url=url_for('contacts.new_contact'),
    icon="fa-address-book",
    button_icon="fa-plus"
) }}

<div class="container mx-auto px-4 py-8">
    <!-- Search and Filter Section -->
    <div class="bg-white rounded-lg shadow-sm p-4 mb-6">
        <form method="GET" class="flex flex-col md:flex-row gap-4" id="filterForm">
            <div class="flex-1">
                <input type="text" name="search" placeholder="Search contacts..." 
                       value="{{ request.args.get('search', '') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-200">
            </div>
            <div class="flex gap-4 items-center">
                <select name="role" class="px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-200">
                    <option value="">All Roles</option>
                    <option value="admin" {% if request.args.get('role') == 'admin' %}selected{% endif %}>Admin</option>
                    <option value="manager" {% if request.args.get('role') == 'manager' %}selected{% endif %}>Manager</option>
                    <option value="user" {% if request.args.get('role') == 'user' %}selected{% endif %}>User</option>
                </select>
                <button type="button" id="clearFilters" class="px-4 py-2 rounded-lg text-white font-medium transition-colors duration-200"
                        style="background-color: var(--primary-color); border: 1px solid var(--primary-color);"
                        onmouseover="this.style.backgroundColor='var(--primary-light)'"
                        onmouseout="this.style.backgroundColor='var(--primary-color)'">
                    Clear
                </button>
            </div>
        </form>
        <script src="{{ url_for('static', filename='js/contacts-filter.js') }}?v={{ 'js/contacts-filter.js'|asset_version }}"></script>
    </div>

    <!-- Contacts List -->
    <div class="bg-white rounded-lg shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Contact Info</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Company</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Role</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for user in users %}
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10">
                                    <img class="h-10 w-10 rounded-full object-cover" 
                                         src="{{ url_for('profile_pic', filename=user.profile_picture) if user.profile_picture else url_for('static', filename='default-avatar.png') }}" 
                                         alt="{{ user.username }}">
                                </div>
                                <div class="ml-4">
                                    <div class="text-sm font-medium text-gray-900">{{ user.username }} {{ user.last_name }}</div>
                                    <div class="text-sm text-gray-500">{{ user.position or 'No position' }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="flex flex-row flex-wrap gap-1.5">
                                <a href="mailto:{{ user.email }}" 
                                   class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm no-underline transition-colors duration-200"
                                   style="background-color: var(--primary-opacity-8); color: var(--primary-color);">
                                    <i class="fas fa-envelope" style="font-size: 0.85em; opacity: 0.7;"></i>
                                    {{ user.email }}
                                </a>
                                {% if user.phone %}
                                <a href="tel:{{ user.phone }}" 
                                   class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm no-underline transition-colors duration-200"
                                   style="background-color: var(--primary-opacity-8); color: var(--primary-color);">
                                    <i class="fas fa-phone" style="font-size: 0.85em; opacity: 0.7;"></i>
                                    {{ user.phone }}
                                </a>
                                {% endif %}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-900">{{ user.company or 'No company' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="flex flex-row flex-wrap gap-1.5">
                                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm font-medium"
                                      style="background-color: {% if user.is_admin %}rgba(147,51,234,0.1){% elif user.is_manager %}rgba(59,130,246,0.1){% else %}rgba(107,114,128,0.1){% endif %}; color: {% if user.is_admin %}#7e22ce{% elif user.is_manager %}#2563eb{% else %}#374151{% endif %};">
                                    <i class="fas fa-{% if user.is_admin %}shield-alt{% elif user.is_manager %}user-tie{% else %}user{% endif %}" style="font-size: 0.85em; opacity: 0.7;"></i>
                                    {{ 'Admin' if user.is_admin else 'Manager' if user.is_manager else 'User' }}
                                </span>
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <div class="flex justify-end gap-1.5">
                                <a href="{{ url_for('contacts.edit_contact', id=user.id) }}" 
                                   class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm no-underline transition-colors duration-200"
                                   style="background-color: var(--primary-opacity-8); color: var(--primary-color);">
                                    <i class="fas fa-edit" style="font-size: 0.85em; opacity: 0.7;"></i>
                                    Edit
                                </a>
                                <form action="{{ url_for('contacts.resend_setup_link', id=user.id) }}" method="POST" class="d-inline">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                    <button type="submit" 
                                            class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm no-underline transition-colors duration-200"
                                            style="background-color: var(--primary-opacity-8); color: var(--primary-color);">
                                        <i class="fas fa-paper-plane" style="font-size: 0.85em; opacity: 0.7;"></i>
                                        Resend Setup Link
                                    </button>
                                </form>
                                {% if user.email != current_user.email %}
                                <button type="button" 
                                        class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-sm no-underline transition-colors duration-200"
                                        style="background-color: rgba(239,68,68,0.1); color: #b91c1c;"
                                        data-bs-toggle="modal" 
                                        data-bs-target="#deleteContactModal{{ user.id }}">
                                    <i class="fas fa-trash" style="font-size: 0.85em; opacity: 0.7;"></i>
                                    Delete
                                </button>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% if user.email != current_user.email %}
                    <!-- Delete Contact Modal -->
                    <div class="modal fade" id="deleteContactModal{{ user.id }}" tabindex="-1" aria-labelledby="deleteContactModalLabel{{ user.id }}" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="deleteContactModalLabel{{ user.id }}">Delete Contact</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <div class="d-flex align-items-center gap-3 mb-3">
                                        <i class="fas fa-trash text-danger" style="font-size: 2rem;"></i>
                                        <div>
                                            <h6 class="mb-1">Delete Contact</h6>
                                            <p class="text-muted mb-0">{{ user.username }} {{ user.last_name }}</p>
                                        </div>
                                    </div>
                                    <div class="alert alert-warning">
                                        <i class="fas fa-exclamation-triangle me-2"></i>
                                        This action cannot be undone. The contact will be permanently deleted.
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                    <form action="{{ url_for('contacts.delete_contact', id=user.id) }}" method="POST" class="d-inline">
                                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                        <button type="submit" class="btn btn-danger">
                                            <i class="fas fa-trash me-2"></i>Delete Contact
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Pagination -->
    {% if pagination and pagination.pages > 1 %}
    <div class="mt-6 flex justify-center">
        <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
            {% if pagination.has_prev %}
            <a href="{{ url_for('contacts.contacts_list', page=pagination.prev_num, **request.args) }}" 
               class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                <span class="sr-only">Previous</span>
                <i class="fas fa-chevron-left"></i>
            </a>
            {% endif %}
            
            {% for page in pagination.iter_pages() %}
                {% if page %}
                    <a href="{{ url_for('contacts.contacts_list', page=page, **request.args) }}"
                       class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium {% if page == pagination.page %}text-[#16767b] bg-[#16767b]/10{% else %}text-gray-700 hover:bg-gray-50{% endif %}">
                        {{ page }}
                    </a>
                {% else %}
                    <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700">
                        ...
                    </span>
                {% endif %}
            {% endfor %}
            
            {% if pagination.has_next %}
            <a href="{{ url_for('contacts.contacts_list', page=pagination.next_num, **request.args) }}"
               class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                <span class="sr-only">Next</span>
                <i class="fas fa-chevron-right"></i>
            </a>
            {% endif %}
        </nav>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/contacts-filter.js') }}?v={{ 'js/contacts-filter.js'|asset_version }}"></script>
{% endblock %} 