@extends('guru.layout')

@section('title', '')

@section('content')

<style>
    .bg-pink { background-color: #6b353dff !important; }
    .table-fixed th, .table-fixed td { white-space: nowrap; }
    .table-bordered th, .table-bordered td { border: 1px solid #dee2e6 !important; }
    .small-col { width: 120px; }
    .phone-col { width: 140px; }
</style>

<div class="card">
    <div class="card-header bg-pink text-white d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-0"><i class="bi bi-star me-1"></i> Walimurid</h5>
                <p class="mb-0 small text-white-50">Halaman Data Walimurid Peserta Didik</p>
            </div>
        </div>
        @php
            $activePeriode = \App\Models\Periode::where('status', \App\Models\Periode::STATUS_AKTIF)->first();
            $tahunPel = $activePeriode->tahun_pelajaran ?? '-';
            $semester = $activePeriode->semester ?? '-';
        @endphp
        <div class="card-body">
            <div class="mb-3 d-flex justify-content-between align-items-start">
                <div>
                    <h5 class="mb-1">Data Walimurid Peserta Didik</h5>
                    <div class="small text-muted">Tahun Pelajaran: <strong>{{ $tahunPel }}</strong> &nbsp;|&nbsp; Semester: <strong>{{ $semester }}</strong></div>
                </div>
             </div>

            <form method="GET" class="mb-3">
                <div class="row g-2 align-items-end">
                    <div class="col-md-6">
                        <label class="form-label">Pilih Kelas</label>
                        <select name="kelas_id" class="form-select" onchange="this.form.submit()">
                            <option value="">-- Pilih Kelas --</option>
                            @foreach($kelasList ?? [] as $k)
                                <option value="{{ $k->id }}" @if(isset($selected) && $selected && $selected->id == $k->id) selected @endif>{{ $k->nama }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-6">
                        <a href="{{ url('guru/pendukung/walimurid') }}" class="btn btn-secondary">Reset</a>
                    </div>
                </div>
            </form>

            @if(isset($selected) && $selected)
                <div class="mb-2"><strong>Menampilkan data untuk :</strong> {{ $selected->nama }} </div>

                <div class="table-responsive">
                    <table class="table table-striped table-sm table-fixed table-bordered">
                        <thead>
                            <tr>
                                <th>Nama Peserta Didik</th>
                                <th class="small-col">NIS</th>
                                <th class="small-col">NISN</th>
                                <th class="small-col">Tempat Lahir</th>
                                <th class="small-col">Tanggal Lahir</th>
                                <th>Nama Ayah</th>
                                <th class="small-col">Pekerjaan Ayah</th>
                                <th>Nama Ibu</th>
                                <th class="small-col">Pekerjaan Ibu</th>
                                <th>Alamat</th>
                                <th class="phone-col">Nomor HP/WA</th>
                                <th class="small-col">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($pesertas as $p)
                                <tr>
                                    <td>{{ $p->nama ?? '-' }}</td>
                                    <td>{{ $p->nis ?? '-' }}</td>
                                    <td>{{ $p->nisn ?? '-' }}</td>
                                    <td>{{ $p->tempat_lahir ?? '-' }}</td>
                                    <td>
                                        @if(!empty($p->tanggal_lahir) && $p->tanggal_lahir instanceof \Illuminate\Support\Carbon) {{ $p->tanggal_lahir->format('Y-m-d') }} @else {{ $p->tanggal_lahir ?? '-' }} @endif
                                    </td>
                                    <td>{{ $p->nama_ayah ?? '-' }}</td>
                                    <td>{{ $p->pekerjaan_ayah ?? '-' }}</td>
                                    <td>{{ $p->nama_ibu ?? '-' }}</td>
                                    <td>{{ $p->pekerjaan_ibu ?? '-' }}</td>
                                    <td>{{ $p->alamat ?? '-' }}</td>
                                    <td>{{ $p->nomor_hp ?? ($p->hp ?? '-') }}</td>
                                    <td class="small-col">
                                        <a href="{{ url('guru/pendukung/walimurid/'.$p->id.'/edit') }}" class="btn btn-sm btn-primary btn-icon me-1" title="Edit"><i class="bi bi-pencil"></i></a>
                                        <form action="{{ url('guru/pendukung/walimurid/'.$p->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Hapus data ini?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="12" class="text-center">Tidak ada data peserta untuk kelas ini.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            @endif

@endsection