{% extends 'base.html' %}
{% load static i18n %}

{% block title %}{% trans "Hiring Agencies" %} - ATS{% endblock %}

{% block customCSS %}
<style>
    /* KAAT-S UI Variables */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-success: #28a745;
        --kaauh-info: #17a2b8;
        --kaauh-danger: #dc3545;
        --kaauh-warning: #ffc107;
        --kaauh-gray-light: #f8f9fa;
    }

    /* Primary Color Overrides */
    .text-primary-theme { color: var(--kaauh-teal) !important; }
    .bg-primary-theme { background-color: var(--kaauh-teal) !important; }

    /* Main Container & Card Styling */
    .kaauh-card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        background-color: white;
    }

    /* Agency Card Styling */
    .agency-card {
        transition: all 0.3s ease;
        border-left: 4px solid var(--kaauh-teal);
    }
    .agency-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0,0,0,0.1);
    }

    /* Button Styling */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        transition: all 0.2s ease;
    }
    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }

    /* Secondary Button Style */
    .btn-outline-secondary {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }

    /* Search Form Styling */
    .search-form {
        background-color: #f8f9fa;
        border-radius: 0.75rem;
        padding: 1.5rem;
        margin-bottom: 2rem;
        border: 1px solid var(--kaauh-border);
    }

    /* Stats Badge */
    .stats-badge {
        background-color: var(--kaauh-teal);
        color: white;
        padding: 0.25rem 0.5rem;
        border-radius: 0.375rem;
        font-size: 0.875rem;
        font-weight: 600;
    }

    /* Table View Styling */
    .table-view .table thead th {
        background-color: var(--kaauh-teal-dark);
        color: white;
        font-weight: 600;
        border-color: var(--kaauh-border);
        text-transform: uppercase;
        font-size: 0.8rem;
        letter-spacing: 0.5px;
        padding: 1rem;
    }
    .table-view .table tbody td {
        vertical-align: middle;
        padding: 1rem;
        border-color: var(--kaauh-border);
    }
    .table-view .table tbody tr:hover {
        background-color: var(--kaauh-gray-light);
    }

    /* Card View Specific Styles */
    .card-view .card {
        height: 100%;
    }
    .card-view .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 700;
    }
    .card-view .card-body {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex-grow: 1;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Header Section -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-1" style="color: var(--kaauh-teal-dark); font-weight: 700;">
                <i class="fas fa-building me-2"></i>
                {% trans "Hiring Agencies" %}
            </h1>
            <h2 class="h5 text-muted mb-0">
                {% trans "Total Agencies:" %} <span class="fw-bold">{{ total_agencies }}</span>
            </h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-main-action" href="{% url 'agency_assignment_list' %}">
                <span class="d-flex align-items-center gap-2">
                    <i class="fas fa-tasks"></i>
                    {% trans "View All Job Assignments" %}
                </span>
            </a>
            <a href="{% url 'agency_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Agency" %}
            </a>
        </div>
    </div>

  
<div class="card mb-4 shadow-sm no-hover">
    <div class="card-body">
        <div class="row g-4">
            <div class="col-md-6">
                <label for="search" class="form-label small text-muted">{% trans "Search by name, contact person, email, or country..." %}</label>
                <div class="input-group input-group-lg">
                    <form method="get" action="" class="w-100">
                        {% include 'includes/search_form.html' %}
                    </form>
                </div>
            </div>

            
        </div>
    </div>
