{% extends 'base.html' %}

{% block title %}Learning Resources - LMS{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Learning Resources</h2>
        {% if is_trainer %}
        <a href="{% url 'add_resource' %}" class="btn btn-primary">
            <i class="fas fa-plus"></i> Add Resource
        </a>
        {% endif %}
    </div>

    {% if messages %}
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show">
            {{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        {% endfor %}
    {% endif %}

    <div class="row">
        {% for resource in resources %}
        <div class="col-md-6 col-lg-4 mb-4">
            <div class="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">
                        <span class="badge bg-{% if resource.resource_type == 'document' %}primary{% elif resource.resource_type == 'video' %}danger{% elif resource.resource_type == 'link' %}success{% else %}secondary{% endif %}">
                            {{ resource.get_resource_type_display }}
                        </span>
                        {% if not resource.is_published %}
                        <span class="badge bg-warning">Draft</span>
                        {% endif %}
                    </div>
                    
                    <h5 class="card-title">{{ resource.title }}</h5>
                    <p class="card-text text-muted" title="{{ resource.description }}">
                        {{ resource.description|truncatewords:20 }}
                    </p>
                    
                    <div class="mt-auto">
                        <small class="text-muted">
                            Uploaded by: {{ resource.uploaded_by.username }}<br>
                            {{ resource.created_at|date:"M d, Y" }}
                        </small>
                    </div>
                </div>
                <div class="card-footer bg-transparent">
                    <div class="btn-group w-100">
                        <a href="{% url 'resource_detail' resource.pk %}" class="btn btn-outline-primary btn-sm">
                            <i class="fas fa-eye"></i> View
                        </a>
                        {% if resource.file %}
                        <a href="{% url 'download_resource' resource.pk %}" class="btn btn-outline-success btn-sm">
                            <i class="fas fa-download"></i> Download
                        </a>
                        {% elif resource.url %}
                        <a href="{{ resource.url }}" target="_blank" class="btn btn-outline-info btn-sm">
                            <i class="fas fa-external-link-alt"></i> Open Link
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% empty %}
        <div class="col-12">
            <div class="text-center py-5">
                <i class="fas fa-book fa-3x text-muted mb-3"></i>
                <h4>No resources available</h4>
                <p class="text-muted">
                    {% if is_trainer %}
                        Get started by adding your first resource.
                    {% else %}
                        Check back later for learning materials.
                    {% endif %}
                </p>
                {% if is_trainer %}
                <a href="{% url 'add_resource' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i> Add First Resource
                </a>
                {% endif %}
            </div>
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}
