@extends('layouts.app-shell')
@section('title', 'Partner: ' . $user->name)
@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')
@php $pp = $user->partnerProfile; @endphp
<div class="flex-between mb-3">
    <div>
        <a href="{{ route('admin.partners') }}" class="btn btn-ghost btn-sm">← All partners</a>
    </div>
    <div class="flex" style="gap: 8px; flex-wrap: wrap;">
        <a href="{{ route('admin.partners.edit', $user) }}" class="btn btn-primary btn-sm">
            <x-icon name="edit" :size="14" /> Edit partner
        </a>
        @if($user->is_partner_approved)
            <form method="POST" action="{{ route('admin.partners.reject', $user) }}" onsubmit="return confirm('Revoke approval? This partner will be unable to take any bookings.');">
                @csrf <button class="btn btn-outline btn-sm">Revoke approval</button>
            </form>
        @else
            <form method="POST" action="{{ route('admin.partners.approve', $user) }}">
                @csrf <button class="btn btn-primary btn-sm"><x-icon name="check" :size="14" /> Approve partner</button>
            </form>
        @endif
        <form method="POST" action="{{ route('admin.partners.destroy', $user) }}" onsubmit="return confirm('Delete this partner permanently?');">
            @csrf @method('DELETE')
            <button class="btn btn-outline btn-sm" style="border-color: var(--bfk-red); color: var(--bfk-red);">Delete</button>
        </form>
    </div>
</div>

<div class="partner-header-card">
    <div class="photo-preview-box-lg">
        @if($pp?->photo_url)
            <img src="{{ $pp->photo_url }}" alt="{{ $pp->display_name }}">
        @else
            <span class="photo-initial">{{ $pp?->initials() ?? strtoupper(substr($user->name, 0, 1)) }}</span>
        @endif
    </div>
    <div class="partner-header-info">
        <h2>{{ $pp?->display_name ?? $user->name }}</h2>
        <p>{{ $user->email }} · {{ $user->phone }} · Joined {{ $user->created_at->format('d M Y') }} · {{ $pp?->experience_years ?? 0 }}y experience</p>
    </div>
</div>

{{-- Status row --}}
<div class="flex" style="gap: 8px; flex-wrap: wrap; margin-bottom: 14px;">
    {!! $user->is_partner_approved ? '<span class="pill pill-green">Approved</span>' : '<span class="pill pill-gold">Pending approval</span>' !!}
    {!! $pp?->aadhaar_verified ? '<span class="pill pill-blue">Aadhaar ✓</span>' : '<span class="pill pill-grey">Aadhaar not verified</span>' !!}
    {!! $pp?->police_verified ? '<span class="pill pill-blue">Police ✓</span>' : '<span class="pill pill-grey">Police not verified</span>' !!}
    @if($pp?->isSuspended())
        <span class="pill pill-red" title="{{ $pp->suspend_reason }}">Suspended — new bookings blocked</span>
    @else
        <span class="pill pill-green">Accepting new bookings</span>
    @endif
    @if($pp?->is_online)<span class="pill pill-blue">Online</span>@endif
    @if($pp?->is_available)<span class="pill pill-green">Available</span>@else<span class="pill pill-grey">Unavailable</span>@endif
</div>

{{-- Suspend / reactivate --}}
@if($pp?->isSuspended())
    <div class="suspend-panel is-suspended">
        <div class="suspend-ic">⛔</div>
        <div class="suspend-body">
            <strong>Suspended from new bookings</strong>
            <span class="muted">Reason: {{ $pp->suspend_reason }}<br>Existing subscriptions will continue to renew and assign visits to this partner.</span>
        </div>
        <form method="POST" action="{{ route('admin.partners.unsuspend', $user) }}">
            @csrf
            <button class="btn btn-primary btn-sm"><x-icon name="check" :size="14" /> Reactivate</button>
        </form>
    </div>
