<x-app-layout>
    <div class="max-w-6xl mx-auto p-6">
        <h2 class="text-2xl font-bold mb-6">Rekap Kuesioner Minggu ke-{{ $minggu }} Tahun {{ $tahun }}</h2>

        <div class="mb-4">
            <form method="GET" class="flex gap-4">
                <input type="number" name="minggu" value="{{ $minggu }}" placeholder="Minggu ke-"
                    class="border rounded px-2 py-1">
                <input type="number" name="tahun" value="{{ $tahun }}" placeholder="Tahun"
                    class="border rounded px-2 py-1">
                <button type="submit" class="bg-blue-500 text-white px-4 py-1 rounded">Filter</button>
            </form>
        </div>

        <table class="w-full table-auto border">
            <thead>
                <tr class="bg-gray-200">
                    <th class="border px-4 py-2">Nama Siswa</th>
                    @foreach ($allDimensi as $dimensi)
                        <th class="border px-4 py-2">{{ $dimensi }}</th>
                    @endforeach
                    <th class="border px-4 py-2">Dimensi Keseluruhan</th>
                    <th class="border px-4 py-2">Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($siswaList as $siswa)
                    <tr class="text-sm bg-white">
                        <td class="border px-4 py-2">{{ $siswa->nama_siswa }}</td>

                        {{-- Loop dimensi dan hitung total --}}
                        @php
                            $totalDimensi = 0;
                            $countDimensi = 0;
                        @endphp

                        @foreach ($allDimensi as $id => $namaDimensi)
                            <td
                                class="border px-4 py-2 text-center 
                @isset($rekap[$siswa->id_siswa][$namaDimensi][0])
                    {{ $rekap[$siswa->id_siswa][$namaDimensi][0]->rata_rata >= 4 ? 'bg-red-200 font-bold' : '' }} 
                @endisset">
                                @isset($rekap[$siswa->id_siswa][$namaDimensi][0])
                                    @php
                                        $nilai = $rekap[$siswa->id_siswa][$namaDimensi][0]->rata_rata;
                                        $totalDimensi += $nilai;
                                        $countDimensi++;
                                    @endphp
                                    {{ number_format($nilai, 2) }}
                                @else
                                    -
                                @endisset
                            </td>
                        @endforeach

                        {{-- Tampilkan total --}}
                        <td class="border px-4 py-2 text-center">
                            {{ $countDimensi > 0 ? number_format($totalDimensi, 2) : '-' }}
                        </td>

                        <td class="border px-4 py-2 text-center">
                            <a href="{{ route('rekap-kuesioner.detail', $siswa->id_siswa) }}"
                                class="text-blue-500 underline">Detail</a>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</x-app-layout>
