{% extends 'core/base.html' %}
{% load static %}

{% block title %}Categories - SwiftPOS{% endblock %}

{% block topbar_title %}
Inventory - Categories
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">Categories</h5>
            <div class="text-muted small">
                {{ categories|length }} category{{ categories|length|pluralize }}
            </div>
        </div>

        <div class="d-flex flex-wrap gap-2">
            <a href="{% url 'inventory:product_list' %}"
               class="btn btn-outline-secondary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-box"></i> Products
            </a>
            <!-- NEW: Add Category button -->
            <a href="{% url 'inventory:add_category' %}"
               class="btn btn-primary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-plus"></i> Add Category
            </a>
        </div>
    </div>

    <div class="card">
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table align-middle mb-0 table-hover">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <!-- <th>Description</th> -->
                        <th>Products</th>
                        <th>Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if categories %}
                        {% for category in categories %}
                            <tr>
                                <td>
                                    <span class="fw-semibold">{{ category.name }}</span>
                                </td>
                               <!--  <td class="d-none d-md-table-cell">
                                    <span class="small text-muted">
                                        {{ category.description|default:"-" }}
                                    </span>
                                </td> -->
                                <td class="text-center">
                                    <span class="badge bg-light text-muted border">
                                        {{ category.product_count|default:category.products.count }} items
                                    </span>
                                </td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm">
                                        <a href="{% url 'inventory:product_list' %}?category={{ category.id }}"
                                           class="btn btn-outline-secondary"
                                           title="View products">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="#"
                                           class="btn btn-outline-secondary"
                                           title="Edit category">
                                            <i class="fas fa-pen"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="4" class="text-center text-muted small py-4">
                                No categories created yet.
                            </td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}