<x-filament-panels::page>
    <style>
        .rc-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
        .rc-card { background: #fff; border: 1px solid #e5e7eb; border-radius: 10px; padding: 16px; }
        .dark .rc-card { background: #1f2937; border-color: #374151; }
        .rc-h3 { font-size: 14px; font-weight: 600; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
        .rc-count { font-size: 11px; padding: 2px 8px; background: #f3f4f6; border-radius: 999px; color: #6b7280; }
        .dark .rc-count { background: #374151; }
        .rc-tbl { width: 100%; border-collapse: collapse; font-size: 12px; }
        .rc-tbl td { padding: 6px 4px; border-bottom: 1px solid #f3f4f6; }
        .rc-r { text-align: right; }
        .rc-empty { color: #9ca3af; text-align: center; padding: 16px 0; font-size: 12px; }
    </style>

    @php
        $lost = $this->lostClients();
        $highMileage = $this->highMileageVehicles();
        $unpaid = $this->unpaidWO();
        $vip = $this->vipNeedingTouchup();
    @endphp

    <div class="rc-grid">
        <div class="rc-card">
            <div class="rc-h3">😴 Clienți pierduți <span class="rc-count">{{ $lost->count() }}</span></div>
            <div style="font-size:11px;color:#6b7280;margin-bottom:12px;">Clienți care n-au mai venit de peste 6 luni — bun moment pentru un mesaj de revenire.</div>
            <table class="rc-tbl">
                @forelse ($lost as $c)
                    <tr>
                        <td>
                            <a href="{{ route('filament.tenant.resources.clients.edit', ['record' => $c->id]) }}" style="color:inherit;text-decoration:underline;">{{ $c->name }}</a>
                            <div style="font-size:10px;color:#9ca3af;">{{ $c->phone }}</div>
                        </td>
                        <td class="rc-r" style="color:#9ca3af;font-size:11px;">
                            ultima fișă<br>{{ $c->workOrders->first()?->opened_at?->diffForHumans() ?? '—' }}
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="2" class="rc-empty">Niciun client pierdut. 🎉</td></tr>
                @endforelse
            </table>
        </div>

        <div class="rc-card">
            <div class="rc-h3">🚗 Mașini cu kilometraj mare <span class="rc-count">{{ $highMileage->count() }}</span></div>
            <div style="font-size:11px;color:#6b7280;margin-bottom:12px;">Sugerează revizie / piese de uzură.</div>
            <table class="rc-tbl">
                @forelse ($highMileage as $v)
                    <tr>
                        <td>
                            {{ $v->make }} {{ $v->model }} {{ $v->year }}
                            @if ($v->plate) <b>[{{ $v->plate }}]</b> @endif
                            <div style="font-size:10px;color:#9ca3af;">{{ $v->client?->name }}</div>
                        </td>
                        <td class="rc-r" style="font-weight:600;color:#d97706;">
                            {{ number_format($v->mileage, 0, '.', ' ') }} km
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="2" class="rc-empty">—</td></tr>
                @endforelse
            </table>
        </div>

        <div class="rc-card">
            <div class="rc-h3">💰 Fișe neplătite <span class="rc-count">{{ $unpaid->count() }}</span></div>
            <div style="font-size:11px;color:#6b7280;margin-bottom:12px;">Sună-i sau trimite-le un email cu suma datorată.</div>
            <table class="rc-tbl">
                @forelse ($unpaid as $w)
                    <tr>
                        <td>
                            <a href="{{ route('filament.tenant.resources.work-orders.edit', ['record' => $w->id]) }}" style="color:inherit;text-decoration:underline;">{{ $w->number }}</a>
                            — {{ $w->client?->name }}
                            <div style="font-size:10px;color:#9ca3af;">{{ $w->vehicle?->make }} {{ $w->vehicle?->model }} {{ $w->vehicle?->plate ? '[' . $w->vehicle->plate . ']' : '' }}</div>
                        </td>
                        <td class="rc-r" style="color:#dc2626;font-weight:600;">
                            {{ number_format(max(0, (float) $w->total - (float) $w->payments->sum('amount')), 2, '.', ' ') }} MDL
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="2" class="rc-empty">Niciun rest de încasat. 🎉</td></tr>
                @endforelse
            </table>
        </div>

        <div class="rc-card">
            <div class="rc-h3">⭐ VIP fără contact recent <span class="rc-count">{{ $vip->count() }}</span></div>
            <div style="font-size:11px;color:#6b7280;margin-bottom:12px;">Clienți VIP cu care nu s-a vorbit de peste 30 zile.</div>
            <table class="rc-tbl">
                @forelse ($vip as $c)
                    <tr>
                        <td>
                            {{ $c->name }}
                            <div style="font-size:10px;color:#9ca3af;">{{ $c->phone }}</div>
                        </td>
                        <td class="rc-r" style="color:#9ca3af;font-size:11px;">
                            {{ $c->last_contact_at?->diffForHumans() ?? 'niciodată' }}
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="2" class="rc-empty">Toți VIP-ii sunt în contact recent.</td></tr>
                @endforelse
            </table>
        </div>
    </div>
</x-filament-panels::page>
