{% extends "core/base.html" %}
{% load currency_filters %}
{% block title %}Promo Codes - SwiftPOS{% endblock %}
{% block topbar_title %}Promo Codes{% endblock %}

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

    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h5 class="mb-0 fw-bold">Promo Codes</h5>
            <div class="small text-muted">Create and manage discount codes for your POS checkout</div>
        </div>
        <a href="{% url 'promo_create' %}" class="btn btn-primary btn-sm d-flex align-items-center gap-2">
            <i class="fas fa-plus"></i> New Code
        </a>
    </div>

    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Code</th>
                            <th>Description</th>
                            <th>Discount</th>
                            <th>Min Order</th>
                            <th>Uses</th>
                            <th>Expires</th>
                            <th class="text-center">Status</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for p in promos %}
                        <tr>
                            <td>
                                <code class="fw-bold fs-6">{{ p.code }}</code>
                            </td>
                            <td class="text-muted small">{{ p.description|default:"—" }}</td>
                            <td>
                                {% if p.discount_type == 'percentage' %}
                                <span class="badge bg-info-subtle text-info border border-info-subtle">
                                    {{ p.discount_value }}% off
                                </span>
                                {% else %}
                                <span class="badge bg-success-subtle text-success border border-success-subtle">
                                    {% money p.discount_value %} off
                                </span>
                                {% endif %}
                            </td>
                            <td class="small">
                                {% if p.minimum_order_amount > 0 %}
                                    {{ currency_symbol }}{{ p.minimum_order_amount|floatformat:0 }}
                                {% else %}
                                    <span class="text-muted">None</span>
                                {% endif %}
                            </td>
                            <td class="small">
                                {{ p.current_uses }}{% if p.max_uses > 0 %} / {{ p.max_uses }}{% else %} / ∞{% endif %}
                            </td>
                            <td class="small">
                                {% if p.expires_at %}
                                    {{ p.expires_at|date:"d M Y" }}
                                    {% if p.is_expired %}<span class="badge bg-danger ms-1">Expired</span>{% endif %}
                                {% else %}
                                    <span class="text-muted">Never</span>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                {% if p.is_valid %}
                                <span class="badge bg-success">Active</span>
                                {% elif p.is_expired %}
                                <span class="badge bg-secondary">Expired</span>
                                {% elif p.is_exhausted %}
                                <span class="badge bg-warning text-dark">Exhausted</span>
                                {% else %}
                                <span class="badge bg-danger">Inactive</span>
                                {% endif %}
                            </td>
                            <td class="text-end">
                                <form method="post" action="{% url 'promo_toggle' p.pk %}" class="d-inline">
                                    {% csrf_token %}
                                    <button class="btn btn-sm btn-outline-{% if p.is_active %}warning{% else %}success{% endif %}" type="submit">
                                        {% if p.is_active %}Deactivate{% else %}Activate{% endif %}
                                    </button>
                                </form>
                                <form method="post" action="{% url 'promo_delete' p.pk %}" class="d-inline"
                                      onsubmit="return confirm('Delete promo code {{ p.code }}?');">
                                    {% csrf_token %}
                                    <button class="btn btn-sm btn-outline-danger ms-1" type="submit">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted">
                                <i class="fas fa-tags fa-2x d-block mb-2 opacity-25"></i>
                                No promo codes yet. <a href="{% url 'promo_create' %}">Create one</a>.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
