@extends('layouts.app-shell')
@section('title', 'Subscriptions')
@section('sidebar')
    @include('admin._sidebar')
@endsection
@section('bottomnav')
    <a href="{{ route('admin.dashboard') }}" class="{{ request()->routeIs('admin.dashboard*') ? 'active' : '' }}"><x-icon name="home" :size="20" /><span>Home</span></a>
    <a href="{{ route('admin.bookings') }}" class="{{ request()->routeIs('admin.bookings*') ? 'active' : '' }}"><x-icon name="bookings" :size="20" /><span>Subs</span></a>
    <a href="{{ route('admin.partners') }}" class="{{ request()->routeIs('admin.partners*') ? 'active' : '' }}"><x-icon name="profile" :size="20" /><span>Partners</span></a>
    <a href="{{ route('admin.services.index') }}" class="{{ request()->routeIs('admin.services*') ? 'active' : '' }}"><x-icon name="services" :size="20" /><span>Catalogue</span></a>
@endsection

@section('content')

<div class="kpi-grid">
    <div class="kpi k1">
        <div class="lbl">Total</div>
        <div class="val">{{ $bookings->total() }}</div>
        <div class="delta">all-time</div>
    </div>
    <div class="kpi k2">
        <div class="lbl">Active</div>
        <div class="val">{{ $stats['active'] ?? 0 }}</div>
        <div class="delta">subscriptions</div>
    </div>
    <div class="kpi k3">
        <div class="lbl">Pending</div>
        <div class="val">{{ $stats['pending'] ?? 0 }}</div>
        <div class="delta">awaiting partner</div>
    </div>
    <div class="kpi k4">
        <div class="lbl">Revenue</div>
        <div class="val">₹{{ number_format(($stats['revenue'] ?? 0)/100, 0) }}</div>
        <div class="delta">from active</div>
    </div>
</div>

<x-admin-filter
    :searchPlaceholder="'Search by code, customer name or phone'"
    :statusOptions="['active'=>'Active', 'paused'=>'Paused', 'cancelled'=>'Cancelled', 'expired'=>'Expired']" />

<div class="panel">
    <div class="table-wrap">
        <table class="table">
            <thead>
                <tr>
                    <th>Code</th>
                    <th>Customer</th>
                    <th>Service</th>
                    <th>Partner</th>
                    <th>Schedule</th>
                    <th>Status</th>
                    <th>Monthly</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse($bookings as $b)
                    <tr>
                        <td data-label="Code">
                            <a href="{{ route('admin.bookings.show', $b) }}"><span class="code">{{ $b->booking_code }}</span></a>
                            @if($b->subscription_code)
                                <div class="muted" style="font-size:10.5px;">{{ $b->subscription_code }}</div>
                            @endif
                        </td>
                        <td data-label="Customer">
                            <strong>{{ $b->customer_name }}</strong>
                            <div class="muted" style="font-size:11.5px;">{{ $b->customer_phone }}</div>
                        </td>
                        <td data-label="Service">
                            <strong>{{ $b->service->name ?? '—' }}</strong>
                            <div class="muted" style="font-size:11.5px;">{{ $b->area->name ?? '—' }}</div>
                        </td>
                        <td data-label="Partner">{{ $b->partner?->display_name ?? '—' }}</td>
                        <td data-label="Schedule">
                            @php
                                $nextVisit = $b->visits->where('status', 'scheduled')->where('scheduled_date', '>=', now()->startOfDay())->sortBy('scheduled_date')->first();
                                $visitsTotal = $b->visits->count();
                                $visitsDone = $b->visits->where('status', 'completed')->count();
                            @endphp
                            @if($nextVisit)
                                <strong>{{ $nextVisit->scheduled_date->format('d M Y') }}</strong>
                                <div class="muted" style="font-size:11.5px;">
                                    Next · {{ $visitsDone }}/{{ $b->visits_per_period }} done
                                </div>
                            @elseif($b->visits->count())
                                <strong class="muted">—</strong>
                                <div class="muted" style="font-size:11.5px;">{{ $visitsDone }}/{{ $b->visits_per_period }} done · none upcoming</div>
                            @else
                                <strong class="muted">—</strong>
                                <div class="muted" style="font-size:11.5px;">No visits scheduled</div>
                            @endif
                        </td>
                        <td data-label="Status">
                            <span class="pill pill-{{ $b->status_color }}">{{ $b->display_status }}</span>
                        </td>
                        <td data-label="Monthly">₹{{ number_format($b->total_rupees, 0) }}</td>
                        <td data-label="">
                            <a href="{{ route('admin.bookings.show', $b) }}" class="ic-btn" title="View">
                                <x-icon name="eye" :size="14" />
                            </a>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="8"><div class="empty">No subscriptions found.</div></td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="mt-3">{{ $bookings->appends(request()->query())->links() }}</div>

@endsection
