{% 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 -->
        <div class="row">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <div>
                        <h1 class="h2 mb-2">{{ pageTitle }}</h1>
                        {% if pageDescription %}
                            <p class="text-muted mb-0">{{ pageDescription }}</p>
                        {% endif %}
                    </div>
                    <div>
                        <a href="/dashboard/{{ user.id }}" class="btn back-button">
                            <i class="fas fa-arrow-left me-2"></i>Back to Dashboard
                        </a>
                    </div>
                </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.status and item.status is string %}
                            <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 Details -->
                            <div class="mt-auto">
                                {% if item.viewCount %}
                                    <small class="text-muted d-block mb-1">
                                        <i class="fas fa-eye me-1"></i>{{ item.viewCount }} views
                                    </small>
                                {% endif %}
                                {% if item.createdAt %}
                                    <small class="text-muted d-block mb-3">
                                        <i class="fas fa-calendar me-1"></i>{{ item.createdAt|date('M j, Y') }}
                                    </small>
                                {% endif %}

                                <!-- Action Buttons -->
                                <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="Page navigation" class="mt-5">
                    <ul class="pagination justify-content-center">
                        {% for page in 1..totalPages %}
                            <li class="page-item {{ page == currentPage ? 'active' : '' }}">
                                <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 fa-3x text-muted mb-3"></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>
{% endblock %}