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

{% block title %}
    {% if category %}{{ category.name }}{% else %}Semua Produk{% endif %} - Pasar UMKM Pemagarsari
{% endblock %}

{% block content %}
<section class="py-4 py-md-5 bg-white border-bottom shadow-sm">
    <div class="container py-2 text-center">
        <h1 class="mb-3">{% if category %}{{ category.name }}{% else %}Semua Produk{% endif %}</h1>
        <p class="lead text-muted mx-auto max-width-700">Jelajahi berbagai pilihan produk terbaik dari UMKM Desa Pemagarsari.</p>
    </div>
</section>

<section class="py-4 py-md-5">
    <div class="container">
        <div class="row g-4">
            <!-- Sidebar / Category List -->
            <div class="col-lg-3">
                <!-- Mobile Category Horizontal Scroll -->
                <div class="d-lg-none mb-4 overflow-x-auto pb-2" style="white-space: nowrap;">
                    <a href="{% url 'product_list' %}" class="btn {% if not category %}btn-primary{% else %}btn-outline-primary{% endif %} btn-sm me-2 rounded-pill px-3">
                        Semua
                    </a>
                    {% for cat in categories %}
                    <a href="{% url 'product_list_by_category' cat.slug %}" class="btn {% if category.id == cat.id %}btn-primary{% else %}btn-outline-primary{% endif %} btn-sm me-2 rounded-pill px-3">
                        {{ cat.name }}
                    </a>
                    {% endfor %}
                </div>

                <!-- Desktop Sidebar -->
                <div class="card shadow-sm border-0 sticky-top d-none d-lg-block" style="top: 100px;">
                    <div class="card-body p-4">
                        <h5 class="mb-4">Kategori</h5>
                        <div class="list-group list-group-flush">
                            <a href="{% url 'product_list' %}" class="list-group-item list-group-item-action border-0 px-0 {% if not category %}text-primary fw-bold{% else %}text-muted{% endif %}">
                                <i class="fas fa-th-large me-2"></i> Semua Kategori
                            </a>
                            {% for cat in categories %}
                            <a href="{% url 'product_list_by_category' cat.slug %}" class="list-group-item list-group-item-action border-0 px-0 {% if category.id == cat.id %}text-primary fw-bold{% else %}text-muted{% endif %}">
                                <i class="fas {{ cat.icon|default:'fa-tag' }} me-2"></i> {{ cat.name }}
                            </a>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- Product Grid -->
            <div class="col-lg-9">
                <div class="row g-3 g-md-4">
                    {% if products %}
                        {% for product in products %}
                        <div class="col-6 col-md-4">
                            <div class="card h-100 shadow-sm">
                                <div class="position-relative">
                                    {% if product.image %}
                                        <img src="{{ product.image.url }}" class="card-img-top product-img-list" alt="{{ product.name }}" >
                                    {% else %}
                                        <div class="bg-secondary-subtle d-flex align-items-center justify-content-center product-img-list" >
                                            <i class="fas fa-image fa-2x text-muted opacity-50"></i>
                                        </div>
                                    {% endif %}
                                    <span class="badge bg-white text-primary position-absolute top-0 end-0 m-2 shadow-sm border-0 d-none d-md-inline-block">{{ product.category.name }}</span>
                                </div>
                                <div class="card-body p-3 p-md-4 d-flex flex-column">
                                    <h5 class="card-title mb-1">
                                        <a href="{% url 'product_detail' product.slug %}" class="text-decoration-none text-dark">{{ product.name }}</a>
                                    </h5>
                                    <p class="small text-muted mb-3 d-none d-md-block"><i class="fas fa-store me-1"></i> {{ product.vendor.name }}</p>
                                    <div class="mt-auto">
                                        <div class="d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center gap-2">
                                            <span class="product-price">Rp {{ product.price|floatformat:0 }}</span>
                                            <a href="{{ product.get_whatsapp_url }}" target="_blank" class="btn btn-sm wa-btn shadow-sm">
                                                <i class="fab fa-whatsapp me-1"></i> Beli
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="col-12 text-center py-5">
                            <i class="fas fa-box-open fa-4x text-muted mb-4 opacity-50"></i>
                            <h3 class="text-muted">Maaf, belum ada produk yang tersedia.</h3>
                            <p class="text-muted">Coba cari di kategori lain atau kembali lagi nanti.</p>
                            <a href="{% url 'product_list' %}" class="btn btn-primary mt-3 px-4">Lihat Semua Produk</a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}