@else
    <div class="suspend-panel is-off">
        <div class="suspend-ic">🟢</div>
        <div class="suspend-body">
            <strong>Accepting new bookings</strong>
            <span class="muted">You can suspend this partner to hide them from the booking flow. Their existing subscriptions will continue to renew and assign visits.</span>
        </div>
        <button type="button" class="btn btn-outline btn-sm" style="border-color: var(--bfk-red); color: var(--bfk-red);" onclick="document.getElementById('suspend-form').style.display='block'">
            Suspend…
        </button>
    </div>
    <form id="suspend-form" method="POST" action="{{ route('admin.partners.suspend', $user) }}" style="display: none; margin-bottom: 14px; padding: 14px; background: #fff; border: 1px solid var(--bfk-border); border-radius: 12px;">
        @csrf
        <div class="field" style="margin-bottom: 10px;">
            <label>Reason for suspension (admin only)</label>
            <input type="text" name="suspend_reason" required maxlength="200" placeholder="e.g. Customer complaint, vacation, performance issue">
        </div>
        <div class="flex" style="gap: 8px;">
            <button class="btn btn-primary btn-sm" style="background: var(--bfk-red); border-color: var(--bfk-red);">Confirm suspension</button>
            <button type="button" class="btn btn-ghost btn-sm" onclick="document.getElementById('suspend-form').style.display='none'">Cancel</button>
        </div>
    </form>
@endif

{{-- Stats --}}
<div class="grid-2" style="grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 14px;">
    <div class="kpi-mini k1">
        <div class="kpi-mini-ic">📦</div>
        <div>
            <div class="kpi-mini-label">Total bookings</div>
            <div class="kpi-mini-val">{{ $stats['total_bookings'] }}</div>
            <div class="kpi-mini-delta">{{ $stats['active_bookings'] }} active</div>
        </div>
    </div>
    <div class="kpi-mini k2">
        <div class="kpi-mini-ic">✓</div>
        <div>
            <div class="kpi-mini-label">Visits present</div>
            <div class="kpi-mini-val">{{ $stats['completed_visits'] }}</div>
            <div class="kpi-mini-delta">{{ $stats['absent_visits'] }} absent</div>
        </div>
    </div>
    <div class="kpi-mini k3">
        <div class="kpi-mini-ic">★</div>
        <div>
            <div class="kpi-mini-label">Rating</div>
            <div class="kpi-mini-val">{{ number_format($pp?->rating_avg ?? 0, 1) }}</div>
            <div class="kpi-mini-delta">{{ $pp?->rating_count ?? 0 }} reviews</div>
        </div>
    </div>
    <div class="kpi-mini k4">
        <div class="kpi-mini-ic">🏖</div>
        <div>
            <div class="kpi-mini-label">Pending leaves</div>
            <div class="kpi-mini-val">{{ $stats['pending_leaves'] }}</div>
            <div class="kpi-mini-delta">{{ $stats['leaves'] }} all-time</div>
        </div>
    </div>
</div>

