@extends('layouts.app')

@section('title', 'Coupons')
@section('heading', 'Subscription Coupons')

@section('content')
<div class="w-full">
    <x-superadmin.page-header
        eyebrow="Billing"
        title="Coupons"
        subtitle="Percent or fixed discounts tenants can apply at checkout."
    >
        <x-slot:actions>
            <a href="{{ route('superadmin.coupons.create') }}" class="inline-flex items-center rounded-xl bg-primary px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:opacity-95"><i class="bi bi-plus-lg me-1"></i>Add coupon</a>
        </x-slot:actions>
    </x-superadmin.page-header>

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-100 text-sm">
                <thead class="bg-slate-50/80 text-left">
                    <tr>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Code</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Discount</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Uses</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Validity</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Status</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($coupons as $coupon)
                        <tr class="hover:bg-slate-50/80">
                            <td class="px-4 py-3.5 font-semibold text-slate-900">{{ $coupon->code }}</td>
                            <td class="px-4 py-3.5 text-slate-600">
                                {{ $coupon->discount_type === 'percent' ? number_format($coupon->discount_value, 0).'%' : '₹'.number_format($coupon->discount_value, 2) }}
                            </td>
                            <td class="px-4 py-3.5 text-slate-600">{{ $coupon->used_count }}{{ $coupon->max_uses ? ' / '.$coupon->max_uses : '' }}</td>
                            <td class="px-4 py-3.5 text-slate-600">
                                {{ $coupon->valid_from?->format('d M Y') ?? '—' }} → {{ $coupon->valid_until?->format('d M Y') ?? '—' }}
                            </td>
                            <td class="px-4 py-3.5">
                                @if($coupon->is_active)
                                    <span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-emerald-700 ring-1 ring-inset ring-emerald-200">Active</span>
                                @else
                                    <span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-slate-500">Inactive</span>
                                @endif
                            </td>
                            <td class="px-4 py-3.5 text-right">
                                <div class="inline-flex flex-wrap items-center justify-end gap-2">
                                    <a href="{{ route('superadmin.coupons.edit', $coupon) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                                    <form action="{{ route('superadmin.coupons.toggle-active', $coupon) }}" method="post" class="inline">@csrf
                                        <button class="rounded-xl border border-slate-200 px-3 py-1.5 text-sm font-semibold text-slate-700 hover:bg-slate-50">Toggle</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="px-4 py-12 text-center text-slate-500">No coupons yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($coupons->hasPages())
            <div class="border-t border-slate-100 px-4 py-3">{{ $coupons->links() }}</div>
        @endif
    </div>
</div>
@endsection
