@extends('layouts.app-universal-v2')

@section('title', 'Dashboard SuperAdmin')

@section('content')
<div class="container-fluid">
    <!-- Header SuperAdmin -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="bg-gradient-warning text-dark p-4 rounded shadow-sm">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h1 class="h2 mb-1"><i class="fas fa-crown me-2"></i>Dashboard SuperAdmin</h1>
                        <p class="mb-0 opacity-75">Pannello di controllo amministrativo avanzato per NetGesCon</p>
                    </div>
                    <div class="text-end">
                        <div class="badge bg-danger fs-6">SUPER ADMIN</div>
                        <div class="small mt-1">{{ auth()->user()->name }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="display-6 text-primary mb-2">
                        <i class="fas fa-users"></i>
                    </div>
                    <h5 class="card-title">{{ \App\Models\User::count() }}</h5>
                    <p class="card-text text-muted">Utenti Totali</p>
                    <a href="{{ route('superadmin.users.index') }}" class="btn btn-outline-primary btn-sm">Gestisci</a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="display-6 text-success mb-2">
                        <i class="fas fa-building"></i>
                    </div>
                    <h5 class="card-title">{{ \App\Models\Stabile::count() }}</h5>
                    <p class="card-text text-muted">Stabili Gestiti</p>
                    <a href="{{ route('admin.stabili.index') }}" class="btn btn-outline-success btn-sm">Visualizza</a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="display-6 text-info mb-2">
                        <i class="fas fa-database"></i>
                    </div>
                    <h5 class="card-title">{{ \Illuminate\Support\Facades\DB::table('comuni_italiani')->count() }}</h5>
                    <p class="card-text text-muted">Comuni in Archivio</p>
                    <a href="{{ route('superadmin.archivi.comuni') }}" class="btn btn-outline-info btn-sm">Visualizza</a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="display-6 text-warning mb-2">
                        <i class="fas fa-ticket-alt"></i>
                    </div>
                    <h5 class="card-title">{{ \App\Models\Ticket::where('stato', 'aperto')->count() }}</h5>
                    <p class="card-text text-muted">Ticket Aperti</p>
                    <a href="{{ route('admin.tickets.index') }}" class="btn btn-outline-warning btn-sm">Gestisci</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Dashboard Content -->
    <div class="row">
        <!-- Gestione Sistema -->
        <div class="col-lg-6 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-cogs me-2"></i>Gestione Sistema
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-6">
                            <a href="{{ route('superadmin.archivi.index') }}" class="btn btn-outline-primary w-100 h-100 d-flex flex-column justify-content-center">
                                <i class="fas fa-database fa-2x mb-2"></i>
                                <span>Archivi Sistema</span>
                            </a>
                        </div>
                        <div class="col-6">
                            <a href="{{ route('superadmin.users.index') }}" class="btn btn-outline-success w-100 h-100 d-flex flex-column justify-content-center">
                                <i class="fas fa-users fa-2x mb-2"></i>
                                <span>Gestione Utenti</span>
                            </a>
                        </div>
                        <div class="col-6">
                            <a href="{{ route('superadmin.impostazioni.index') }}" class="btn btn-outline-info w-100 h-100 d-flex flex-column justify-content-center">
                                <i class="fas fa-cog fa-2x mb-2"></i>
                                <span>Impostazioni</span>
                            </a>
                        </div>
                        <div class="col-6">
                            <button class="btn btn-outline-warning w-100 h-100 d-flex flex-column justify-content-center" onclick="showDiagnostica()">
                                <i class="fas fa-chart-line fa-2x mb-2"></i>
                                <span>Diagnostica</span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Attività Recenti -->
        <div class="col-lg-6 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-clock me-2"></i>Attività Recenti
                    </h5>
                </div>
                <div class="card-body">
                    <div class="timeline">
                        @php
                            $recent_logs = \Illuminate\Support\Facades\DB::table('import_logs')
                                ->latest()
                                ->limit(5)
                                ->get();
                        @endphp
                        
                        @if($recent_logs->count() > 0)
                            @foreach($recent_logs as $log)
                            <div class="timeline-item mb-3">
                                <div class="d-flex">
                                    <div class="flex-shrink-0">
                                        <div class="timeline-icon bg-primary text-white">
                                            <i class="fas fa-upload"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <h6 class="mb-1">Import {{ ucfirst($log->tipo) }}</h6>
                                        <p class="text-muted mb-1 small">{{ $log->records_importati }} record importati</p>
                                        <small class="text-muted">{{ \Carbon\Carbon::parse($log->created_at)->diffForHumans() }}</small>
                                    </div>
                                </div>
                            </div>
                            @endforeach
                        @else
                        <div class="text-center text-muted py-4">
                            <i class="fas fa-clock fa-2x mb-3"></i>
                            <p>Nessuna attività recente</p>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Sistema Status & Tools -->
    <div class="row">
        <div class="col-12">
            <div class="card border-0 shadow-sm">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-server me-2"></i>Stato Sistema & Strumenti
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row g-4">
                        <!-- Stato Database -->
                        <div class="col-md-4">
                            <div class="p-3 border rounded">
                                <h6><i class="fas fa-database text-primary me-2"></i>Database</h6>
                                <div class="d-flex justify-content-between align-items-center">
                                    <span class="text-muted">Stato:</span>
                                    <span class="badge bg-success">Online</span>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mt-2">
                                    <span class="text-muted">Dimensione:</span>
                                    <span>{{ number_format(\Illuminate\Support\Facades\DB::table('information_schema.tables')->where('table_schema', config('database.connections.mysql.database'))->sum('data_length + index_length') / 1024 / 1024, 2) }} MB</span>
                                </div>
                                <button class="btn btn-outline-primary btn-sm mt-2 w-100" onclick="checkDatabase()">
                                    <i class="fas fa-check me-1"></i>Verifica
                                </button>
                            </div>
                        </div>

                        <!-- Backup System -->
                        <div class="col-md-4">
                            <div class="p-3 border rounded">
                                <h6><i class="fas fa-save text-success me-2"></i>Backup</h6>
                                <div class="d-flex justify-content-between align-items-center">
                                    <span class="text-muted">Ultimo:</span>
                                    <span>Mai configurato</span>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mt-2">
                                    <span class="text-muted">Automatico:</span>
                                    <span class="badge bg-warning">Da Configurare</span>
                                </div>
                                <button class="btn btn-outline-success btn-sm mt-2 w-100" onclick="createBackup()">
                                    <i class="fas fa-download me-1"></i>Backup Ora
                                </button>
                            </div>
                        </div>

                        <!-- Performance -->
                        <div class="col-md-4">
                            <div class="p-3 border rounded">
                                <h6><i class="fas fa-tachometer-alt text-info me-2"></i>Performance</h6>
                                <div class="d-flex justify-content-between align-items-center">
                                    <span class="text-muted">Velocità:</span>
                                    <span class="badge bg-success">Ottima</span>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mt-2">
                                    <span class="text-muted">Cache:</span>
                                    <span class="badge bg-success">Attiva</span>
                                </div>
                                <button class="btn btn-outline-info btn-sm mt-2 w-100" onclick="clearCache()">
                                    <i class="fas fa-broom me-1"></i>Pulisci Cache
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@push('styles')
<style>
.timeline-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.bg-gradient-warning {
    background: linear-gradient(135deg, #ffc107 0%, #f39c12 100%);
}
</style>
@endpush

@push('scripts')
<script>
function showDiagnostica() {
    alert('Diagnostica sistema - Funzionalità in sviluppo');
}

function checkDatabase() {
    alert('Database verificato correttamente');
}

function createBackup() {
    if (confirm('Creare un backup completo del sistema?')) {
        alert('Backup creato con successo (simulazione)');
    }
}

function clearCache() {
    if (confirm('Pulire la cache del sistema?')) {
        alert('Cache pulita con successo (simulazione)');
    }
}
</script>
@endpush
                            <span class="text-sm font-medium text-gray-900 dark:text-white">Cache</span>
                        </div>
                        <span class="text-sm text-green-600 dark:text-green-400">Attiva</span>
                    </div>
                    
                    <!-- Ultimo Backup -->
                    <div class="flex items-center justify-between p-3 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg">
                        <div class="flex items-center">
                            <div class="w-3 h-3 bg-yellow-500 rounded-full mr-3"></div>
                            <span class="text-sm font-medium text-gray-900 dark:text-white">Ultimo Backup</span>
                        </div>
                        <span class="text-sm text-yellow-600 dark:text-yellow-400">2 ore fa</span>
                    </div>
                </div>
            </div>
        </div>
        
    </div>
    
</div>
@endsection
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 dark:text-gray-400 truncate">Amministratori</dt>
                            <dd class="text-lg font-medium text-gray-900 dark:text-white">{{ \App\Models\Amministratore::count() }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>

        <!-- Utenti per Ruolo -->
        <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
            <div class="p-6">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <div class="w-8 h-8 bg-purple-500 rounded-full flex items-center justify-center">
                            <svg class="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M3 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd"></path>
                            </svg>
                        </div>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 dark:text-gray-400 truncate">Super Admin</dt>
                            <dd class="text-lg font-medium text-gray-900 dark:text-white">{{ \App\Models\User::role('super-admin')->count() }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Azioni Rapide -->
    <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
        <div class="p-6 bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-medium text-gray-900 mb-4">Azioni Rapide</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <a href="{{ route('superadmin.users.create') }}" 
                   class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded text-center transition duration-200">
                    Nuovo Utente
                </a>
                <a href="{{ route('superadmin.amministratori.create') }}" 
                   class="bg-green-500 hover:bg-green-700 text-white font-bold py-3 px-4 rounded text-center transition duration-200">
                    Nuovo Amministratore
                </a>
                <a href="{{ route('superadmin.users.index') }}" 
                   class="bg-indigo-500 hover:bg-indigo-700 text-white font-bold py-3 px-4 rounded text-center transition duration-200">
                    Gestisci Utenti
                </a>
                <a href="{{ route('superadmin.amministratori.index') }}" 
                   class="bg-purple-500 hover:bg-purple-700 text-white font-bold py-3 px-4 rounded text-center transition duration-200">
                    Gestisci Amministratori
                </a>
            </div>
        </div>
    </div>

    <!-- Ultimi Utenti Creati -->
    <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
        <div class="p-6 bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
            <h3 class="text-lg font-medium text-gray-900 mb-4">Ultimi Utenti Creati</h3>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Nome</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Email</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Ruolo</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Data Creazione</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200">
                        @foreach(\App\Models\User::latest()->take(5)->get() as $user)
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-white">{{ $user->name }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">{{ $user->email }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">
                                    @foreach($user->roles as $role)
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 mr-1">
                                            {{ $role->name }}
                                        </span>
                                    @endforeach
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">{{ $user->created_at->format('d/m/Y H:i') }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</x-layout.universal>