{% extends 'base.html' %}

{% block title %}Dashboard Penjual - UMKM Pemagarsari{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-5">
        <div>
            <h1 class="fw-bold mb-1">Dashboard Penjual</h1>
            <p class="text-muted mb-0">Kelola toko dan produk Anda di sini.</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{% url 'shop_add' %}" class="btn btn-outline-emerald rounded-pill px-4">
                <i class="fa-solid fa-shop me-2"></i>Tambah Toko
            </a>
            <a href="{% url 'product_add' %}" class="btn btn-emerald rounded-pill px-4">
                <i class="fa-solid fa-plus me-2"></i>Tambah Produk
            </a>
        </div>
    </div>

    <!-- Stats -->
    <div class="row g-4 mb-5">
        <div class="col-md-6 col-lg-3">
            <div class="card border-0 rounded-4 shadow-sm p-4">
                <div class="d-flex align-items-center mb-2">
                    <div class="bg-emerald bg-opacity-10 p-2 rounded-3 me-3">
                        <i class="fa-solid fa-shop text-emerald"></i>
                    </div>
                    <span class="text-muted small">Toko Anda</span>
                </div>
                <h3 class="fw-bold mb-0">{{ shops.count }}</h3>
            </div>
        </div>
        <div class="col-md-6 col-lg-3">
            <div class="card border-0 rounded-4 shadow-sm p-4">
                <div class="d-flex align-items-center mb-2">
                    <div class="bg-primary bg-opacity-10 p-2 rounded-3 me-3">
                        <i class="fa-solid fa-box text-primary"></i>
                    </div>
                    <span class="text-muted small">Total Produk</span>
                </div>
                <h3 class="fw-bold mb-0">{{ products.count }}</h3>
            </div>
        </div>
    </div>

    <!-- Shops Table -->
    <div class="card border-0 rounded-4 shadow-sm overflow-hidden mb-5">
        <div class="card-header bg-white py-3 border-0">
            <h5 class="fw-bold mb-0">Toko Saya</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light text-muted small uppercase">
                    <tr>
                        <th class="ps-4">Toko</th>
                        <th>WhatsApp</th>
                        <th class="text-end pe-4">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for shop in shops %}
                    <tr>
                        <td class="ps-4">
                            <div class="d-flex align-items-center">
                                <div class="rounded-3 bg-light me-3 overflow-hidden" style="width: 40px; height: 40px;">
                                    {% if shop.logo %}
                                    <img src="{{ shop.logo.url }}" class="w-100 h-100 object-fit-cover">
                                    {% else %}
                                    <div class="w-100 h-100 d-flex align-items-center justify-content-center">
                                        <i class="fa-solid fa-shop text-muted"></i>
                                    </div>
                                    {% endif %}
                                </div>
                                <span class="fw-bold">{{ shop.name }}</span>
                            </div>
                        </td>
                        <td>{{ shop.whatsapp_number }}</td>
                        <td class="text-end pe-4">
                            <a href="{% url 'shop_edit' shop.pk %}" class="btn btn-light btn-sm rounded-pill px-3">
                                <i class="fa-solid fa-pen-to-square me-1"></i> Edit
                            </a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="3" class="text-center py-4">
                            <p class="text-muted mb-0">Anda belum memiliki toko.</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Products Table -->
    <div class="card border-0 rounded-4 shadow-sm overflow-hidden">
        <div class="card-header bg-white py-3 border-0">
            <h5 class="fw-bold mb-0">Produk Saya</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light text-muted small uppercase">
                    <tr>
                        <th class="ps-4">Produk</th>
                        <th>Toko</th>
                        <th>Kategori</th>
                        <th>Harga</th>
                        <th>Stok</th>
                        <th class="text-end pe-4">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for product in products %}
                    <tr>
                        <td class="ps-4">
                            <div class="d-flex align-items-center">
                                <div class="rounded-3 bg-light me-3 overflow-hidden" style="width: 48px; height: 48px;">
                                    {% if product.image %}
                                    <img src="{{ product.image.url }}" class="w-100 h-100 object-fit-cover">
                                    {% else %}
                                    <div class="w-100 h-100 d-flex align-items-center justify-content-center">
                                        <i class="fa-solid fa-image text-muted"></i>
                                    </div>
                                    {% endif %}
                                </div>
                                <span class="fw-bold">{{ product.name }}</span>
                            </div>
                        </td>
                        <td>{{ product.shop.name }}</td>
                        <td><span class="badge bg-light text-dark rounded-pill">{{ product.category.name }}</span></td>
                        <td>Rp {{ product.price|floatformat:0 }}</td>
                        <td>{{ product.stock }}</td>
                        <td class="text-end pe-4">
                            <div class="dropdown">
                                <button class="btn btn-light btn-sm rounded-circle shadow-none" type="button" data-bs-toggle="dropdown">
                                    <i class="fa-solid fa-ellipsis-vertical"></i>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-end border-0 shadow-sm rounded-3">
                                    <li><a class="dropdown-item" href="{% url 'product_edit' product.pk %}"><i class="fa-solid fa-pen-to-square me-2 text-muted"></i> Edit</a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li>
                                        <form action="{% url 'product_delete' product.pk %}" method="POST" onsubmit="return confirm('Yakin ingin menghapus produk ini?')">
                                            {% csrf_token %}
                                            <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i> Hapus</button>
                                        </form>
                                    </li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center py-5">
                            <p class="text-muted mb-3">Anda belum menambahkan produk.</p>
                            <a href="{% url 'product_add' %}" class="btn btn-emerald btn-sm rounded-pill px-4">Tambah Sekarang</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