{{-- Commission & wallet summary --}}
@if($commissionAggregate)
    @php
        $gross = $commissionAggregate['active']['gross_paise'] + $commissionAggregate['completed']['gross_paise'];
        $platformCut = $commissionAggregate['active']['platform_paise'] + $commissionAggregate['completed']['platform_paise'];
        $partnerNet = $commissionAggregate['active']['partner_net_paise'] + $commissionAggregate['completed']['partner_net_paise'];
        $partnerPct = $commissionAggregate['percent'];
        $wallet = (int) ($pp?->wallet_balance_paise ?? 0);
        $isCustom = $pp?->commission_percent !== null;
    @endphp
    <div class="panel" style="background: linear-gradient(135deg, var(--bfk-secondary), #2a2d3a); color: #fff; margin-bottom: 14px;">
        <div class="panel-head" style="border-color: rgba(255,255,255,0.12);">
            <h3 style="color: #fff;"><x-icon name="money" :size="14" style="color: var(--bfk-gold);" /> Commission &amp; earnings</h3>
            <span class="meta" style="color: rgba(255,255,255,0.6); font-size: 12px;">Rate <strong style="color: var(--bfk-gold);">{{ number_format($partnerPct, 1) }}%</strong>{{ $isCustom ? ' (custom)' : ' (global)' }}</span>
        </div>
        <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px;">
            <div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700;">Gross revenue</div>
                <div style="font-size: 22px; font-weight: 800; color: #fff;">₹{{ number_format($gross/100, 0) }}</div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.5);">active + completed</div>
            </div>
            <div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700;">Platform earns</div>
                <div style="font-size: 22px; font-weight: 800; color: #ff9a7e;">₹{{ number_format($platformCut/100, 0) }}</div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.5);">{{ number_format($partnerPct, 1) }}%</div>
            </div>
            <div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700;">Partner net</div>
                <div style="font-size: 22px; font-weight: 800; color: #5fffaa;">₹{{ number_format($partnerNet/100, 0) }}</div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.5);">paid out so far</div>
            </div>
            <div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700;">Wallet balance</div>
                <div style="font-size: 22px; font-weight: 800; color: var(--bfk-gold);">₹{{ number_format($wallet/100, 0) }}</div>
                <div style="font-size: 11px; color: rgba(255,255,255,0.5);">awaiting payout</div>
            </div>
        </div>
        @if($pp?->commission_notes)
            <div style="margin-top: 14px; padding: 10px 12px; background: rgba(255,255,255,0.06); border-radius: 8px; font-size: 12.5px;">
                <span style="color: rgba(255,255,255,0.6);">Notes:</span> {{ $pp->commission_notes }}
            </div>
        @endif
        <div style="margin-top: 14px; text-align: right;">
            <a href="{{ route('admin.partners.edit', $user) }}" class="btn-app btn-outline" style="background: rgba(255,255,255,0.08); color: #fff; border-color: rgba(255,255,255,0.2);"><x-icon name="settings" :size="14" /> Adjust commission rate</a>
        </div>
    </div>
@endif

