{% set current_theme = current_theme() %} {% from "macros/form_errors.j2" import render_field_errors, render_form_errors %}

<!doctype html>
<html lang="{{ current_locale() }}" class="h-100">
    <head>
        {{ current_theme.headertags() }}
        <title>Cupones - {{ curso.nombre }}</title>
        {{ current_theme.local_style() }}
    </head>

    <body>
        {{ current_theme.navbar() }}

        <main>
            <div class="container-fluid">
                <div class="d-flex justify-content-between align-items-center py-3">
                    <h1 class="h3">Cupones del {{ _("Curso") }}: {{ curso.nombre }}</h1>
                    <a href="{{ url_for('course.create_coupon', course_code=curso.codigo) }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> {{ _("Crear") }} {{ _("Nuevo") }} Cupón
                    </a>
                </div>

                {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in
                messages %}
                <div
                    class="alert alert-{{ 'danger' if category == 'error' else category }} alert-dismissible fade show"
                    role="alert"
                >
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
                {% endfor %} {% endif %} {% endwith %} {% if coupons %}
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Código</th>
                                        <th>{{ _("Tipo") }}</th>
                                        <th>Descuento</th>
                                        <th>Usos</th>
                                        <th>Expira</th>
                                        <th>{{ _("Estado") }}</th>
                                        <th>{{ _("Acciones") }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for coupon in coupons %}
                                    <tr>
                                        <td><strong>{{ coupon.code }}</strong></td>
                                        <td>
                                            {% if coupon.discount_type == 'percentage' %}
                                            <span class="badge bg-primary">Porcentaje</span>
                                            {% else %}
                                            <span class="badge bg-info">Fijo</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if coupon.discount_type == 'percentage' %} {{ coupon.discount_value }}% {% else
                                            %} ${{ coupon.discount_value }} {% endif %}
                                        </td>
                                        <td>
                                            {{ coupon.current_uses }} {% if coupon.max_uses %} / {{ coupon.max_uses }} {% else
                                            %} / ∞ {% endif %}
                                        </td>
                                        <td>
                                            {% if coupon.expires_at %} {{ coupon.expires_at.strftime('%d/%m/%Y') }} {% else %}
                                            Sin expiración {% endif %}
                                        </td>
                                        <td>
                                            {% set is_valid, error_msg = coupon.is_valid() %} {% if is_valid %}
                                            <span class="badge bg-success">{{ _("Activo") }}</span>
                                            {% else %}
                                            <span class="badge bg-danger">{{ error_msg }}</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a
                                                    href="{{ url_for('course.edit_coupon', course_code=curso.codigo, coupon_id=coupon.id) }}"
                                                    class="btn btn-sm btn-outline-primary"
                                                >
                                                    <i class="bi bi-pencil"></i> {{ _("Editar") }}
                                                </a>
                                                <form
                                                    method="post"
                                                    action="{{ url_for('course.delete_coupon', course_code=curso.codigo, coupon_id=coupon.id) }}"
                                                    style="display: inline"
                                                    onsubmit="return confirm('¿Está seguro de que desea eliminar este cupón?')"
                                                >
                                                    <button type="submit" class="btn btn-sm btn-outline-danger">
                                                        <i class="bi bi-trash"></i> {{ _("Eliminar") }}
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                {% else %}
                <div class="card">
                    <div class="card-body text-center">
                        <h5>No hay cupones creados</h5>
                        <p class="text-muted">Cree el primer cupón de descuento para este curso.</p>
                        <a href="{{ url_for('course.create_coupon', course_code=curso.codigo) }}" class="btn btn-primary">
                            <i class="bi bi-plus-circle"></i> {{ _("Crear") }} Primer Cupón
                        </a>
                    </div>
                </div>
                {% endif %}

                <div class="mt-3">
                    <a href="{{ url_for('course.administrar_curso', course_code=curso.codigo) }}" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Volver al {{ _("Curso") }}
                    </a>
                </div>
            </div>
        </main>
    </body>
</html>
