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

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

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

{% block content %}
{% with products=page_obj %}
<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>
    <!-- Header + primary actions -->
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">Products</h5>
            <div class="text-muted small">
                {{ page_obj.paginator.count }} products total
            </div>
        </div>
        <div class="d-flex flex-wrap gap-2">
            {% if show_hidden == 'true' %}
                <a href="{% url 'inventory:product_list' %}" class="btn btn-outline-info btn-sm d-flex align-items-center gap-2">
                    <i class="fas fa-eye"></i> Show Active
                </a>
            {% else %}
                <a href="{% url 'inventory:product_list' %}?hidden=true" class="btn btn-outline-warning btn-sm d-flex align-items-center gap-2">
                    <i class="fas fa-eye-slash"></i> View Hidden
                </a>
            {% endif %}
            <a href="{% url 'inventory:stock_adjustment_history' %}"
               class="btn btn-outline-secondary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-rotate"></i> Stock history
            </a>
            <a href="{% url 'inventory:add_product' %}"
               class="btn btn-primary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-plus"></i> Add Product
            </a>
        </div>
    </div>

    <!-- Filters / search -->
    <form method="get" class="card mb-3">
        <div class="card-body row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label small fw-semibold">Search</label>
                <input type="text"
                       name="q"
                       value="{{ query }}"
                       class="form-control"
                       placeholder="Search by name, SKU or barcode">
            </div>
            <div class="col-md-3">
                <label class="form-label small fw-semibold">Category</label>
                <select name="category" class="form-select">
                    <option value="">All categories</option>
                    {% for cat in categories %}
                        <option value="{{ cat.id }}" {% if selected_category|stringformat:'s' == cat.id|stringformat:'s' %}selected{% endif %}>
                            {{ cat.name }}
                        </option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label small fw-semibold">Stock status</label>
                <select name="stock_status" class="form-select">
                    <option value="">All</option>
                    <option value="in_stock" {% if selected_stock_status == 'in_stock' %}selected{% endif %}>In stock</option>
                    <option value="low_stock" {% if selected_stock_status == 'low_stock' %}selected{% endif %}>Low stock</option>
                    <option value="out_of_stock" {% if selected_stock_status == 'out_of_stock' %}selected{% endif %}>Out of stock</option>
                </select>
            </div>
            <div class="col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary w-100 btn-sm">
                    <i class="fas fa-search me-1"></i> Filter
                </button>
                <a href="{% url 'inventory:product_list' %}" class="btn btn-outline-secondary btn-sm">
                    Reset
                </a>
            </div>
        </div>
    </form>

    <!-- Products table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="sp-table-contaner">
                <table class="table align-middle mb-0">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th class="d-none d-md-table-cell">SKU</th>
                        <th class="d-none d-lg-table-cell">Category</th>
                        <th class="text-end">Stock</th>
                        <th class="text-end d-none d-md-table-cell">Selling Price</th>
                        <th class="text-center">Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if products %}
                        {% for product in products %}
                            <tr>
                                <td>
                                    <a href="{% url 'inventory:product_detail' product.id %}"
                                       class="fw-semibold text-decoration-none">
                                        {{ product.name }}
                                    </a>
                                    <div class="small text-muted d-md-none">
                                        SKU: {{ product.sku }}
                                    </div>
                                </td>
                                <td class="d-none d-md-table-cell">
                                    <span class="small text-muted">{{ product.sku }}</span>
                                </td>
                                <td class="d-none d-lg-table-cell">
                                    {{ product.category.name|default:'Uncategorized' }}
                                </td>
                                <td class="text-end">
                                    {{ product.stock_quantity }}
                                </td>
                                <td class="text-end d-none d-md-table-cell">
                                    {% money product.price %}
                                </td>
                                <td class="text-center">
                                    {% if product.is_hidden %}
                                        <span class="badge bg-secondary text-light">Hidden</span>
                                    {% elif product.stock_status == 'in_stock' %}
                                        <span class="badge bg-success-subtle text-success border border-success-subtle">
                                            In stock
                                        </span>
                                    {% elif product.stock_status == 'low_stock' %}
                                        <span class="badge bg-warning text-dark">
                                            Low stock
                                        </span>
                                    {% else %}
                                        <span class="badge bg-danger text-light">
                                            Out of stock
                                        </span>
                                    {% endif %}
                                </td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{% url 'inventory:product_detail' product.id %}"
                                           class="btn btn-outline-secondary"
                                           title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'inventory:edit_product' product.id %}"
                                           class="btn btn-outline-secondary"
                                           title="Edit">
                                            <i class="fas fa-pen"></i>
                                        </a>
                                        <form method="POST" action="{% url 'inventory:toggle_product_visibility' product.id %}" style="display: inline;">
                                            {% csrf_token %}
                                            <button type="submit" class="btn btn-outline-secondary" title="{% if product.is_hidden %}Unhide{% else %}Hide{% endif %}">
                                                <i class="fas {% if product.is_hidden %}fa-eye{% else %}fa-eye-slash{% endif %}"></i>
                                            </button>
                                        </form>
                                        <!--<a href="{% url 'inventory:restock_product' product.id %}"-->
                                        <!--   class="btn btn-outline-secondary"-->
                                        <!--   title="Restock">-->
                                        <!--    <i class="fas fa-plus"></i>-->
                                        <!--</a>-->
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="7" class="text-center text-muted small py-4">
                                No products found. Try adjusting your filters or
                                <a href="{% url 'inventory:add_product' %}">add a new product</a>.
                            </td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            {% if products.has_other_pages %}
                <div class="px-3 py-2 border-top d-flex justify-content-between align-items-center small">
                    <span>
                        Page {{ products.number }} of {{ products.paginator.num_pages }}
                    </span>
                    <nav>
                        <ul class="pagination pagination-sm mb-0">
                            {% if products.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ products.previous_page_number }}&q={{ query }}&category={{ selected_category }}&stock_status={{ selected_stock_status }}">
                                        Prev
                                    </a>
                                </li>
                            {% endif %}
                            {% for num in products.paginator.page_range %}
                                {% if num == products.number %}
                                    <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                                {% elif num > products.number|add:'-3' and num < products.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}&q={{ query }}&category={{ selected_category }}&stock_status={{ selected_stock_status }}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            {% if products.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ products.next_page_number }}&q={{ query }}&category={{ selected_category }}&stock_status={{ selected_stock_status }}">
                                        Next
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endwith %}
{% endblock %}