@extends('layouts.admin')

@section('header_title', 'Painel de Controlo')
@section('header_subtitle', 'Monitorização em tempo real do ecossistema de segurança.')

@section('content')
    <!-- Dashboard Stats Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6 gap-6 mb-8">
        
        <!-- Stat Card 1 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-blue-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-blue-500/10 flex items-center justify-center text-blue-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Clientes</p>
                    <p class="text-2xl font-bold font-heading text-white">{{ $stats['clients'] ?? 0 }}</p>
                </div>
            </div>
            <div class="text-xs text-emerald-400 flex items-center gap-1">
                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18" /></svg>
                <span>+12% este mês</span>
            </div>
        </div>

        <!-- Stat Card 2 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-indigo-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-indigo-500/10 flex items-center justify-center text-indigo-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Instalações</p>
                    <p class="text-2xl font-bold font-heading text-white">{{ $stats['installations'] ?? 0 }}</p>
                </div>
            </div>
            <div class="text-xs text-emerald-400 flex items-center gap-1">
                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18" /></svg>
                <span>Ativas na rede</span>
            </div>
        </div>

        <!-- Stat Card 3 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-teal-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-teal-500/10 flex items-center justify-center text-teal-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Câmeras</p>
                    <p class="text-2xl font-bold font-heading text-white">{{ $stats['cameras'] ?? 0 }}</p>
                </div>
            </div>
            <div class="text-xs text-slate-500 flex items-center gap-1">
                <span>Total mapeado</span>
            </div>
        </div>

        <!-- Stat Card 4 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-purple-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-purple-500/10 flex items-center justify-center text-purple-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Contratos</p>
                    <p class="text-2xl font-bold font-heading text-white">{{ $stats['contracts'] ?? 0 }}</p>
                </div>
            </div>
            <div class="text-xs text-slate-500 flex items-center gap-1">
                <span>Manutenção Ativa</span>
            </div>
        </div>

        <!-- Stat Card 5 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-red-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-red-500/10 flex items-center justify-center text-red-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Tickets</p>
                    <p class="text-2xl font-bold font-heading text-white">{{ $stats['tickets'] ?? 0 }}</p>
                </div>
            </div>
            <div class="text-xs text-red-400 flex items-center gap-1">
                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
                <span>Abertos / Pendentes</span>
            </div>
        </div>

        <!-- Stat Card 6 -->
        <div class="p-5 rounded-2xl bg-slate-900 border border-white/5 shadow-xl relative overflow-hidden group">
            <div class="absolute inset-0 bg-linear-to-br from-green-500/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"></div>
            <div class="flex items-center gap-4 mb-4">
                <div class="w-10 h-10 rounded-xl bg-green-500/10 flex items-center justify-center text-green-400">
                    <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                </div>
                <div>
                    <p class="text-xs font-medium text-slate-400 uppercase tracking-wider">Faturação</p>
                    <p class="text-2xl font-bold font-heading text-white">€{{ number_format($stats['revenue'] ?? 0, 2, ',', '.') }}</p>
                </div>
            </div>
            <div class="text-xs text-slate-500 flex items-center gap-1">
                <span>Este mês</span>
            </div>
        </div>
    </div>

    <!-- Main Content Area -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        
        <!-- Cameras Status / Analytics placeholder -->
        <div class="lg:col-span-2 bg-slate-900 border border-white/5 rounded-2xl p-6 shadow-xl">
            <div class="flex items-center justify-between mb-6">
                <h3 class="text-lg font-bold font-heading text-white">Status da Monitorização</h3>
                <button class="text-xs font-medium text-blue-400 hover:text-blue-300">Ver Relatório Completo</button>
            </div>
            <div class="h-64 rounded-xl border border-white/5 bg-slate-950 flex flex-col items-center justify-center relative overflow-hidden">
                <div class="absolute inset-0 bg-[url('https://www.transparenttextures.com/patterns/cubes.png')] opacity-10"></div>
                <svg class="w-16 h-16 text-slate-700 mb-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
                </svg>
                <p class="text-sm text-slate-500 font-medium">Gráfico de atividade em tempo real (Chart.js via CDN)</p>
            </div>
        </div>

        <!-- Recent Tickets -->
        <div class="bg-slate-900 border border-white/5 rounded-2xl p-6 shadow-xl flex flex-col">
            <div class="flex items-center justify-between mb-6">
                <h3 class="text-lg font-bold font-heading text-white">Tickets Recentes</h3>
                <span class="px-2 py-1 bg-red-500/10 text-red-400 text-xs font-bold rounded-lg">{{ $stats['tickets'] ?? 0 }} abertos</span>
            </div>

            <div class="space-y-4 flex-1">
                @forelse($recentTickets ?? [] as $ticket)
                <div class="p-4 rounded-xl border border-white/5 bg-slate-950 hover:bg-slate-800 transition-colors cursor-pointer group">
                    <div class="flex items-start justify-between mb-2">
                        <p class="text-sm font-semibold text-white group-hover:text-blue-400 transition-colors">{{ $ticket->subject }}</p>
                        <span class="text-[10px] uppercase font-bold px-2 py-0.5 rounded-full 
                            @if($ticket->priority == 'high' || $ticket->priority == 'urgent') bg-red-500/20 text-red-400
                            @elseif($ticket->priority == 'medium') bg-amber-500/20 text-amber-400
                            @else bg-blue-500/20 text-blue-400 @endif">
                            {{ $ticket->priority }}
                        </span>
                    </div>
                    <p class="text-xs text-slate-400 mb-2 truncate">{{ $ticket->client->name ?? 'Cliente Desconhecido' }}</p>
                    <div class="flex items-center justify-between text-xs text-slate-500">
                        <span>Há {{ $ticket->created_at->diffForHumans() }}</span>
                        <span>{{ $ticket->status }}</span>
                    </div>
                </div>
                @empty
                <div class="h-full flex flex-col items-center justify-center text-center pb-8 border border-dashed border-white/10 rounded-xl bg-slate-950/50">
                    <svg class="h-10 w-10 text-slate-600 mb-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                    <p class="text-sm text-slate-500">Nenhum ticket pendente.</p>
                </div>
                @endforelse
            </div>
            
            <a href="#" class="mt-4 block w-full py-3 px-4 rounded-xl border border-white/10 text-sm font-medium text-white text-center hover:bg-white/5 transition-colors">
                Ver todos os tickets
            </a>
        </div>

    </div>
@endsection
