@extends('layouts.app')

@section('title', 'Offline requests')
@section('heading', 'Offline subscription requests')

@section('content')
<div class="w-full">
    <x-superadmin.page-header
        eyebrow="Billing"
        title="Offline requests"
        subtitle="Review tenant bank-transfer and custom package subscription requests."
    />

    <div class="mb-6 grid gap-3 sm:grid-cols-3">
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-amber-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Pending</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $pendingCount }}</p>
        </div>
    </div>

    <div class="mb-4 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
        <form method="get" class="flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-center">
            <input type="text" name="search" value="{{ request('search') }}" placeholder="Search company…"
                   class="w-full flex-1 rounded-xl border border-slate-200 px-3 py-2.5 text-sm focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20 sm:min-w-[200px]">
            <select name="status" class="rounded-xl border border-slate-200 px-3 py-2.5 text-sm focus:border-primary focus:outline-none js-select2" style="width: 160px">
                <option value="">All statuses</option>
                <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</option>
                <option value="approved" {{ request('status') === 'approved' ? 'selected' : '' }}>Approved</option>
                <option value="rejected" {{ request('status') === 'rejected' ? 'selected' : '' }}>Rejected</option>
            </select>
            <button type="submit" class="inline-flex rounded-xl bg-primary px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:opacity-95">Filter</button>
        </form>
    </div>

    <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">Company</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Plan</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Amount</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Requested</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">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($requests as $req)
                        <tr class="hover:bg-slate-50/80">
                            <td class="whitespace-nowrap px-4 py-3.5 font-medium text-slate-900">{{ $req->company?->name ?? '—' }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">
                                {{ $req->subscriptionPlan?->name ?? '—' }}
                                @if($req->is_upgrade)
                                    <span class="ml-1 inline-flex rounded-full bg-primary/10 px-1.5 py-0.5 text-[10px] font-bold uppercase text-primary">Upgrade</span>
                                @endif
                                @if($req->coupon_code)
                                    <span class="ml-1 inline-flex rounded-full bg-slate-100 px-1.5 py-0.5 text-[10px] font-bold uppercase text-slate-600">{{ $req->coupon_code }}</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 font-semibold text-slate-900">{{ config('app.currency_symbol', '₹') }}{{ number_format($req->calculated_total ?: $req->amount, 2) }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">{{ $req->requested_at ? $req->requested_at->format('d M Y H:i') : $req->created_at->format('d M Y H:i') }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5">
                                @if($req->status === 'approved')
                                    <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">Approved</span>
                                @elseif($req->status === 'rejected')
                                    <span class="inline-flex rounded-full bg-red-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-red-700 ring-1 ring-inset ring-red-200">Rejected</span>
                                @else
                                    <span class="inline-flex rounded-full bg-amber-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-amber-800 ring-1 ring-inset ring-amber-200">Pending</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5">
                                @if($req->status === 'pending')
                                    <div class="flex gap-2">
                                        <a href="{{ route('superadmin.offline-requests.approve', $req) }}" class="rounded-xl border border-slate-200 px-3 py-1.5 text-sm font-semibold text-primary hover:bg-slate-50">Approve</a>
                                        <form action="{{ route('superadmin.offline-requests.reject', $req) }}" method="post" class="inline" onsubmit="return confirm('Reject this request?');">
                                            @csrf
                                            <button type="submit" class="rounded-xl border border-red-200 px-3 py-1.5 text-sm font-semibold text-red-700 hover:bg-red-50">Reject</button>
                                        </form>
                                    </div>
                                @else
                                    <span class="text-slate-400">—</span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-12 text-center text-slate-500">No requests found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($requests->hasPages())
            <div class="border-t border-slate-100 px-4 py-3">{{ $requests->links() }}</div>
        @endif
    </div>
</div>
@endsection
