@extends('layouts.app')

@section('title', 'Visitas — ImóvelZapAi')
@section('page-title', 'Visitas')

@section('content')
    @include('partials.flash')

    <div class="mb-6 flex flex-wrap items-center justify-between gap-3">
        <p class="text-slate-600">Nunca mais perca uma visita por falta de organização.</p>
        <div class="flex gap-2">
            <a href="{{ route('visits.agenda') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Agenda</a>
            <a href="{{ route('visits.create') }}" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-500">Nova visita</a>
        </div>
    </div>

    <div class="mb-6 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
        <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs uppercase text-slate-500">Agendadas</p>
            <p class="mt-1 text-2xl font-semibold">{{ $dashboard['scheduled'] }}</p>
        </div>
        <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs uppercase text-slate-500">Realizadas</p>
            <p class="mt-1 text-2xl font-semibold">{{ $dashboard['completed'] }}</p>
        </div>
        <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs uppercase text-slate-500">Canceladas</p>
            <p class="mt-1 text-2xl font-semibold">{{ $dashboard['cancelled'] }}</p>
        </div>
        <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs uppercase text-slate-500">Comparecimento</p>
            <p class="mt-1 text-2xl font-semibold">{{ $dashboard['attendance_rate'] !== null ? $dashboard['attendance_rate'].'%' : '—' }}</p>
        </div>
    </div>

    <div class="overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full divide-y divide-slate-200 text-sm">
            <thead class="bg-slate-50 text-left text-xs font-semibold uppercase text-slate-500">
                <tr>
                    <th class="px-4 py-3">Pessoa</th>
                    <th class="px-4 py-3">Imóvel</th>
                    <th class="px-4 py-3">Quando</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                @forelse ($visits as $visit)
                    <tr>
                        <td class="px-4 py-3">{{ $visit->contact?->display_name ?? '—' }}</td>
                        <td class="px-4 py-3">{{ $visit->property?->title ?? '—' }}</td>
                        <td class="px-4 py-3">{{ $visit->scheduled_start?->format('d/m/Y H:i') ?? 'A definir' }}</td>
                        <td class="px-4 py-3">{{ $visit->status->label() }}</td>
                        <td class="px-4 py-3 text-right">
                            <a href="{{ route('visits.show', $visit) }}" class="font-medium text-emerald-700 hover:underline">Abrir</a>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="px-4 py-8 text-center text-slate-500">Nenhuma visita ainda.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">{{ $visits->links() }}</div>
@endsection
