@extends('layouts.app')

@section('title', 'Data Siswa')

@section('content')
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-12">
            <h2 class="h4 mb-0">Data Siswa</h2>
            <p class="text-muted">Kelola data siswa berkebutuhan khusus</p>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card shadow-sm mb-3">
        <div class="card-body">
            <form action="{{ route('students.index') }}" method="GET" class="row g-3">
                <div class="col-md-3">
                    <input type="text" 
                           class="form-control" 
                           name="search" 
                           placeholder="Cari nama, NIK, NISN, NIS..." 
                           value="{{ request('search') }}">
                </div>
                
                @if(in_array(Auth::user()->role->slug, ['super-admin', 'admin-kcd']))
                    <div class="col-md-3">
                        <select class="form-select" name="school_id">
                            <option value="">Semua Sekolah</option>
                            @foreach($schools as $school)
                                <option value="{{ $school->id }}" {{ request('school_id') == $school->id ? 'selected' : '' }}>
                                    {{ $school->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                @endif
                
                <div class="col-md-2">
                    <select class="form-select" name="kelas">
                        <option value="">Semua Kelas</option>
                        @foreach(['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'] as $kelas)
                            <option value="{{ $kelas }}" {{ request('kelas') == $kelas ? 'selected' : '' }}>
                                Kelas {{ $kelas }}
                            </option>
                        @endforeach
                    </select>
                </div>
                
                <div class="col-md-2">
                    <select class="form-select" name="disability_type_id">
                        <option value="">Semua Ketunaan</option>
                        @foreach($disabilityTypes as $type)
                            <option value="{{ $type->id }}" {{ request('disability_type_id') == $type->id ? 'selected' : '' }}>
                                {{ $type->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                
                <div class="col-md-2">
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search"></i> Cari
                        </button>
                        <a href="{{ route('students.index') }}" class="btn btn-secondary">
                            <i class="bi bi-arrow-clockwise"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="mb-0">Daftar Siswa</h5>
            <a href="{{ route('students.create') }}" class="btn btn-light btn-sm">
                <i class="bi bi-plus-circle me-1"></i> Tambah Siswa
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover table-striped">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 3%">#</th>
                            <th style="width: 15%">Nama</th>
                            <th style="width: 10%">NIK</th>
                            <th style="width: 8%">NISN</th>
                            <th style="width: 8%">NIS</th>
                            <th style="width: 5%" class="text-center">Kelas</th>
                            @if(in_array(Auth::user()->role->slug, ['super-admin', 'admin-kcd']))
                                <th style="width: 15%">Sekolah</th>
                            @endif
                            <th style="width: 12%">Ketunaan</th>
                            <th style="width: 8%">TTL</th>
                            <th style="width: 10%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($students as $index => $student)
                            <tr>
                                <td>{{ $students->firstItem() + $index }}</td>
                                <td>
                                    <strong>{{ $student->nama }}</strong>
                                    @if($student->jenis_kelamin)
                                        <small class="text-muted">({{ $student->jenis_kelamin }})</small>
                                    @endif
                                </td>
                                <td><small>{{ $student->nik }}</small></td>
                                <td><small>{{ $student->nisn }}</small></td>
                                <td>{{ $student->nis }}</td>
                                <td class="text-center">
                                    <span class="badge bg-info">{{ $student->kelas }}</span>
                                </td>
                                @if(in_array(Auth::user()->role->slug, ['super-admin', 'admin-kcd']))
                                    <td><small>{{ $student->school->name ?? '-' }}</small></td>
                                @endif
                                <td>
                                    @if($student->disabilityType)
                                        <span class="badge bg-warning text-dark">{{ $student->disabilityType->code }}</span>
                                        {{ $student->disabilityType->name }}
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>
                                <td>
                                    <small>{{ $student->tempat_lahir }}, {{ $student->tanggal_lahir->format('d/m/Y') }}</small>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{{ route('students.show', $student) }}" 
                                           class="btn btn-outline-info" 
                                           title="Detail">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        <a href="{{ route('students.edit', $student) }}" 
                                           class="btn btn-outline-primary" 
                                           title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <form action="{{ route('students.destroy', $student) }}" 
                                              method="POST" 
                                              class="d-inline"
                                              onsubmit="return confirm('Yakin ingin menghapus data siswa ini?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" 
                                                    class="btn btn-outline-danger" 
                                                    title="Hapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="{{ in_array(Auth::user()->role->slug, ['super-admin', 'admin-kcd']) ? '10' : '9' }}" class="text-center py-4">
                                    <i class="bi bi-inbox text-muted" style="font-size: 3rem;"></i>
                                    <p class="text-muted mt-2">Belum ada data siswa</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($students->hasPages())
                <div class="mt-3">
                    {{ $students->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