<div class="grid-2" style="grid-template-columns: 1.4fr 1fr; gap: 18px;">
    <div>
        <div class="panel">
            <div class="panel-head">
                <h3><x-icon name="profile" :size="14" /> Profile</h3>
                <a href="{{ route('admin.partners.edit', $user) }}" class="muted" style="font-size: 12px;">Edit →</a>
            </div>
            @if($pp)
                <div class="form-grid">
                    <div>
                        <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Display name</div>
                        <strong>{{ $pp->display_name }}</strong>
                    </div>
                    <div>
                        <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Experience</div>
                        <strong>{{ $pp->experience_years ?? 0 }} years</strong>
                    </div>
                    <div>
                        <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Aadhaar</div>
                        <strong>{{ $pp->aadhaar_number ?? '—' }}</strong>
                    </div>
                    <div>
                        <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Jobs done</div>
                        <strong>{{ $pp->jobs_done ?? 0 }}</strong>
                    </div>
                    @if($pp->bio)
                        <div style="grid-column: 1 / -1;">
                            <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Bio</div>
                            <p style="font-size: 13.5px; line-height: 1.5;">{{ $pp->bio }}</p>
                        </div>
                    @endif
                    @if($pp->admin_notes)
                        <div style="grid-column: 1 / -1;">
                            <div class="muted" style="font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em;">Admin notes</div>
                            <p style="font-size: 13.5px; line-height: 1.5; background: #fff7e6; padding: 10px 12px; border-radius: 8px; border-left: 3px solid #f0b132;">{{ $pp->admin_notes }}</p>
                        </div>
                    @endif
                </div>
            @else
                <p class="muted">No partner profile created.</p>
            @endif
        </div>

        <div class="panel">
            <div class="panel-head">
                <h3>Services offered ({{ $pp?->services->count() ?? 0 }})</h3>
            </div>
            @if($pp?->services->count())
                <div class="chip-list">
                    @foreach($pp->services as $s)
                        <span class="chip">
                            <span class="chip-ic">{{ strtoupper(substr($s->name, 0, 1)) }}</span>
                            <span>{{ $s->name }}</span>
                            @if($s->pivot->custom_price_paise)
                                <span class="muted" style="font-size: 11px; font-weight: 500;">· ₹{{ number_format($s->pivot->custom_price_paise/100, 0) }}</span>
                            @endif
                            <form method="POST" action="{{ route('admin.partners.services.detach', [$user, $s]) }}" style="display: inline;" onsubmit="return confirm('Remove this service?');">
                                @csrf @method('DELETE')
                                <button type="submit" class="chip-remove" title="Remove">×</button>
                            </form>
                        </span>
                    @endforeach
                </div>
            @else
                <p class="empty-text">No services assigned yet.</p>
            @endif
            @php $availableServices = $allServices->reject(fn($s) => $pp?->services->contains($s->id)); @endphp
            @if($availableServices->count())
                <form method="POST" action="{{ route('admin.partners.services.attach', $user) }}" class="inline-add">
                    @csrf
                    <div class="select-wrap size-sm">
                        <select name="service_id" required>
                            <option value="">+ Add a service…</option>
                            @foreach($availableServices as $s)
                                <option value="{{ $s->id }}">{{ $s->name }}{{ $s->category?->name ? ' · ' . $s->category->name : '' }}</option>
                            @endforeach
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm">Add service</button>
                </form>
            @else
                <p class="empty-text">All services already assigned.</p>
            @endif
        </div>

        <div class="panel">
            <div class="panel-head">
                <h3>Service areas ({{ $pp?->areas->count() ?? 0 }})</h3>
            </div>
            @if($pp?->areas->count())
                <div class="chip-list">
                    @foreach($pp->areas as $a)
                        <span class="chip">
                            <span class="chip-ic">📍</span>
                            <span>{{ $a->name }}</span>
                            <form method="POST" action="{{ route('admin.partners.areas.detach', [$user, $a]) }}" style="display: inline;" onsubmit="return confirm('Remove this area?');">
                                @csrf @method('DELETE')
                                <button type="submit" class="chip-remove" title="Remove">×</button>
                            </form>
                        </span>
                    @endforeach
                </div>
            @else
                <p class="empty-text">No service areas assigned yet.</p>
            @endif
            @php $availableAreas = $allAreas->reject(fn($a) => $pp?->areas->contains($a->id)); @endphp
            @if($availableAreas->count())
                <form method="POST" action="{{ route('admin.partners.areas.attach', $user) }}" class="inline-add">
                    @csrf
                    <div class="select-wrap size-sm">
                        <select name="area_id" required>
                            <option value="">+ Add a service area…</option>
                            @foreach($availableAreas as $a)
                                <option value="{{ $a->id }}">{{ $a->name }}{{ $a->city ? ' · ' . $a->city : '' }}</option>
                            @endforeach
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm">Add area</button>
                </form>
            @else
                <p class="empty-text">All areas already assigned.</p>
            @endif
        </div>

        <div class="panel">
            <div class="panel-head">
                <h3>KYC documents</h3>
            </div>
            <div class="form-grid" style="grid-template-columns: 1fr 1fr; gap: 14px;">
                <div class="kyc-card">
                    <div class="kyc-ic">🪪</div>
                    <div class="kyc-info">
                        <div class="kyc-label">Aadhaar</div>
                        @if($pp?->aadhaar_doc_path)
                            <a href="{{ asset('storage/' . $pp->aadhaar_doc_path) }}" target="_blank" class="kyc-action">
                                <x-icon name="eye" :size="13" /> View uploaded
                            </a>
                            @if($pp->aadhaar_verified_at)
                                <p class="muted" style="font-size: 11px; margin: 4px 0 0;">Verified {{ $pp->aadhaar_verified_at->format('d M Y') }}</p>
                            @else
                                <p class="muted" style="font-size: 11px; margin: 4px 0 0;">Not yet verified</p>
                            @endif
                        @else
                            <p class="empty-text" style="margin: 0; padding: 0;">No document uploaded</p>
                        @endif
                    </div>
                </div>
                <div class="kyc-card">
                    <div class="kyc-ic">🛡️</div>
                    <div class="kyc-info">
                        <div class="kyc-label">Police verification</div>
                        @if($pp?->police_doc_path)
                            <a href="{{ asset('storage/' . $pp->police_doc_path) }}" target="_blank" class="kyc-action">
                                <x-icon name="eye" :size="13" /> View uploaded
                            </a>
                            @if($pp->police_verified_at)
                                <p class="muted" style="font-size: 11px; margin: 4px 0 0;">Verified {{ $pp->police_verified_at->format('d M Y') }}</p>
                            @else
                                <p class="muted" style="font-size: 11px; margin: 4px 0 0;">Not yet verified</p>
                            @endif
                        @else
                            <p class="empty-text" style="margin: 0; padding: 0;">No document uploaded</p>
                        @endif
                    </div>
                </div>
            </div>
            <form method="POST" action="{{ route('admin.partners.kyc.upload', $user) }}" enctype="multipart/form-data" class="inline-add" style="margin-top: 14px;">
                @csrf
                <div style="flex: 1; min-width: 180px;">
                    <label style="font-size: 11px; font-weight: 600; display: block; margin-bottom: 4px; color: var(--bfk-text-2);">Replace Aadhaar</label>
                    <input type="file" name="aadhaar_doc" accept=".jpg,.jpeg,.png,.pdf" style="width: 100%; font-size: 12px;">
                </div>
                <div style="flex: 1; min-width: 180px;">
                    <label style="font-size: 11px; font-weight: 600; display: block; margin-bottom: 4px; color: var(--bfk-text-2);">Replace police doc</label>
                    <input type="file" name="police_doc" accept=".jpg,.jpeg,.png,.pdf" style="width: 100%; font-size: 12px;">
                </div>
                <button type="submit" class="btn btn-primary btn-sm" style="align-self: end;">Upload KYC</button>
            </form>
        </div>

        <div class="panel">
            <div class="panel-head">
                <h3><x-icon name="profile" :size="14" /> Team members ({{ $pp?->teamMembers->count() ?? 0 }})</h3>
                <button type="button" class="btn btn-primary btn-sm" onclick="document.getElementById('add-team-form').style.display='block'">+ Add team member</button>
            </div>

            {{-- Add team member form --}}
            <form id="add-team-form" method="POST" action="{{ route('admin.partners.team.store', $user) }}" enctype="multipart/form-data" style="display: none; margin-bottom: 14px; padding: 14px; background: var(--bfk-bg-soft); border-radius: 12px;">
                @csrf
                <div class="form-grid" style="grid-template-columns: 1fr 1fr; gap: 12px;">
                    <div>
                        <label style="font-size: 12px;">Name</label>
                        <input type="text" name="name" required placeholder="e.g. Ramesh">
                    </div>
                    <div>
                        <label style="font-size: 12px;">Phone</label>
                        <input type="text" name="phone" required placeholder="9876543210">
                    </div>
                    <div>
                        <label style="font-size: 12px;">Email (optional, gives login access)</label>
                        <input type="email" name="email" placeholder="ramesh@partner.com">
                    </div>
                    <div>
                        <label style="font-size: 12px;">Role</label>
                        <div class="select-wrap size-sm">
                            <select name="role">
                                <option value="Helper">Helper</option>
                                <option value="Lead">Lead</option>
                                <option value="Apprentice">Apprentice</option>
                            </select>
                        </div>
                    </div>
                    <div style="grid-column: 1 / -1;">
                        <label style="font-size: 12px;">Photo</label>
                        <div class="file-input">
                            <input type="file" name="photo" accept="image/*" id="team-photo-input">
                            <label for="team-photo-input" class="file-input-label">Tap to upload a photo</label>
                        </div>
                    </div>
                    <div style="grid-column: 1 / -1; display: flex; gap: 14px;">
                        <label style="display: flex; align-items: center; gap: 6px; font-size: 12px;">
                            <input type="checkbox" name="aadhaar_verified" value="1"> Aadhaar verified
                        </label>
                        <label style="display: flex; align-items: center; gap: 6px; font-size: 12px;">
                            <input type="checkbox" name="police_verified" value="1"> Police verified
                        </label>
                    </div>
                </div>
                <div class="flex" style="gap: 8px; margin-top: 10px;">
                    <button type="submit" class="btn btn-primary btn-sm">Add team member</button>
                    <button type="button" class="btn btn-ghost btn-sm" onclick="document.getElementById('add-team-form').style.display='none'">Cancel</button>
                </div>
            </form>

            @if($pp?->teamMembers && $pp->teamMembers->count())
                <div class="team-grid">
                    @foreach($pp->teamMembers as $m)
                        <div class="team-card {{ $m->is_active ? '' : 'is-disabled' }}">
                            <div class="team-avatar">
                                @if($m->photo_url)
                                    <img src="{{ $m->photo_url }}" alt="">
                                @else
                                    {{ $m->initials() }}
                                @endif
                            </div>
                            <div class="team-info">
                                <div class="team-name">{{ $m->name }} <span class="muted" style="font-weight: 500; font-size: 11.5px;">· {{ $m->role }}</span></div>
                                <div class="team-meta">
                                    {{ $m->phone ?: '—' }}@if($m->email) · {{ $m->email }}@endif
                                    @if($m->user_id)<br><span class="pill pill-blue" style="font-size: 9px; padding: 1px 6px;">Has login</span>@endif
                                    @if(!$m->is_active) <span class="pill pill-grey" style="font-size: 9px; padding: 1px 6px;">Disabled</span> @endif
                                </div>
                            </div>
                            <div class="team-actions">
                                <form method="POST" action="{{ route('admin.partners.team.toggle', [$user, $m]) }}">
                                    @csrf
                                    <button type="submit" class="ic-btn" title="{{ $m->is_active ? 'Disable' : 'Enable' }}">
                                        <x-icon :name="$m->is_active ? 'eye-off' : 'eye'" :size="14" />
                                    </button>
                                </form>
                                <form method="POST" action="{{ route('admin.partners.team.destroy', [$user, $m]) }}" onsubmit="return confirm('Remove this team member?');">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="ic-btn ic-delete" title="Remove">
                                        <x-icon name="trash" :size="14" />
                                    </button>
                                </form>
                            </div>
                        </div>
                    @endforeach
                </div>
            @else
                <p class="empty-text">No team members added yet. Add helpers who can take visits on this partner's behalf.</p>
            @endif
            <p class="muted" style="font-size: 11.5px; margin: 10px 0 0;">ℹ Team members are visible to the partner and admin only. <strong>Customers will only ever see the partner's name and photo</strong>, even if a team member fulfills a visit.</p>
        </div>

        <div class="panel">
            <div class="panel-head"><h3>Recent bookings ({{ $stats['total_bookings'] }})</h3></div>
            <div class="table-wrap">
                <table class="table">
                    <thead>
                        <tr><th>Code</th><th>Service</th><th>Customer</th><th>Status</th><th>Monthly</th></tr>
                    </thead>
                    <tbody>
                        @forelse($pp?->bookings->sortByDesc('id')->take(8) ?? [] as $b)
                            <tr>
                                <td data-label="Code"><a href="{{ route('admin.bookings.show', $b) }}"><span class="code">{{ $b->booking_code }}</span></a></td>
                                <td data-label="Service">{{ $b->service->name ?? '—' }}</td>
                                <td data-label="Customer">{{ $b->customer_name }}</td>
                                <td data-label="Status"><span class="pill pill-{{ $b->status_color }}">{{ str_replace('_',' ', $b->subscription_status) }}</span></td>
                                <td data-label="Monthly">₹{{ number_format($b->total_rupees, 0) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="5"><div class="empty">No bookings yet.</div></td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div>
        <div class="panel">
            <div class="panel-head"><h3>Recent ratings</h3></div>
            @forelse($pp?->ratings->sortByDesc('id')->take(5) ?? [] as $r)
                <div style="padding: 10px 0; border-bottom: 1px solid var(--bfk-border);">
                    <div style="color: var(--bfk-gold); font-size: 14px;">{{ str_repeat('★', $r->rating) }}{{ str_repeat('☆', 5 - $r->rating) }}</div>
                    <p style="font-size: 12.5px; margin: 4px 0 0;">{{ $r->review ?: 'No written review.' }}</p>
                    <p class="muted" style="font-size: 11px; margin: 2px 0 0;">{{ $r->created_at->diffForHumans() }}</p>
                </div>
            @empty
                <p class="muted">No ratings yet.</p>
            @endforelse
        </div>
    </div>
</div>
@endsection
