@extends('admin.layout')

@section('title', 'Codes promo')
@section('header', 'Codes promo')

@section('content')
<nav class="mb-6">
    <a href="{{ route('admin.ecommerce.index') }}" class="text-slate-500 hover:text-slate-900 text-sm">← E-commerce</a>
</nav>

<div class="flex flex-col sm:flex-row sm:justify-between sm:items-start gap-4 mb-6">
    <div>
        <h2 class="text-lg font-semibold text-slate-800">Codes promotionnels</h2>
        <p class="text-sm text-slate-500 mt-1">Créez des codes promo pour offrir des réductions en pourcentage ou en montant fixe.</p>
    </div>
    <a href="{{ route('admin.ecommerce.coupons.create') }}" class="px-6 py-2.5 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition shrink-0">+ Nouveau code</a>
</div>

<div class="bg-white rounded-2xl border border-slate-200 overflow-hidden">
    <div class="overflow-x-auto">
    <table class="w-full min-w-[700px]">
        <thead class="bg-slate-50 text-left text-sm text-slate-500">
            <tr>
                <th class="px-6 py-3 font-medium">Code</th>
                <th class="px-6 py-3 font-medium">Réduction</th>
                <th class="px-6 py-3 font-medium">Min. achat</th>
                <th class="px-6 py-3 font-medium">Utilisations</th>
                <th class="px-6 py-3 font-medium">Validité</th>
                <th class="px-6 py-3 font-medium">Statut</th>
                <th class="px-6 py-3 font-medium w-40">Actions</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-slate-100">
            @forelse($coupons as $c)
            <tr class="hover:bg-slate-50/50">
                <td class="px-6 py-4">
                    <span class="font-mono font-semibold">{{ $c->code }}</span>
                    @if($c->description)
                    <div class="text-xs text-slate-500 mt-0.5">{{ Str::limit($c->description, 40) }}</div>
                    @endif
                </td>
                <td class="px-6 py-4">
                    @if($c->type === 'percent')
                    {{ (int) $c->value }} %
                    @else
                    {{ number_format((float) $c->value, 2, ',', ' ') }} €
                    @endif
                    @if($c->scope_type === 'products' && !empty($c->scope_ids))
                    <span class="text-xs text-slate-500 block">sur {{ count($c->scope_ids) }} produit(s)</span>
                    @elseif($c->scope_type === 'categories' && !empty($c->scope_ids))
                    <span class="text-xs text-slate-500 block">sur {{ count($c->scope_ids) }} catégorie(s)</span>
                    @endif
                </td>
                <td class="px-6 py-4">{{ $c->min_amount ? number_format((float) $c->min_amount, 2, ',', ' ').' €' : '—' }}</td>
                <td class="px-6 py-4">{{ $c->used_count }}{{ $c->max_uses ? ' / '.$c->max_uses : '' }}</td>
                <td class="px-6 py-4 text-sm">
                    @if($c->valid_from || $c->valid_until)
                    {{ $c->valid_from?->format('d/m/Y') ?? '—' }} → {{ $c->valid_until?->format('d/m/Y') ?? '∞' }}
                    @else
                    Toujours
                    @endif
                </td>
                <td class="px-6 py-4">
                    <span class="px-2 py-1 rounded-lg text-xs font-medium {{ $c->is_active ? 'bg-emerald-100 text-emerald-700' : 'bg-slate-100 text-slate-600' }}">{{ $c->is_active ? 'Actif' : 'Inactif' }}</span>
                </td>
                <td class="px-6 py-4">
                    <a href="{{ route('admin.ecommerce.coupons.edit', $c) }}" class="text-primary-500 hover:underline text-sm font-medium">Modifier</a>
                    <form method="POST" action="{{ route('admin.ecommerce.coupons.destroy', $c) }}" class="inline ml-2" onsubmit="return confirm('Supprimer ce code promo ?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="text-red-500 hover:underline text-sm">Supprimer</button>
                    </form>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="7" class="px-6 py-16 text-center">
                    <p class="text-slate-500 mb-4">Aucun code promo.</p>
                    <a href="{{ route('admin.ecommerce.coupons.create') }}" class="inline-flex items-center gap-2 px-4 py-2 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition text-sm">+ Créer un code promo</a>
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>
    </div>
</div>
@endsection
