@extends('layouts.app')

@section('title', 'Indicadores')
@section('page-title', 'Indicadores')

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

    <div class="mb-6 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <p class="text-sm text-slate-600">Visão executiva de oportunidades, visitas, financeiro e operação.</p>
        <div class="flex flex-wrap gap-2">
            <a href="{{ route('analytics.funnel') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Funil</a>
            <a href="{{ route('analytics.rankings') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Rankings</a>
            <a href="{{ route('analytics.alerts') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Alertas</a>
            <a href="{{ route('analytics.layout.edit') }}" class="rounded-lg border border-emerald-300 px-4 py-2 text-sm font-semibold text-emerald-700 hover:bg-emerald-50">Personalizar</a>
            <a href="{{ route('analytics.export', ['format' => 'csv']) }}" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-700">Exportar CSV</a>
        </div>
    </div>

    @php($kpis = $dashboard['kpis'] ?? [])

    <div class="mb-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
        @foreach ([
            'people_opportunities_created' => 'Oportunidades criadas',
            'deals_won' => 'Oportunidades ganhas',
            'volume_negotiated' => 'Volume negociado',
            'conversion_rate' => 'Taxa de conversão (%)',
        ] as $key => $label)
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase tracking-wide text-slate-500">{{ $label }}</p>
                <p class="mt-1 text-xl font-semibold text-slate-900">
                    @if ($key === 'volume_negotiated')
                        R$ {{ number_format((float) ($kpis['comercial'][$key] ?? 0), 2, ',', '.') }}
                    @else
                        {{ $kpis['comercial'][$key] ?? 0 }}
                    @endif
                </p>
            </div>
        @endforeach
    </div>

    <div class="mb-6 grid gap-6 lg:grid-cols-2">
        <div class="rounded-xl border border-slate-200 bg-white p-5 shadow-sm">
            <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Funil resumido</h2>
            <ul class="mt-4 space-y-2">
                @foreach ($dashboard['funnel']['stages'] ?? [] as $stage)
                    <li class="flex items-center justify-between text-sm">
                        <span>{{ $stage['label'] }}</span>
                        <span class="font-semibold text-emerald-700">{{ $stage['count'] }}</span>
                    </li>
                @endforeach
            </ul>
        </div>

        <div class="rounded-xl border border-slate-200 bg-white p-5 shadow-sm">
            <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Alertas abertos</h2>
            <ul class="mt-4 space-y-2">
                @forelse ($dashboard['alerts'] ?? [] as $alert)
                    <li class="rounded-lg border border-slate-100 px-3 py-2 text-sm">
                        <p class="font-medium text-slate-900">{{ $alert->title }}</p>
                        <p class="text-slate-500">{{ $alert->body }}</p>
                    </li>
                @empty
                    <li class="text-sm text-slate-500">Nenhum alerta aberto.</li>
                @endforelse
            </ul>
        </div>
    </div>
@endsection
