{% extends "layout.html.twig" %}

{% block title %}{{ pageTitle }} - Local Lendables{% endblock %}

{% block head %}
<style>
.item-card {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.item-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.status-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1;
}

.item-image {
    height: 200px;
    object-fit: cover;
    background-color: #f8f9fa;
    max-width: 100px !important;
    max-height: 100px !important;
    width: 100px !important;
    height: 100px !important;
}

.back-button {
    background-color: #6c757d;
    border-color: #6c757d;
    color: white;
}

.back-button:hover {
    background-color: #5a6268;
    border-color: #545b62;
    color: white;
}
</style>
{% endblock %}

{% block content %}
<!-- Main Content -->
<main class="content-wrapper">
    <section class="container pt-4">
        <!-- Header Section -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="mb-3">
                    <h1 class="h2 mb-2">{{ pageTitle }}</h1>
                    {% if pageDescription %}
                        <p class="text-muted mb-0">{{ pageDescription }}</p>
                    {% endif %}
                </div>
                
                {% if items|length > 0 %}
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        Showing {{ items|length }} item{{ items|length != 1 ? 's' : '' }}
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- Items Grid -->
        {% if items|length > 0 %}
            <div class="row g-4">
                {% for item in items %}
                <div class="col-12 col-md-6 col-lg-4 col-xl-3">
                    <div class="card border-0 shadow-sm item-card h-100 position-relative">
                        <!-- Status Badge -->
                        {% if item.isRequest %}
                            <span class="badge status-badge bg-warning text-dark">
                                Wish List
                            </span>
                        {% elseif item.status and item.status != true and item.status != false %}
                            <span class="badge status-badge
                                {% if item.status == 'available' %}bg-success
                                {% elseif item.status == 'on_loan' %}bg-warning text-dark
                                {% elseif item.status == 'pending' %}bg-info
                                {% else %}bg-secondary{% endif %}">
                                {{ item.status|title }}
                            </span>
                        {% endif %}

                        <!-- Item Image -->
                        {% if item.isRequest %}
                            <!-- For "asked for" items, always use the Ask For logo -->
                            <div class="card-img-top item-image d-flex align-items-center justify-content-center bg-light">
                                <img src="/lendables/logo_askfor_600.png" 
                                     alt="Ask For" 
                                     style="max-width: 100px; max-height: 100px; object-fit: contain;">
                            </div>
                        {% else %}
                            <!-- For regular lendable items, try to load their actual image -->
                            {% set itemImage = item.thumbnailLogo ?: item.primaryLogo ?: item.imageUrl %}
                            {% if itemImage and itemImage is string and itemImage != 'true' and itemImage|length > 3 %}
                                <img src="/lendable_items/{{ itemImage }}" 
                                     class="card-img-top item-image" 
                                     alt="{{ item.name }}"
                                     onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
                                <div class="card-img-top item-image d-none align-items-center justify-content-center bg-light">
                                    <i class="fas fa-image text-muted" style="font-size: 2rem;"></i>
                                </div>
                            {% else %}
                                <!-- No valid image available, show placeholder -->
                                <div class="card-img-top item-image d-flex align-items-center justify-content-center bg-light">
                                    <i class="fas fa-image text-muted" style="font-size: 2rem;"></i>
                                </div>
                            {% endif %}
                        {% endif %}

                        <div class="card-body d-flex flex-column">
                            <h5 class="card-title mb-2">{{ item.name }}</h5>
                            
                            {% if item.categoryName %}
                                <p class="text-muted small mb-2">
                                    <i class="fas fa-tag me-1"></i>{{ item.categoryName }}
                                </p>
                            {% endif %}

                            {% if item.description %}
                                <p class="card-text text-muted small mb-3">
                                    {{ item.description|length > 100 ? item.description|slice(0, 100) ~ '...' : item.description }}
                                </p>
                            {% endif %}

                            <!-- Item Stats -->
                            <div class="small text-muted mb-3">
                                {% if item.viewCount %}
                                    <span class="me-3">
                                        <i class="fas fa-eye me-1"></i>{{ item.viewCount }} views
                                    </span>
                                {% endif %}
                                {% if item.createdAt %}
                                    <span>
                                        <i class="fas fa-calendar me-1"></i>{{ item.createdAt|date('M j, Y') }}
                                    </span>
                                {% endif %}
                            </div>

                            <!-- Action Buttons -->
                            <div class="mt-auto">
                                <div class="d-flex gap-2">
                                    <a href="/item/{{ item.id }}" class="btn btn-outline-primary btn-sm flex-fill">
                                        <i class="fas fa-eye me-1"></i>View
                                    </a>
                                    {% if canEdit %}
                                        <a href="/edit-item/{{ item.id }}" class="btn btn-outline-success btn-sm flex-fill">
                                            <i class="fas fa-edit me-1"></i>Edit
                                        </a>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>

            <!-- Pagination (if needed) -->
            {% if totalPages > 1 %}
                <nav aria-label="Items pagination" class="mt-5">
                    <ul class="pagination justify-content-center">
                        {% for page in 1..totalPages %}
                            <li class="page-item {% if page == currentPage %}active{% endif %}">
                                <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <!-- Empty State -->
            <div class="text-center py-5">
                <i class="fas fa-box-open text-muted mb-3" style="font-size: 4rem;"></i>
                <h3 class="text-muted mb-3">{{ emptyStateTitle ?: 'No Items Found' }}</h3>
                <p class="text-muted mb-4">{{ emptyStateDescription ?: 'No items match your current criteria.' }}</p>
                
                {% if showCreateButton %}
                    <a href="/create-item" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>Create Your First Item
                    </a>
                {% endif %}
            </div>
        {% endif %}
    </section>
</main>

{% include 'common-footer.html.twig' %}
{% endblock %}