@extends('layouts.app')

@section('content')
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h1 class="h3 mb-0">Cupones de descuento</h1>
        <a href="{{ route('cupones.create') }}" class="btn btn-primary">Nuevo cupón</a>
    </div>

    @if (session('status'))
        <div class="alert alert-success">{{ session('status') }}</div>
    @endif

    <div class="card">
        <div class="table-responsive">
            <table class="table table-striped mb-0">
                <thead>
                    <tr>
                        <th>Código</th>
                        <th>Descuento</th>
                        <th>Origen</th>
                        <th>Vigencia</th>
                        <th>Usos</th>
                        <th>Estado</th>
                        <th class="text-end">Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($cupones as $cupon)
                        <tr>
                            <td><code>{{ $cupon->codigo }}</code></td>
                            <td>{{ $cupon->etiquetaDescuento() }}</td>
                            <td>
                                @if ($cupon->origen === 'sistema')
                                    <span class="badge bg-info text-dark">Sistema</span>
                                @else
                                    <span class="badge bg-light text-dark border">Manual</span>
                                @endif
                            </td>
                            <td>
                                {{ $cupon->fecha_inicio?->format('d/m/Y') ?? '—' }}
                                a
                                {{ $cupon->fecha_fin?->format('d/m/Y') ?? '—' }}
                            </td>
                            <td>{{ $cupon->usos_actuales }} / {{ $cupon->usos_maximos ?? '∞' }}</td>
                            <td>
                                @if ($cupon->activo)
                                    <span class="badge bg-success">Activo</span>
                                @else
                                    <span class="badge bg-secondary">Inactivo</span>
                                @endif
                            </td>
                            <td class="text-end">
                                <a href="{{ route('cupones.edit', $cupon) }}" class="btn btn-sm btn-outline-primary">Editar</a>
                                <form action="{{ route('cupones.toggle', $cupon) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('PATCH')
                                    <button type="submit" class="btn btn-sm btn-outline-secondary">
                                        {{ $cupon->activo ? 'Desactivar' : 'Activar' }}
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center text-muted py-4">No hay cupones registrados.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-3">
        {{ $cupones->links() }}
    </div>
</div>
@endsection
