@extends('guru.layout')

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

@section('content')
    <div class="card">
        <div class="card-body" style="background-color: #f686e5ff; border: none;">
            <h2 class="card-title"><i class="bi bi-person-badge me-1"></i> Halo, {{ optional(auth()->user())->name ?? 'Guru' }}</h2>
            @php
                try {
                    $userId = optional(auth()->user())->id;
                    $classes = \App\Models\Kelas::where('walikelas_id', $userId)->get();
                } catch (\Throwable $e) {
                    $classes = collect();
                }

                $classNames = $classes->pluck('nama')->filter()->values();
                $totalPeserta = 0;
                try {
                    if (class_exists(\App\Models\DidikPokok::class) && $classNames->isNotEmpty()) {
                        $totalPeserta = \App\Models\DidikPokok::whereIn('kelas', $classNames->toArray())->count();
                    }
                } catch (\Throwable $e) {
                    $totalPeserta = 0;
                }
            @endphp

            @if($classNames->isNotEmpty())
                <p class="text-muted card-text">Walikelas {{ $classNames->implode(', ') }} dengan jumlah peserta didik {{ $totalPeserta }}</p>
            @else
                <p class="text-muted card-text">Daftar diri anda pada admin sebagai guru walikelas</p>
            @endif
        </div>
    </div>
@endsection
