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

{% block title %}{% trans "Zoom Meetings" %} - {{ block.super }}{% endblock %}

{% block customCSS %}
<style>
    /* UI Variables for the KAAT-S Theme (Consistent with Reference) */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-gray-light: #f8f9fa; 
    }

    /* Enhanced Card Styling (Consistent) */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        transition: transform 0.2s, box-shadow 0.2s;
        background-color: white;
    }
    .card:not(.no-hover):hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.1);
    }
    .card.no-hover:hover {
        transform: none;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    }

    /* Main Action Button Style (Teal Theme) */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        padding: 0.5rem 1rem;
    }

    .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 (For Edit/Outline - Consistent) */
    .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);
    }
    /* Primary Outline for View/Join */
    .btn-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }

    /* Meeting Card Specifics (Adapted to Standard Card View) */
    .meeting-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }
    .meeting-card .card-text i {
        color: var(--kaauh-teal);
        width: 1.25rem;
    }

    /* Status Badges (Standardized) */
    .status-badge {
        font-size: 0.8rem;
        padding: 0.4em 0.8em;
        border-radius: 0.4rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.7px;
    }
    /* Status Badge Mapping */
    .bg-waiting { background-color: #ffc107 !important; color: var(--kaauh-primary-text) !important;}
    .bg-started { background-color: var(--kaauh-teal) !important; color: white !important;}
    .bg-ended { background-color: #dc3545 !important; color: white !important;}

    /* Table Styling (Consistent with Reference) */
    .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);
    }
    
    /* Pagination Link Styling (Consistent) */
    .pagination .page-item .page-link {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-border);
    }
    .pagination .page-item.active .page-link {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
    }
    .pagination .page-item:hover .page-link:not(.active) {
        background-color: #e9ecef;
    }

    /* Filter & Search Layout Adjustments */
    .filter-buttons {
        display: flex;
        gap: 0.5rem;
    }
    
    /* Icon color for empty state */
    .text-muted.fa-3x {
        color: var(--kaauh-teal-dark) !important;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 style="color: var(--kaauh-teal-dark); font-weight: 700;">
            <i class="fas fa-video me-2"></i> {% trans "Zoom Meetings" %}
        </h1>
        <a href="{% url 'create_meeting' %}" class="btn btn-main-action">
            <i class="fas fa-plus me-1"></i> {% trans "Create Meeting" %}
        </a>
    </div>

    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <label for="search" class="form-label small text-muted">{% trans "Search by Topic" %}</label>
                    <div class="input-group input-group-lg mb-3">
                        <form method="get" action="" class="w-100">
                            {% include "includes/search_form.html" with search_query=search_query %}
                        </form>
                    </div>
                </div>
                <div class="col-md-8">
                    <form method="GET" class="row g-3 align-items-end" >
                        {% if search_query %}<input type="hidden" name="q" value="{{ search_query }}">{% endif %}

                        <div class="col-md-3">
                            <label for="status" class="form-label small text-muted">{% trans "Filter by Status" %}</label>
                            <select name="status" id="status" class="form-select form-select-sm">
                                <option value="">{% trans "All Statuses" %}</option>
                                <option value="waiting" {% if status_filter == 'waiting' %}selected{% endif %}>{% trans "Waiting" %}</option>
                                <option value="started" {% if status_filter == 'started' %}selected{% endif %}>{% trans "Started" %}</option>
                                <option value="ended" {% if status_filter == 'ended' %}selected{% endif %}>{% trans "Ended" %}</option>
                            </select>
                        </div>
        
                        <div class="col-md-5">
                            <div class="filter-buttons">
                                <button type="submit" class="btn btn-main-action btn-lg">
                                    <i class="fas fa-filter me-1"></i> {% trans "Apply Filters" %}
                                </button>
                                {% if status_filter or search_query %}
                                    <a href="{% url 'meeting_list' %}" class="btn btn-outline-secondary btn-lg">
                                        <i class="fas fa-times me-1"></i> {% trans "Clear" %}
                                    </a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% if meetings %}
        <div id="meetings-list">
            {# View Switcher #}
            {% include "includes/_list_view_switcher.html" with list_id="meetings-list" %}

            {# Card View #}
            <div class="card-view active row">
                {% for meeting in meetings %}
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card meeting-card h-100 shadow-sm">
                            <div class="card-body d-flex flex-column">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <h5 class="card-title flex-grow-1 me-3">{{ meeting.topic }}</h5>
                                    <span class="status-badge bg-{{ meeting.status }}">
                                        {{ meeting.status|title }}
                                    </span>
                                </div>

                                <p class="card-text text-muted small mb-3">
                                    <i class="fas fa-hashtag"></i> {% trans "ID" %}: {{ meeting.meeting_id|default:meeting.id }}<br>
                                    <i class="fas fa-clock"></i> {% trans "Start" %}: {{ meeting.start_time|date:"M d, Y H:i" }}<br>
                                    <i class="fas fa-stopwatch"></i> {% trans "Duration" %}: {{ meeting.duration }} minutes
                                </p>

                                <div class="mt-auto pt-2 border-top">
                                   <div class="d-flex gap-2">
                                        <a href="{% url 'meeting_details' meeting.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i> {% trans "View" %}
                                        </a>

                                        {% if meeting.join_url %}
                                            <a href="{{ meeting.join_url }}" target="_blank" class="btn btn-sm btn-main-action">
                                                <i class="fas fa-link"></i> {% trans "Join" %}
                                            </a>
                                        {% endif %}

                                        <a href="{% url 'update_meeting' meeting.pk %}" class="btn btn-sm btn-outline-secondary">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button type="button" class="btn btn-outline-danger btn-sm" title="{% trans 'Delete' %}"
                                                data-bs-toggle="modal" data-bs-target="#deleteModal"
                                                data-delete-url="{% url 'delete_meeting' meeting.pk %}"
                                                data-item-name="{{ meeting.topic }}">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>

            {# Table View #}
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col" style="width: 30%;">{% trans "Topic" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "ID" %}</th>
                                <th scope="col" style="width: 20%;">{% trans "Start Time" %}</th>
                                <th scope="col" style="width: 10%;">{% trans "Duration" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Status" %}</th>
                                <th scope="col" style="width: 10%;" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for meeting in meetings %}
                            <tr>
                                <td><strong class="text-primary">{{ meeting.topic }}</strong></td>
                                <td>{{ meeting.meeting_id|default:meeting.id }}</td>
                                <td>{{ meeting.start_time|date:"M d, Y H:i" }}</td>
                                <td>{{ meeting.duration }} min</td>
                                <td>
                                    <span class="status-badge bg-{{ meeting.status }}">
                                        {{ meeting.status|title }}
                                    </span>
                                </td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        {% if meeting.join_url %}
                                            <a href="{{ meeting.join_url }}" target="_blank" class="btn btn-main-action" title="{% trans 'Join' %}">
                                                <i class="fas fa-sign-in-alt"></i>
                                            </a>
                                        {% endif %}
                                        <a href="{% url 'meeting_details' meeting.pk %}" class="btn btn-outline-primary" title="{% trans 'View' %}">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'update_meeting' meeting.pk %}" class="btn btn-outline-secondary" title="{% trans 'Update' %}">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button type="button" class="btn btn-outline-danger" title="{% trans 'Delete' %}"
                                                data-bs-toggle="modal" data-bs-target="#deleteModal"
                                                data-delete-url="{% url 'delete_meeting' meeting.pk %}"
                                                data-item-name="{{ meeting.topic }}">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# Pagination (Standardized) #}
        {% if is_paginated %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}">First</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}">Previous</a>
                        </li>
                    {% endif %}

                    <li class="page-item active">
                        <span class="page-link">{{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
                    </li>

                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}">Next</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}">Last</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        {% endif %}
    {% else %}
        <div class="text-center py-5 card shadow-sm">
            <div class="card-body">
                <i class="fas fa-video fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No Zoom meetings found" %}</h3>
                <p class="text-muted">{% trans "Create your first meeting or adjust your filters." %}</p>
                <a href="{% url 'create_meeting' %}" class="btn btn-main-action mt-3">
                    <i class="fas fa-plus me-1"></i> {% trans "Create Your First Meeting" %}
                </a>
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}