<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row">
    <div class="col-md-4">
        <!-- Kartu Profil -->
        <div class="card mb-3">
            <div class="card-body text-center py-4">
                <div class="mb-3">
                    <?php if (!empty($student['photo'])): ?>
                        <img src="<?= \App\Services\StorageService::getUrl($student['photo'], 'students') ?>" 
                            style="width:80px;height:80px;border-radius:50%;object-fit:cover;margin:0 auto;display:block;border:2px solid #0d6efd">
                    <?php else: ?>
                        <div style="width:80px;height:80px;border-radius:50%;background:#e8f0fe;display:flex;align-items:center;justify-content:center;margin:0 auto;font-size:2.5rem;color:#0d6efd">
                            <i class="bi bi-person-circle"></i>
                        </div>
                    <?php endif; ?>
                </div>
                <h5 class="fw-bold mb-1"><?= esc($student['full_name']) ?></h5>
                <div class="text-muted small mb-2">NIS: <?= esc($student['nis']) ?></div>
                <?php
                $statusColor = [
                    'Aktif'    => 'success',
                    'Alumni'   => 'secondary',
                    'Mutasi'   => 'warning',
                    'Drop Out' => 'danger',
                ];
                $color = $statusColor[$student['status']] ?? 'secondary';
                ?>
                <span class="badge bg-<?= $color ?>"><?= esc($student['status']) ?></span>
            </div>
            <div class="card-footer">
                <div class="d-flex gap-2 justify-content-center flex-wrap">
                    <a href="/student/edit/<?= $student['id'] ?>" class="btn btn-sm btn-primary">
                        <i class="bi bi-pencil me-1"></i>Edit
                    </a>
                    <button type="button" class="btn btn-sm btn-warning" onclick="resetStudentPassword(<?= $student['id'] ?>)">
                        <i class="bi bi-key-fill me-1"></i>Reset Password
                    </button>
                    <a href="/student" class="btn btn-sm btn-outline-secondary">
                        <i class="bi bi-arrow-left me-1"></i>Kembali
                    </a>
                </div>
            </div>
        </div>

        <!-- Info Singkat -->
        <div class="card">
            <div class="card-header fw-semibold">
                <i class="bi bi-info-circle text-primary me-2"></i>Info Singkat
            </div>
            <div class="card-body p-0">
                <table class="table table-sm table-borderless mb-0">
                    <tr>
                        <td class="text-muted ps-3" width="45%">NISN</td>
                        <td><?= esc($student['nisn'] ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">NIK</td>
                        <td><?= esc($student['nik'] ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">No. KIP</td>
                        <td><?= esc($student['kip_no'] ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">Jenis Kelamin</td>
                        <td><?= $student['gender'] == 'L' ? 'Laki-laki' : 'Perempuan' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">Agama</td>
                        <td><?= esc($student['religion']) ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">TTL</td>
                        <td><?= esc($student['birth_place']) ?>, <?= $student['birth_date'] ? date('d M Y', strtotime($student['birth_date'])) : '-' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">Tahun Masuk</td>
                        <td><?= esc($student['entry_year']) ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">Asal Sekolah</td>
                        <td><?= esc($student['origin_school'] ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted ps-3">No. Seri Ijazah</td>
                        <td><?= esc($student['graduation_certificate_no'] ?? '-') ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <ul class="nav nav-tabs card-header-tabs" id="detailTabs">
                    <li class="nav-item">
                        <a class="nav-link active" data-bs-toggle="tab" href="#tabAlamat">
                            <i class="bi bi-geo-alt me-1"></i>Alamat
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" data-bs-toggle="tab" href="#tabOrangtua">
                            <i class="bi bi-people me-1"></i>Orang Tua
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" data-bs-toggle="tab" href="#tabRiwayat">
                            <i class="bi bi-clock-history me-1"></i>Riwayat Akademik
                        </a>
                    </li>
                </ul>
            </div>
            <div class="card-body">
                <div class="tab-content">

                    <!-- Tab Alamat -->
                    <div class="tab-pane fade show active" id="tabAlamat">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" width="35%">Alamat</td>
                                <td><?= esc($student['address'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">RT/RW</td>
                                <td><?= esc($student['rt'] ?? '-') ?>/<?= esc($student['rw'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Desa/Kelurahan</td>
                                <td><?= esc($student['village'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Kecamatan</td>
                                <td><?= esc($student['district'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Kota/Kabupaten</td>
                                <td><?= esc($student['city'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Provinsi</td>
                                <td><?= esc($student['province'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Kode Pos</td>
                                <td><?= esc($student['postal_code'] ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">No. HP</td>
                                <td><?= esc($student['phone'] ?? '-') ?></td>
                            </tr>
                        </table>
                    </div>

                    <!-- Tab Orang Tua -->
                    <div class="tab-pane fade" id="tabOrangtua">
                        <div class="card bg-light border-0 mb-4" style="border-radius: 8px;">
                            <div class="card-body p-3">
                                <h6 class="fw-semibold text-primary mb-3">
                                    <i class="bi bi-file-earmark-person me-1"></i>Data Kartu Keluarga (KK)
                                </h6>
                                <div class="row">
                                    <div class="col-md-6 mb-2">
                                        <span class="text-muted small">No. Kartu Keluarga (KK):</span>
                                        <strong class="d-block text-dark small"><?= esc($student['family_card_no'] ?? '-') ?></strong>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <span class="text-muted small">Nama Kepala Keluarga:</span>
                                        <strong class="d-block text-dark small"><?= esc($student['family_head_name'] ?? '-') ?></strong>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <h6 class="fw-semibold text-primary mb-3">
                                    <i class="bi bi-person me-1"></i>Ayah
                                </h6>
                                <table class="table table-sm table-borderless">
                                    <tr>
                                        <td class="text-muted" width="45%">Nama</td>
                                        <td><?= esc($student['father_name'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">NIK</td>
                                        <td><?= esc($student['father_nik'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Tempat Lahir</td>
                                        <td><?= esc($student['father_birth_place'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Tgl Lahir</td>
                                        <td><?= $student['father_birth'] ? date('d M Y', strtotime($student['father_birth'])) : '-' ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Pendidikan</td>
                                        <td><?= esc($student['father_edu'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Pekerjaan</td>
                                        <td><?= esc($student['father_job'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">No. HP</td>
                                        <td><?= esc($student['father_phone'] ?? '-') ?></td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <h6 class="fw-semibold mb-3" style="color:#e91e8c">
                                    <i class="bi bi-person me-1"></i>Ibu
                                </h6>
                                <table class="table table-sm table-borderless">
                                    <tr>
                                        <td class="text-muted" width="45%">Nama</td>
                                        <td><?= esc($student['mother_name'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">NIK</td>
                                        <td><?= esc($student['mother_nik'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Tempat Lahir</td>
                                        <td><?= esc($student['mother_birth_place'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Tgl Lahir</td>
                                        <td><?= $student['mother_birth'] ? date('d M Y', strtotime($student['mother_birth'])) : '-' ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Pendidikan</td>
                                        <td><?= esc($student['mother_edu'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Pekerjaan</td>
                                        <td><?= esc($student['mother_job'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">No. HP</td>
                                        <td><?= esc($student['mother_phone'] ?? '-') ?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- Tab Riwayat Akademik -->
                    <div class="tab-pane fade" id="tabRiwayat">
                        <?php if (empty($histories)): ?>
                        <div class="text-center text-muted py-4">
                            <i class="bi bi-inbox fs-3 d-block mb-2"></i>
                            Belum ada riwayat akademik
                        </div>
                        <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>Tahun Ajaran</th>
                                        <th>Semester</th>
                                        <th>Kelas</th>
                                        <th>Status</th>
                                        <th>Catatan</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($histories as $history): ?>
                                    <tr>
                                        <td><?= esc($history['year_name']) ?></td>
                                        <td>
                                            <span class="badge <?= $history['semester'] == 'Ganjil' ? 'bg-primary' : 'bg-info text-dark' ?>">
                                                <?= esc($history['semester']) ?>
                                            </span>
                                        </td>
                                        <td><?= esc($history['grade_name']) ?> - <?= esc($history['classroom_name']) ?></td>
                                        <td>
                                            <?php
                                            $sc = [
                                                'Aktif'        => 'success',
                                                'Naik Kelas'   => 'primary',
                                                'Tinggal Kelas'=> 'warning',
                                                'Lulus'        => 'info',
                                                'Mutasi'       => 'secondary',
                                                'Drop Out'     => 'danger',
                                            ];
                                            ?>
                                            <span class="badge bg-<?= $sc[$history['status']] ?? 'secondary' ?>">
                                                <?= esc($history['status']) ?>
                                            </span>
                                        </td>
                                        <td class="text-muted small"><?= esc($history['notes'] ?? '-') ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <?php endif; ?>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
function resetStudentPassword(studentId) {
    if (confirm("Apakah Anda yakin ingin mereset password akun login siswa ini? Password akan dikembalikan ke default (Tanggal Lahir format DDMMYYYY, cth: 15052012).")) {
        fetch('/student/reset-password/' + studentId, {
            method: 'POST',
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                '<?= csrf_header() ?>': '<?= csrf_hash() ?>'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert(data.message);
            } else {
                alert(data.message || 'Gagal mereset password.');
            }
        })
        .catch(err => {
            console.error(err);
            alert('Terjadi kesalahan koneksi.');
        });
    }
}
</script>

<?= $this->endSection() ?>