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

{% block title %}{% trans "Training Materials" %} - {{ 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; /* Added for hover/background consistency */
    }

    /* 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;
    }
    /* Standard card hover effect for list items */
    .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);
    }
    .btn-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }

    /* Training Card Specifics */
    .training-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }

    /* Table Styling (Consistent with Reference) */
    .table-view .table thead th {
        background-color: var(--kaauh-teal-dark); /* Dark header background */
        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;
    }
    /* Override for the primary text color being used for card-titles in table */
    .text-primary { color: var(--kaauh-teal) !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-graduation-cap me-2"></i> {% trans "Training Materials" %}
        </h1>
        {% if user.is_authenticated %}
            <a href="{% url 'training_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Material" %}
            </a>
        {% endif %}
    </div>

    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <label for="search" class="form-label small text-muted">{% trans "Search by Title or Creator" %}</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-6 d-flex align-items-end">
                    {# Additional Filters can go here if needed #}
                </div>
            </div>
        </div>
    </div>
    {% if materials %}
        <div id="training-materials-list">
            {# View Switcher - list_id must match the container ID #}
            {% include "includes/_list_view_switcher.html" with list_id="training-materials-list" %}

            {# Card View (Default) - Must have 'row' class for grid layout #}
            <div class="card-view active row">
                {% for material in materials %}
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card training-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">{{ material.title }}</h5>
                                </div>
                                
                                <p class="card-text text-muted small">
                                    <i class="fas fa-user-edit"></i> {% trans "Created By" %}: {{ material.created_by.username|default:"Anonymous" }}<br>
                                    <i class="fas fa-calendar-alt"></i> {% trans "Created On" %}: {{ material.created_at|date:"M d, Y" }}
                                </p>

                                <div class="mt-auto pt-2 border-top">
                                   <div class="d-flex gap-2">
                                        <a href="{% url 'training_detail' material.pk %}" class="btn btn-sm btn-main-action">
                                            <i class="fas fa-eye"></i> {% trans "View" %}
                                        </a>
                                        {% if user.is_authenticated and material.created_by == user %}
                                            <a href="{% url 'training_update' material.pk %}" class="btn btn-sm btn-outline-secondary">
                                                <i class="fas fa-edit"></i> {% trans "Edit" %}
                                            </a>
                                            <button type="button" class="btn btn-outline-danger btn-sm" title="{% trans 'Delete' %}"
                                                    data-bs-toggle="modal" data-bs-target="#deleteModal" {# Assuming standard Bootstrap modal trigger #}
                                                    data-delete-url="{% url 'training_delete' material.pk %}"
                                                    data-item-name="{{ material.title }}">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>

            {# Table View #}
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th scope="col" style="width: 40%;">{% trans "Title" %}</th>
                                <th scope="col" style="width: 30%;">{% trans "Created By" %}</th>
                                <th scope="col" style="width: 20%;">{% trans "Created" %}</th>
                                <th scope="col" style="width: 10%;" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for material in materials %}
                                <tr>
                                    <td><strong class="text-primary">{{ material.title }}</strong></td>
                                    <td>{{ material.created_by.username|default:"Anonymous" }}</td>
                                    <td>{{ material.created_at|date:"M d, Y" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'training_detail' material.pk %}" class="btn btn-outline-primary" title="{% trans 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if user.is_authenticated and material.created_by == user %}
                                                <a href="{% url 'training_update' material.pk %}" class="btn btn-outline-secondary" title="{% trans 'Edit' %}">
                                                    <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 'training_delete' material.pk %}"
                                                        data-item-name="{{ material.title }}">
                                                    <i class="fas fa-trash-alt"></i>
                                                </button>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# Pagination (Standardized to Reference) #}
        {% 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 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 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 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 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-graduation-cap fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No training materials found" %}</h3>
                <p class="text-muted">{% trans "It looks like there are no materials yet. Start by adding one!" %}</p>
                {% if user.is_authenticated %}
                    <a href="{% url 'training_create' %}" class="btn btn-main-action mt-3">
                        <i class="fas fa-plus me-1"></i> {% trans "Create Your First Material" %}
                    </a>
                {% endif %}
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}