@extends('layouts.app')

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

@section('content')
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-person-badge text-primary"></i>
                Data Murid
            </h2>
            <p class="text-muted mb-0">Kelola data siswa di {{ $school->name }}</p>
        </div>
        <div>
            <button class="btn btn-success me-2" data-bs-toggle="modal" data-bs-target="#importModal">
                <i class="bi bi-file-earmark-excel me-1"></i>
                Import Excel
            </button>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addMuridModal">
                <i class="bi bi-plus-circle me-1"></i>
                Tambah Murid
            </button>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="rounded-circle bg-primary bg-opacity-10 p-3">
                                <i class="bi bi-people-fill fs-4 text-primary"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="text-muted mb-1">Total Murid</h6>
                            <h3 class="mb-0">{{ $stats['total'] }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="rounded-circle bg-success bg-opacity-10 p-3">
                                <i class="bi bi-check-circle-fill fs-4 text-success"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="text-muted mb-1">Murid Aktif</h6>
                            <h3 class="mb-0">{{ $stats['aktif'] }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="rounded-circle bg-info bg-opacity-10 p-3">
                                <i class="bi bi-gender-male fs-4 text-info"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="text-muted mb-1">Laki-laki</h6>
                            <h3 class="mb-0">{{ $stats['laki_laki'] }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="rounded-circle bg-danger bg-opacity-10 p-3">
                                <i class="bi bi-gender-female fs-4 text-danger"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="text-muted mb-1">Perempuan</h6>
                            <h3 class="mb-0">{{ $stats['perempuan'] }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Section -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">Filter Kelas</label>
                    <select class="form-select" id="filterKelas">
                        <option value="">Semua Kelas</option>
                        <option value="X">Kelas X</option>
                        <option value="XI">Kelas XI</option>
                        <option value="XII">Kelas XII</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Filter Status</label>
                    <select class="form-select" id="filterStatus">
                        <option value="">Semua Status</option>
                        <option value="Aktif">Aktif</option>
                        <option value="Lulus">Lulus</option>
                        <option value="Pindah">Pindah</option>
                        <option value="Keluar">Keluar</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Filter Jenis Kelamin</label>
                    <select class="form-select" id="filterJK">
                        <option value="">Semua</option>
                        <option value="L">Laki-laki</option>
                        <option value="P">Perempuan</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Tahun Masuk</label>
                    <select class="form-select" id="filterTahun">
                        <option value="">Semua Tahun</option>
                        <option value="2024">2024</option>
                        <option value="2023">2023</option>
                        <option value="2022">2022</option>
                        <option value="2021">2021</option>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <!-- Murid Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white py-3">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-table me-2"></i>
                    Daftar Murid
                </h5>
                <div>
                    <button class="btn btn-sm btn-outline-primary" onclick="window.print()">
                        <i class="bi bi-printer me-1"></i>
                        Cetak
                    </button>
                    <button class="btn btn-sm btn-outline-success">
                        <i class="bi bi-file-earmark-excel me-1"></i>
                        Export Excel
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle" id="muridTable">
                    <thead class="table-light">
                        <tr>
                            <th width="5%">No</th>
                            <th width="10%">NISN</th>
                            <th width="8%">NIS</th>
                            <th width="22%">Nama Lengkap</th>
                            <th width="8%">L/P</th>
                            <th width="12%">Kelas</th>
                            <th width="10%">Tahun Masuk</th>
                            <th width="10%">Status</th>
                            <th width="15%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($murid as $index => $item)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td><strong>{{ $item['nisn'] }}</strong></td>
                            <td>{{ $item['nis'] }}</td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="avatar-sm {{ $item['jenis_kelamin'] == 'L' ? 'bg-info' : 'bg-danger' }} bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-2">
                                        <i class="bi {{ $item['jenis_kelamin'] == 'L' ? 'bi-gender-male text-info' : 'bi-gender-female text-danger' }}"></i>
                                    </div>
                                    <strong>{{ $item['nama'] }}</strong>
                                </div>
                            </td>
                            <td>
                                @if($item['jenis_kelamin'] == 'L')
                                    <span class="badge bg-info">L</span>
                                @else
                                    <span class="badge bg-danger">P</span>
                                @endif
                            </td>
                            <td>
                                <span class="badge bg-primary bg-opacity-75">{{ $item['kelas'] }}</span>
                            </td>
                            <td>{{ $item['tahun_masuk'] }}</td>
                            <td>
                                @if($item['status'] == 'Aktif')
                                    <span class="badge bg-success">
                                        <i class="bi bi-check-circle me-1"></i>{{ $item['status'] }}
                                    </span>
                                @else
                                    <span class="badge bg-secondary">{{ $item['status'] }}</span>
                                @endif
                            </td>
                            <td class="text-center">
                                <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-sm btn-info" title="Detail">
                                        <i class="bi bi-eye"></i>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-warning" title="Edit">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-danger" title="Hapus">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" class="text-center py-4">
                                <i class="bi bi-inbox fs-1 text-muted d-block mb-2"></i>
                                <p class="text-muted mb-0">Belum ada data murid</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Info Card -->
    <div class="card border-0 shadow-sm mt-4">
        <div class="card-body">
            <div class="d-flex align-items-start">
                <div class="flex-shrink-0">
                    <i class="bi bi-info-circle-fill fs-4 text-info"></i>
                </div>
                <div class="flex-grow-1 ms-3">
                    <h6 class="mb-2">Catatan</h6>
                    <ul class="text-muted mb-0 small">
                        <li>Data murid dapat diimport dari file Excel untuk mempermudah input data massal</li>
                        <li>NISN (Nomor Induk Siswa Nasional) harus unik dan terdiri dari 10 digit</li>
                        <li>Pastikan data murid selalu diperbarui setiap awal tahun ajaran</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    $(document).ready(function() {
        const table = $('#muridTable').DataTable({
            responsive: true,
            language: {
                url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json'
            },
            pageLength: 25,
            order: [[3, 'asc']], // Sort by name
            dom: 'Bfrtip',
            buttons: []
        });

        // Filter handlers
        $('#filterKelas, #filterStatus, #filterJK, #filterTahun').on('change', function() {
            table.draw();
        });

        // Custom filtering function
        $.fn.dataTable.ext.search.push(
            function(settings, data, dataIndex) {
                const kelasFilter = $('#filterKelas').val();
                const statusFilter = $('#filterStatus').val();
                const jkFilter = $('#filterJK').val();
                const tahunFilter = $('#filterTahun').val();
                
                const kelas = data[5]; // Kelas column
                const status = data[7]; // Status column  
                const jk = data[4]; // Jenis Kelamin column
                const tahun = data[6]; // Tahun Masuk column
                
                if (kelasFilter && !kelas.includes(kelasFilter)) return false;
                if (statusFilter && !status.includes(statusFilter)) return false;
                if (jkFilter && !jk.includes(jkFilter)) return false;
                if (tahunFilter && tahun !== tahunFilter) return false;
                
                return true;
            }
        );
    });
</script>
@endpush
@endsection
