@extends('layouts.app')

@section('title', 'Data Siswa Magang - MagangTime')
@section('page-title', 'Data Siswa Magang')

@section('content')
<div class="card">
    <div class="card-header bg-white">
        <div class="row align-items-center">
            <div class="col-12 col-md-6 mb-2 mb-md-0">
                <h5 class="mb-0"><i class="fas fa-user-graduate me-2"></i>Data Siswa Magang</h5>
            </div>
            <div class="col-12 col-md-6 text-md-end">
                <button class="btn btn-primary w-100 w-md-auto" data-bs-toggle="modal" data-bs-target="#addSiswaModal">
                    <i class="fas fa-plus me-2"></i>Tambah Siswa
                </button>
            </div>
        </div>

        <!-- Form Filter dan Pencarian -->
        <form method="GET" action="{{ route('admin.siswa') }}" class="mt-3">
            <div class="row g-2">
                <div class="col-12 col-md-3">
                    <label class="form-label">Cari Siswa</label>
                    <input type="text" name="search" class="form-control" 
                           placeholder="Cari nama siswa atau sekolah..." 
                           value="{{ request('search') }}">
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label">Jenis Kelamin</label>
                    <select name="jk" class="form-select">
                        <option value="">Semua JK</option>
                        <option value="L" {{ request('jk') == 'L' ? 'selected' : '' }}>Laki-laki</option>
                        <option value="P" {{ request('jk') == 'P' ? 'selected' : '' }}>Perempuan</option>
                    </select>
                </div>
                <div class="col-6 col-md-3">
                    <label class="form-label">Sekolah</label>
                    <select name="sekolah" class="form-select">
                        <option value="">Semua Sekolah</option>
                        @foreach($sekolah as $sch)
                            <option value="{{ $sch->id }}" {{ request('sekolah') == $sch->id ? 'selected' : '' }}>
                                {{ $sch->nama_sekolah }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label">Status Akun</label>
                    <select name="status_akun" class="form-select">
                        <option value="">Semua Status</option>
                        <option value="ada" {{ request('status_akun') == 'ada' ? 'selected' : '' }}>Sudah Ada Akun</option>
                        <option value="belum" {{ request('status_akun') == 'belum' ? 'selected' : '' }}>Belum Ada Akun</option>
                    </select>
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label d-none d-md-block">&nbsp;</label>
                    <div class="d-flex mt-md-0 mt-2">
                        <button type="submit" class="btn btn-primary flex-fill me-1">
                            <i class="fas fa-search"></i><span class="d-none d-sm-inline ms-1">Cari</span>
                        </button>
                        @if(request()->hasAny(['search', 'jk', 'sekolah', 'status_akun']))
                            <a href="{{ route('admin.siswa') }}" class="btn btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        @endif
                    </div>
                </div>
            </div>
        </form>
    </div>
    
    <div class="card-body">
        @if($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        {{-- Desktop Table --}}
        <div class="table-responsive d-none d-md-block">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th width="5%" class="text-center">No</th>
                        <th>Nama Siswa</th>
                        <th width="15%" class="text-center">Jenis Kelamin</th>
                        <th width="15%" class="text-center">Sekolah</th>
                        <th width="15%" class="text-center">Periode Magang</th>
                        <th width="12%" class="text-center">Status Akun</th>
                        <th width="10%" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($siswa as $i => $s)
                    <tr>
                        <td class="text-center">{{ $siswa->firstItem() + $i }}</td>
                        <td>
                            <div class="fw-bold">{{ $s->nama_siswa }}</div>
                            <small class="text-muted">{{ $s->no_telp_siswa }}</small>
                        </td>
                        <td class="text-center">
                            <span class="badge bg-{{ $s->jk == 'L' ? 'primary' : 'danger' }}">{{ $s->jk == 'L' ? 'Laki-laki' : 'Perempuan' }}</span>
                        </td>
                        <td class="text-center">{{ $s->sekolah->nama_sekolah }}</td>
                        <td class="text-center">
                            <small>{{ $s->mulai_magang->format('d/m/Y') }} s/d<br>{{ $s->selesai_magang->format('d/m/Y') }}</small>
                        </td>
                        <td class="text-center">
                            @if($s->user)
                                <span class="badge bg-success">{{ $s->user->username }}</span>
                            @else
                                <span class="badge bg-secondary">Belum ada akun</span>
                            @endif
                        </td>
                        <td>
                            <div class="btn-group text-center" role="group">
                                <a href="{{ route('admin.detail.siswa', $s->id) }}" class="btn btn-sm btn-info" title="Detail">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <button class="btn btn-sm btn-warning" onclick="editSiswa({{ $s->id }})" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <a href="{{ route('admin.export.pdf', $s->id) }}" class="btn btn-sm btn-success" title="Export PDF">
                                    <i class="fas fa-file-pdf"></i>
                                </a>
                                <button class="btn btn-sm btn-danger" onclick="confirmDelete({{ $s->id }}, '{{ $s->nama_siswa }}')" title="Hapus">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">
                            @if(request()->hasAny(['search', 'jk', 'sekolah', 'status_akun']))
                                <i class="fas fa-search fa-3x mb-3"></i>
                                <p>Tidak ditemukan data siswa dengan kriteria yang dipilih</p>
                                <a href="{{ route('admin.siswa') }}" class="btn btn-primary">Reset Filter</a>
                            @else
                                <i class="fas fa-user-graduate fa-3x mb-3"></i>
                                <p>Belum ada data siswa magang</p>
                            @endif
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Mobile Cards --}}
        <div class="d-block d-md-none">
            @forelse($siswa as $i => $s)
            <div class="card mb-3 border-start border-primary border-4">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <div>
                            <h6 class="fw-bold mb-0">{{ $s->nama_siswa }}</h6>
                            <small class="text-muted">{{ $s->no_telp_siswa }}</small>
                        </div>
                        <small class="text-muted">#{{ $siswa->firstItem() + $i }}</small>
                    </div>
                    
                    <div class="mb-3">
                        <div class="mb-2">
                            <small class="text-muted">Jenis Kelamin:</small><br>
                            <span class="badge bg-{{ $s->jk == 'L' ? 'primary' : 'danger' }}">
                                {{ $s->jk == 'L' ? 'Laki-laki' : 'Perempuan' }}
                            </span>
                        </div>
                        <div class="mb-2">
                            <small class="text-muted">Sekolah:</small><br>
                            <small class="fw-bold">{{ $s->sekolah->nama_sekolah }}</small>
                        </div>
                        <div class="mb-2">
                            <small class="text-muted">Periode Magang:</small><br>
                            <small>{{ $s->mulai_magang->format('d/m/Y') }} s/d {{ $s->selesai_magang->format('d/m/Y') }}</small>
                        </div>
                        <div class="mb-2">
                            <small class="text-muted">Status Akun:</small><br>
                            @if($s->user)
                                <span class="badge bg-success">{{ $s->user->username }}</span>
                            @else
                                <span class="badge bg-secondary">Belum ada akun</span>
                            @endif
                        </div>
                    </div>
                    
                    <div class="row g-1">
                        <div class="col-3">
                            <a href="{{ route('admin.detail.siswa', $s->id) }}" class="btn btn-sm btn-info w-100" title="Detail">
                                <i class="fas fa-eye"></i>
                            </a>
                        </div>
                        <div class="col-3">
                            <button class="btn btn-sm btn-warning w-100" onclick="editSiswa({{ $s->id }})" title="Edit">
                                <i class="fas fa-edit"></i>
                            </button>
                        </div>
                        <div class="col-3">
                            <a href="{{ route('admin.export.pdf', $s->id) }}" class="btn btn-sm btn-success w-100" title="Export PDF">
                                <i class="fas fa-file-pdf"></i>
                            </a>
                        </div>
                        <div class="col-3">
                            <button class="btn btn-sm btn-danger w-100" onclick="confirmDelete({{ $s->id }}, '{{ $s->nama_siswa }}')" title="Hapus">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            @empty
            <div class="text-center text-muted py-5">
                @if(request()->hasAny(['search', 'jk', 'sekolah', 'status_akun']))
                    <i class="fas fa-search fa-3x mb-3"></i>
                    <p>Tidak ditemukan data siswa dengan kriteria yang dipilih</p>
                    <a href="{{ route('admin.siswa') }}" class="btn btn-primary">Reset Filter</a>
                @else
                    <i class="fas fa-user-graduate fa-3x mb-3"></i>
                    <p>Belum ada data siswa magang</p>
                @endif
            </div>
            @endforelse
        </div>
        
        <!-- Pagination -->
        @if($siswa->hasPages())
            {{ $siswa->appends(request()->query())->links('pagination::bootstrap-4') }}
        @endif
    </div>
</div>

<!-- Include Modals -->
@include('admin.siswa.tambah')
@include('admin.siswa.edit')
@include('admin.siswa.hapus')

<!-- Include JavaScript -->
@include('admin.siswa.script')
@endsection