@extends('admin.layout')

@section('title', '')

@section('content')
    <div class="card">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0"><i class="bi bi-building me-1"></i> Sekolah</h5>
            <p class="mb-0 small text-white-50">Halaman manajemen data sekolah</p>
        </div>
        <div class="card-body">
                <div class="d-flex justify-content-between mb-3">
                    <div>
                        <p class="mb-0">Data Sekolah Sesuai Tahun Pelajaran</p>
                        @php
                            $activePeriode = \App\Services\PeriodeService::getActive();
                        @endphp
                        @if($activePeriode)
                            <small class="text-muted">Tahun Pelajaran: {{ $activePeriode->tahun_pelajaran }} &middot; Semester: {{ $activePeriode->semester }}</small>
                        @else
                            <small class="text-muted">Belum ada periode aktif</small>
                        @endif
                    </div>
                    <div>
                        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahSekolahModal">Tambah Sekolah</button>
                    </div>
                </div>

                @if(session('success'))
                    <div class="alert alert-success">{{ session('success') }}</div>
                @endif

                <div class="table-responsive">
                    <table class="table table-bordered table-striped" style="table-layout: fixed;">
                        <thead>
                                <tr>
                                    <th style="width:80px;">NO</th>
                                    <th style="width:320px;">Nama Sekolah</th>
                                    <th style="width:120px;">NSS</th>
                                    <th style="width:120px;">NPSN</th>
                                    <th style="width:240px;">Kepala Sekolah</th>
                                    <th style="width:160px;">NIP</th>
                                    <th style="width:160px;">Kelurahan</th>
                                    <th style="width:160px;">Kecamatan</th>
                                    <th style="width:180px;">Kota/Kabupaten</th>
                                    <th style="width:120px;">Provinsi</th>
                                    <th style="width:420px;">Alamat</th>
                                    <th style="width:140px;">Telepon</th>
                                    <th style="width:220px;">Email</th>
                                    <th style="width:180px;">Tanggal Rapot 1</th>
                                    <th style="width:180px;">Tanggal Rapot 2</th>
                                    <th style="width:140px;">Aksi</th>
                                </tr>
                        </thead>
                        <tbody>
                            @forelse($sekolahs as $s)
                                <tr>
                                    <td>{{ ($sekolahs->firstItem() ?? 0) + $loop->index }}</td>
                                    <td>{{ $s->nama }}</td>
                                    <td>{{ $s->nss }}</td>
                                    <td>{{ $s->npsn }}</td>
                                    <td>{{ $s->kepala_sekolah }}</td>
                                    <td>{{ $s->nip }}</td>
                                    <td>{{ $s->kelurahan }}</td>
                                    <td>{{ $s->kecamatan }}</td>
                                    <td>{{ $s->kota_kabupaten }}</td>
                                    <td>{{ $s->provinsi }}</td>
                                    <td>{{ Str::limit($s->alamat, 60) }}</td>
                                    <td>{{ $s->telepon }}</td>
                                    <td>{{ $s->email }}</td>
                                    <td>{{ $s->tanggal_rapot_1 }}</td>
                                    <td>{{ $s->tanggal_rapot_2 }}</td>
                                    <td>
                                        <button class="btn btn-sm btn-primary btn-edit" 
                                            data-id="{{ $s->id }}"
                                            data-nama="{{ $s->nama }}"
                                            data-npsn="{{ $s->npsn }}"
                                            data-alamat="{{ $s->alamat }}"
                                            data-telepon="{{ $s->telepon }}"
                                            data-email="{{ $s->email }}"
                                            data-tanggal_rapot_1="{{ $s->tanggal_rapot_1 }}"
                                            data-tanggal_rapot_2="{{ $s->tanggal_rapot_2 }}"
                                            data-bs-toggle="modal" data-bs-target="#editSekolahModal" title="Edit">
                                            <i class="bi bi-pencil" aria-hidden="true"></i>
                                        </button>

                                        <form action="{{ url('admin/data-umum/sekolah/' . $s->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Yakin ingin menghapus sekolah ini?');">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger" title="Hapus">
                                                <i class="bi bi-trash" aria-hidden="true"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="16" class="text-center">Belum ada data sekolah.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                @include('admin.data-umum.sekolah.create')
                @include('admin.data-umum.sekolah.edit')

                <div class="d-flex justify-content-end mt-3">
                    {{ $sekolahs instanceof \Illuminate\Pagination\LengthAwarePaginator ? $sekolahs->links() : '' }}
                </div>

                <script>
                    document.addEventListener('DOMContentLoaded', function () {
                        var editButtons = document.querySelectorAll('.btn-edit');
                        editButtons.forEach(function (btn) {
                            btn.addEventListener('click', function () {
                                var id = this.dataset.id;
                                document.getElementById('edit_nama').value = this.dataset.nama || '';
                                document.getElementById('edit_npsn').value = this.dataset.npsn || '';
                                document.getElementById('edit_alamat').value = this.dataset.alamat || '';
                                document.getElementById('edit_telepon').value = this.dataset.telepon || '';
                                        document.getElementById('edit_email').value = this.dataset.email || '';
                                        document.getElementById('edit_tanggal_rapot_1').value = this.dataset.tanggal_rapot_1 || '';
                                        document.getElementById('edit_tanggal_rapot_2').value = this.dataset.tanggal_rapot_2 || '';
                                var form = document.getElementById('editSekolahForm');
                                form.action = '/admin/data-umum/sekolah/' + id;
                            });
                        });
                    });
                </script>
        </div>
    </div>
@endsection
