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

{% block title %}{% trans "Zoom Meetings" %} - {{ block.super }}{% endblock %}
{% block customCSS %}
<style>
    /* UI Variables for the KAAT-S Theme */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-gray: #6c757d;

        /* Status Colors based on KAAT-S example */
        --kaauh-success: var(--kaauh-teal);
        --kaauh-warning: #ffc107;
        --kaauh-danger: #dc3545;
        --kaauh-secondary: #6c757d;
    }

    /* Base style for all buttons to inherit Bootstrap's basic structure/reset */
    .btn-base {
        display: inline-block;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        user-select: none;
        padding: 0.375rem 0.75rem; /* Standard default padding */
        font-size: 1rem;
        line-height: 1.5;
        border-radius: 0.25rem;
        border: 1px solid transparent;
        transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        /* FIX: Remove link underline for anchor tags used as buttons */
        text-decoration: none; 
    }

    /* Small Button Size */
    .btn-sm {
        padding: 0.3rem 0.6rem; 
        font-size: 0.8rem;
        font-weight: 600;
        line-height: 1.5;
    }
    
    /* Main Action Button (Create Meeting) */
    .btn-main-action { 
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
    }
    .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);
        text-decoration: none; /* Ensure no hover underline if base fails */
    }
    
    /* Outline Primary (View/Join buttons) */
    .btn-kaats-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        background-color: transparent;
    }
    .btn-kaats-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
        text-decoration: none;
    }

    /* Outline Secondary (Update button) */
    .btn-kaats-outline-secondary {
        color: var(--kaauh-secondary);
        border-color: var(--kaauh-secondary);
        background-color: transparent;
    }
    .btn-kaats-outline-secondary:hover {
        background-color: var(--kaauh-secondary);
        color: white;
        border-color: var(--kaauh-secondary);
        text-decoration: none;
    }

    /* Outline Danger (Delete button) */
    .btn-kaats-outline-danger {
        color: var(--kaauh-danger);
        border-color: var(--kaauh-danger);
        background-color: transparent;
    }
    .btn-kaats-outline-danger:hover {
        background-color: var(--kaauh-danger);
        color: white;
        border-color: var(--kaauh-danger);
        text-decoration: none;
    }

    /* --- General UI Styles (Unchanged) --- */

    /* CARD GRID STYLES */
    .meetings-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 1.5rem;
        padding: 1rem;
    }
    .meeting-card {
        background: white;
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        padding: 1.5rem;
        border: 1px solid var(--kaauh-border);
        transition: transform 0.2s ease, box-shadow 0.2s ease;
        height: 100%;
    }
    .meeting-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.1);
    }
    
    /* TOPIC AND DETAILS STYLES */
    .meeting-topic {
        font-size: 1.15rem;
        font-weight: 600;
        color: var(--kaauh-teal-dark);
        margin-bottom: 1rem;
        border-bottom: 1px solid var(--kaauh-border);
        padding-bottom: 0.5rem;
    }
    .meeting-detail {
        display: flex;
        margin-bottom: 0.75rem;
        align-items: flex-start;
    }
    .detail-label {
        font-weight: 600;
        min-width: 80px;
        color: var(--kaauh-gray);
        font-size: 0.9rem;
        flex-shrink: 0;
    }
    .detail-value {
        flex: 1;
        font-size: 0.9rem;
        word-break: break-word;
        color: var(--kaauh-primary-text);
    }

    /* STATUS BADGE STYLES */
    .status-badge {
        display: inline-block;
        font-size: 0.75rem;
        font-weight: 700;
        padding: 0.4em 0.8em;
        border-radius: 0.4rem;
        text-align: center;
        color: white;
    }
    .bg-warning {
        background: var(--kaauh-warning) !important;
        color: var(--kaauh-primary-text) !important;
    }
    .bg-success {
        background: var(--kaauh-success) !important;
        color: white !important;
    }
    .bg-danger {
        background: var(--kaauh-danger) !important;
        color: white !important;
    }
    
    /* ACTION AREA STYLES */
    .actions {
        margin-top: 1rem;
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
    }
    
    /* Header Styling */
    .card-header h1 {
        color: var(--kaauh-teal-dark);
        font-weight: 700;
    }
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    }

    /* Empty State Icon Color */
    .text-muted.mb-3 {
        color: var(--kaauh-teal-dark) !important;
    }
    
    /* Pagination Link Styling */
    .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;
    }

    /* RESPONSIVE STYLES */
    @media (max-width: 768px) {
        .meetings-grid {
            grid-template-columns: 1fr;
        }
        .meeting-detail {
            flex-direction: column;
            gap: 0.25rem;
        }
        .detail-label {
            min-width: auto;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="card">
    <div class="card-header">
        <div class="d-flex justify-content-between align-items-center">
            <h1 class="h3 mb-0">
                {% include "icons/meeting.html" %}
                {% trans "Zoom Meetings" %}
            </h1>
            <div class="d-flex gap-2 align-items-center">
                {% include "includes/search_form.html" with search_query=search_query %}

                <a href="{% url 'create_meeting' %}" class="btn-base btn-main-action">
                    <svg class="heroicon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12 4v16m8-8H4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                    </svg>
                    {% trans "Create Meeting" %}
                </a>
            </div>
        </div>
    </div>

    {% if meetings %}
        <div class="meetings-grid">
            {% for meeting in meetings %}
                <div class="meeting-card">
                    <div class="meeting-topic">{{ meeting.topic }}</div>

                    <div class="meeting-detail">
                        <div class="detail-label">{% trans "ID" %}:</div>
                        <div class="detail-value">{{ meeting.meeting_id|default:meeting.id }}</div>
                    </div>

                    <div class="meeting-detail">
                        <div class="detail-label">{% trans "Start Time" %}:</div>
                        <div class="detail-value">{{ meeting.start_time|date:"M d, Y H:i" }}</div>
                    </div>

                    <div class="meeting-detail">
                        <div class="detail-label">{% trans "Duration" %}:</div>
                        <div class="detail-value">{{ meeting.duration }} minutes</div>
                    </div>

                    <div class="meeting-detail">
                        <div class="detail-label">{% trans "Status" %}:</div>
                        <div class="detail-value">
                            <span class="status-badge {% if meeting.status == 'waiting' %}bg-warning{% elif meeting.status == 'started' %}bg-success{% elif meeting.status == 'ended' %}bg-danger{% endif %}">
                                {% if meeting.status == 'waiting' %}
                                    {% trans "Waiting" %}
                                {% elif meeting.status == 'started' %}
                                    {% trans "Started" %}
                                {% elif meeting.status == 'ended' %}
                                    {% trans "Ended" %}
                                {% endif %}
                            </span>
                        </div>
                    </div>

                    {% if meeting.join_url %}
                        <div class="meeting-detail">
                            <div class="detail-label">{% trans "Join URL" %}:</div>
                            <div class="detail-value">
                                <a href="{{ meeting.join_url }}" target="_blank" class="btn-base btn-kaats-outline-primary btn-sm">
                                    {% trans "Join Meeting" %}
                                </a>
                            </div>
                        </div>
                    {% endif %}

                    <div class="actions">
                        <a href="{% url 'meeting_details' meeting.pk %}" class="btn-base btn-kaats-outline-primary btn-sm" title="{% trans 'View' %}">
                            <svg class="heroicon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                <path d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0 8.268-2.943-9.542-7z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                        </a>
                        <a href="{% url 'update_meeting' meeting.pk %}" class="btn-base btn-kaats-outline-secondary btn-sm" title="{% trans 'Update' %}">
                            <svg class="heroicon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                        </a>
                        <button type="button" class="btn-base btn-kaats-outline-danger btn-sm" title="{% trans 'Delete' %}"
                                data-bs-toggle="deleteModal"
                                data-delete-url="{% url 'delete_meeting' meeting.pk %}"
                                data-item-name="{{ meeting.topic }}">
                            <svg class="heroicon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                        </button>
                    </div>
                </div>
            {% endfor %}
        </div>

        {% 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={{ page_obj.previous_page_number }}&search={{ search_query }}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </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 }}&search={{ search_query }}">{{ 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 }}&search={{ search_query }}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        {% endif %}
    {% else %}
        <div class="text-center py-5">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="text-muted mb-3" style="width: 80px;">
                <path stroke-linecap="round" stroke-linejoin="round" d="M9.879 7.519c1.171-1.025 3.071-1.025 4.242 0 1.172 1.025 1.172 2.687 0 3.712-.203.179-.43.326-.67.442-.745.361-1.45.999-1.45 1.827v.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9 5.25h.008v.008H12v-.008Z" />
            </svg>
            <p class="text-muted">{% trans "No meetings found." %}</p>
            {% if user.is_staff %}
                <a href="{% url 'create_meeting' %}" class="btn-base btn-main-action mt-3">
                    <svg class="heroicon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12 4v16m8-8H4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                    </svg>
                    {% trans "Create Your First Meeting" %}
                </a>
            {% endif %}
        </div>
    {% endif %}
</div>
{% endblock %}