</div>

    <!-- Agencies List -->
    {% if page_obj %}
        <div id="agency-list">
            {% include "includes/_list_view_switcher.html" with list_id="agency-list" %}

            <!-- Table View -->
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Agency Name" %}</th>
                                <th scope="col">{% trans "Contact Person" %}</th>
                                <th scope="col">{% trans "Email" %}</th>
                                <th scope="col">{% trans "Phone" %}</th>
                                <th scope="col">{% trans "Country" %}</th>
                                <th scope="col">{% trans "Website" %}</th>
                                <th scope="col">{% trans "Created" %}</th>
                                <th scope="col" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for agency in page_obj %}
                                <tr>
                                    <td class="fw-medium">
                                        <a href="{% url 'agency_detail' agency.slug %}"
                                           class="text-decoration-none text-secondary">
                                            {{ agency.name }}
                                        </a>
                                    </td>
                                    <td>{{ agency.contact_person|default:"-" }}</td>
                                    <td>
                                        {% if agency.email %}
                                            <a href="mailto:{{ agency.email }}"
                                               class="text-decoration-none text-dark smal"
                                               title="{{ agency.email }}">
                                                <i class="fas fa-envelope me-1"></i>
                                                {{ agency.email|truncatechars:30 }}
                                            </a>
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>{{ agency.phone|default:"-" }}</td>
                                    <td>
                                        {% if agency.country %}
                                            <i class="fas fa-globe text-muted me-1"></i>
                                            {{ agency.get_country_display }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if agency.website %}
                                            <a href="{{ agency.website }}"
                                               target="_blank"
                                               class="text-decoration-none text-secondary">
                                                {{ agency.website|truncatechars:25 }}
                                                <i class="fas fa-external-link-alt ms-1 small"></i>
                                            </a>
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="stats-badge">
                                            {{ agency.created_at|date:"M d, Y" }}
                                        </span>
                                    </td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'agency_detail' agency.slug %}"
                                               class="btn btn-outline-secondary"
                                               title="{% trans 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'agency_update' agency.slug %}"
                                               class="btn btn-outline-secondary"
                                               title="{% trans 'Edit' %}">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Card View -->
            <div class="card-view row g-4">
                {% for agency in page_obj %}
                    <div class="col-lg-4 col-md-6 mb-4">
                        <div class="card kaauh-card agency-card h-100">
                            <div class="card-body">
                                <!-- Agency Header -->
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <h5 class="card-title mb-0" style="color: var(--kaauh-teal-dark);">
                                        {{ agency.name }}
                                    </h5>
                                    {% if agency.email %}
                                    <a href="mailto:{{ agency.email }}" class="text-muted" title="{{ agency.email }}">
                                        <i class="fas fa-envelope"></i>
                                    </a>
                                    {% endif %}
                                </div>

                                <!-- Contact Information -->
                                {% if agency.contact_person %}
                                <p class="card-text mb-2">
                                    <i class="fas fa-user text-muted me-2"></i>
                                    <strong>{% trans "Contact:" %}</strong> {{ agency.contact_person }}
                                </p>
                                {% endif %}

                                {% if agency.phone %}
                                <p class="card-text mb-2">
                                    <i class="fas fa-phone text-muted me-2"></i>
                                    {{ agency.phone }}
                                </p>
                                {% endif %}

                                {% if agency.country %}
                                <p class="card-text mb-2">
                                    <i class="fas fa-globe text-muted me-2"></i>
                                    {{ agency.get_country_display }}
                                </p>
                                {% endif %}

                                <!-- Website Link -->
                                {% if agency.website %}
                                <p class="card-text mb-3">
                                    <i class="fas fa-link text-muted me-2"></i>
                                    <a href="{{ agency.website }}" target="_blank" class="text-decoration-none text-secondary">
                                        {{ agency.website|truncatechars:30 }}
                                        <i class="fas fa-external-link-alt ms-1 small"></i>
                                    </a>
                                </p>
                                {% endif %}

                                <!-- Action Buttons -->
                                <div class="d-flex justify-content-between align-items-center mt-auto">
                                    <div>
                                        <a href="{% url 'agency_detail' agency.slug %}"
                                           class="btn btn-main-action btn-sm me-2">
                                            <i class="fas fa-eye me-1"></i> {% trans "View" %}
                                        </a>
                                        <a href="{% url 'agency_update' agency.slug %}"
                                           class="btn btn-outline-secondary btn-sm">
                                            <i class="fas fa-edit me-1"></i> {% trans "Edit" %}
                                        </a>
                                    </div>
                                    <div>
                                        <span class="stats-badge">
                                            {% trans "Created" %} {{ agency.created_at|date:"M d, Y" }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>

        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <nav aria-label="{% trans 'Agency pagination' %}" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ num }}{% if search_query %}&q={{ search_query }}{% endif %}">{{ num }}</a>
                        </li>
                    {% elif num == 1 or num == page_obj.paginator.num_pages %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ num }}{% if search_query %}&q={{ search_query }}{% endif %}">{{ num }}</a>
                        </li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    {% else %}
        <!-- Empty State -->
        <div class="text-center py-5">
            <div class="mb-4">
                <i class="fas fa-building fa-4x text-muted"></i>
            </div>
            <h4 class="text-muted mb-3">
                {% if search_query %}
                    {% trans "No agencies found matching your search criteria." %}
                {% else %}
                    {% trans "No hiring agencies have been added yet." %}
                {% endif %}
            </h4>
            <p class="text-muted mb-4">
                {% trans "Start by adding your first hiring agency to manage your recruitment partners." %}
            </p>
            <a href="{% url 'agency_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-2"></i> {% trans "Add Your First Agency" %}
            </a>
        </div>
    {% endif %}
</div>
{% endblock %}
