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

{% block title %}{% trans "Training Materials" %} - {{ block.super }}{% endblock %}

{% block customCSS %}
<style>
    /* ================================================= */
    /* THEME VARIABLES AND GLOBAL STYLES (FROM JOB DETAIL) */
    /* ================================================= */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
    }

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

    /* Main Action Button Style (Applied to .btn-primary) */
    .btn-main-action, .btn-primary {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        padding: 0.6rem 1.2rem;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none; /* Ensure links look like buttons */
    }
    .btn-main-action:hover, .btn-primary:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }
    
    /* Outlined Button Styles for Table Actions */
    .btn-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }
    .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);
    }
    /* Danger button remains standard for deletion */

    /* Card enhancements */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        background-color: white;
    }
    
    /* Colored Header Card */
    .list-header-card {
        background: linear-gradient(135deg, var(--kaauh-teal), #004d57);
        color: white;
        border-radius: 0.75rem 0.75rem 0 0;
        padding: 1.5rem;
        box-shadow: 0 4px 10px rgba(0,0,0,0.15);
    }
    .list-header-card h1 {
        font-weight: 700;
        margin: 0;
        font-size: 1.8rem;
    }
    .heroicon {
        width: 1.25rem;
        height: 1.25rem;
        vertical-align: text-bottom;
        stroke: currentColor;
        margin-right: 0.5rem;
    }

    /* Table Styling */
    .table thead th {
        background-color: #f8f9fa;
        color: var(--kaauh-primary-text);
        font-weight: 600;
        border-bottom: 2px solid var(--kaauh-border);
    }
    .table tbody tr:hover {
        background-color: #f0f8ff; /* Light hover color */
    }
    .btn-group .btn-sm {
        padding: 0.35rem 0.6rem;
    }
    
    /* Pagination Styling */
    .pagination .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;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="card shadow-sm">
        
        <div class="list-header-card">
            <div class="d-flex justify-content-between align-items-center flex-wrap">
                <h1 class="h3 mb-0">
                    <i class="fas fa-graduation-cap me-2"></i>
                    {% trans "Training Materials" %}
                </h1>
                <div class="d-flex gap-3 align-items-center mt-2 mt-md-0">
                    
                    <div class="order-3 order-md-1">
                        {% include "includes/search_form.html" with search_query=search_query %}
                    </div>

                    {% if user.is_authenticated %}
                        <a href="{% url 'training_create' %}" class="btn btn-main-action btn-sm order-1 order-md-2" title="{% trans 'Add New Material' %}">
                            <i class="fas fa-plus"></i>
                            <span class="d-none d-sm-inline">{% trans "Add New Material" %}</span>
                        </a>
                    {% endif %}
                </div>
            </div>
        </div>
    

        {% if materials %}
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th scope="col">{% trans "Title" %}</th>
                            <th scope="col">{% trans "Created By" %}</th>
                            <th scope="col">{% trans "Created" %}</th>
                            <th scope="col" class="text-center">{% 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-center">
                                    <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="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>

            {% if is_paginated %}
                <div class="card-footer bg-light border-top">
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center mb-0">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}" 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 }}">{{ 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 }}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
            {% endif %}
            
        {% else %}
            <div class="text-center py-5">
                <i class="fas fa-book-open fa-3x text-muted mb-3"></i>
                <h5 class="mb-3">{% trans "No training materials found." %}</h5>
                <p class="text-muted mb-4">{% 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">
                        <i class="fas fa-plus me-1"></i>
                        {% trans "Create Your First Material" %}
                    </a>
                {% endif %}
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}