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

<div class="row">
    <!-- Panel Kiri: Form Import -->
    <div class="col-md-5">

        <!-- Download Template -->
        <div class="card mb-3 border-primary border-opacity-25">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-file-earmark-arrow-down text-primary"></i>
                <span class="fw-semibold">Langkah 1: Download Template</span>
            </div>
            <div class="card-body">
                <p class="text-muted small mb-3">
                    Download template Excel terlebih dahulu, isi data siswa sesuai format, lalu upload kembali.
                </p>
                <a href="/student/download-template" class="btn btn-outline-primary w-100">
                    <i class="bi bi-download me-2"></i>Download Template Excel
                </a>
                <div class="mt-3">
                    <div class="small text-muted fw-semibold mb-1">Kolom yang tersedia:</div>
                    <div class="table-responsive">
                        <table class="table table-sm table-bordered mb-0" style="font-size:.78rem">
                            <thead class="table-light">
                                <tr>
                                    <th>Kolom</th>
                                    <th>Keterangan</th>
                                    <th>Wajib</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr><td>A</td><td>NIS</td><td><span class="badge bg-danger">Ya</span></td></tr>
                                <tr><td>B</td><td>NISN</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>C</td><td>NIK</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>D</td><td>No. KIP</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>E</td><td>Nama Lengkap</td><td><span class="badge bg-danger">Ya</span></td></tr>
                                <tr><td>F</td><td>JK (L/P)</td><td><span class="badge bg-danger">Ya</span></td></tr>
                                <tr><td>G</td><td>Agama</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>H</td><td>Tempat Lahir</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>I</td><td>Tanggal Lahir (YYYY-MM-DD)</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>J</td><td>No. HP Siswa</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>K</td><td>Tahun Masuk</td><td><span class="badge bg-danger">Ya</span></td></tr>
                                <tr><td>L</td><td>Tanggal Masuk</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>M</td><td>Asal Sekolah</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>N</td><td>No. Seri Ijazah</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>O</td><td>Status (Aktif/Alumni/...)</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>P - W</td><td>Data Alamat Lengkap</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>X</td><td>No. KK</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>Y</td><td>Nama Kepala Keluarga</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>Z - AF</td><td>Data Ayah</td><td><span class="text-muted">Tidak</span></td></tr>
                                <tr><td>AG - AM</td><td>Data Ibu</td><td><span class="text-muted">Tidak</span></td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Form Upload -->
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-upload text-primary"></i>
                <span class="fw-semibold">Langkah 2: Upload File Excel</span>
            </div>
            <div class="card-body">

                <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger py-2 small">
                    <i class="bi bi-exclamation-circle me-1"></i>
                    <?= session()->getFlashdata('error') ?>
                </div>
                <?php endif; ?>

                <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success py-2 small">
                    <i class="bi bi-check-circle me-1"></i>
                    <?= session()->getFlashdata('success') ?>
                </div>
                <?php endif; ?>

                <?php if (!$activePeriod): ?>
                <div class="alert alert-warning py-2 small mb-3">
                    <i class="bi bi-exclamation-triangle me-1"></i>
                    <strong>Perhatian:</strong> Belum ada periode aktif. Siswa yang diimport tidak akan otomatis masuk ke kelas.
                </div>
                <?php endif; ?>

                <form action="/student/import-process" method="post" enctype="multipart/form-data" id="importForm">
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label class="form-label">File Excel <span class="text-danger">*</span></label>
                        <input type="file" name="excel_file" id="excelFile"
                            class="form-control" accept=".xlsx,.xls" required>
                        <div class="form-text">Format: .xlsx atau .xls | Maks. 5MB</div>
                    </div>

                    <?php if ($activePeriod): ?>
                    <div class="mb-3">
                        <label class="form-label">Masukkan ke Kelas</label>
                        <select name="classroom_id" class="form-select">
                            <option value="">-- Tidak Assign ke Kelas --</option>
                            <?php foreach ($classrooms as $classroom): ?>
                            <option value="<?= $classroom['id'] ?>">
                                <?= esc($classroom['grade_name']) ?> -
                                <?= esc($classroom['name']) ?>
                                <?php if ($classroom['major_name']): ?>
                                (<?= esc($classroom['major_name']) ?>)
                                <?php endif; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <div class="form-text">
                            Periode aktif: <strong><?= esc($activePeriod['year_name']) ?> — <?= esc($activePeriod['semester']) ?></strong>
                        </div>
                    </div>
                    <?php endif; ?>

                    <!-- Preview file yang dipilih -->
                    <div id="filePreview" class="d-none mb-3">
                        <div class="p-2 bg-light rounded border d-flex align-items-center gap-2">
                            <i class="bi bi-file-earmark-excel text-success fs-5"></i>
                            <div>
                                <div class="small fw-semibold" id="fileName"></div>
                                <div class="text-muted" style="font-size:.75rem" id="fileSize"></div>
                            </div>
                            <button type="button" class="btn btn-sm btn-link text-danger ms-auto p-0"
                                onclick="clearFile()" title="Hapus file">
                                <i class="bi bi-x-circle"></i>
                            </button>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary w-100" id="btnImport">
                        <i class="bi bi-cloud-upload me-2"></i>Import Sekarang
                    </button>
                </form>
            </div>
        </div>

    </div>

    <!-- Panel Kanan: Hasil / Panduan -->
    <div class="col-md-7">

        <!-- Error Import (jika ada) -->
        <?php $importErrors = session()->getFlashdata('import_errors'); ?>
        <?php if ($importErrors): ?>
        <div class="card mb-3 border-warning">
            <div class="card-header d-flex align-items-center gap-2 bg-warning bg-opacity-10">
                <i class="bi bi-exclamation-triangle text-warning"></i>
                <span class="fw-semibold">Baris yang Gagal Diimport (<?= count($importErrors) ?> baris)</span>
            </div>
            <div class="card-body p-0">
                <div class="list-group list-group-flush" style="max-height:280px;overflow-y:auto">
                    <?php foreach ($importErrors as $err): ?>
                    <div class="list-group-item list-group-item-warning py-2 small">
                        <i class="bi bi-x-circle me-1"></i><?= esc($err) ?>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Panduan -->
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-question-circle text-primary"></i>
                <span class="fw-semibold">Panduan Import</span>
            </div>
            <div class="card-body">

                <div class="d-flex flex-column gap-3">

                    <div class="d-flex gap-3 align-items-start">
                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center flex-shrink-0"
                            style="width:32px;height:32px;font-size:.85rem;font-weight:700">1</div>
                        <div>
                            <div class="fw-semibold small">Download Template</div>
                            <div class="text-muted small">Klik tombol "Download Template Excel" untuk mendapatkan file template dengan format yang benar.</div>
                        </div>
                    </div>

                    <div class="d-flex gap-3 align-items-start">
                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center flex-shrink-0"
                            style="width:32px;height:32px;font-size:.85rem;font-weight:700">2</div>
                        <div>
                            <div class="fw-semibold small">Isi Data di Excel</div>
                            <div class="text-muted small">Isi data siswa mulai dari baris ke-2 (baris 1 adalah header). Jangan mengubah urutan atau nama kolom.</div>
                        </div>
                    </div>

                    <div class="d-flex gap-3 align-items-start">
                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center flex-shrink-0"
                            style="width:32px;height:32px;font-size:.85rem;font-weight:700">3</div>
                        <div>
                            <div class="fw-semibold small">Perhatikan Format Data</div>
                            <div class="text-muted small">
                                <ul class="ps-3 mb-0 mt-1">
                                    <li>NIS harus unik (belum terdaftar)</li>
                                    <li>Jenis Kelamin: isi <code>L</code> atau <code>P</code></li>
                                    <li>Tanggal Lahir: format <code>YYYY-MM-DD</code> (mis. 2012-05-15)</li>
                                    <li>Agama: Islam, Kristen, Katolik, Hindu, Buddha, atau Konghucu</li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-3 align-items-start">
                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center flex-shrink-0"
                            style="width:32px;height:32px;font-size:.85rem;font-weight:700">4</div>
                        <div>
                            <div class="fw-semibold small">Upload & Pilih Kelas</div>
                            <div class="text-muted small">Pilih file Excel yang sudah diisi, pilih kelas tujuan (opsional), lalu klik "Import Sekarang".</div>
                        </div>
                    </div>

                    <div class="d-flex gap-3 align-items-start">
                        <div class="rounded-circle bg-warning text-white d-flex align-items-center justify-content-center flex-shrink-0"
                            style="width:32px;height:32px;font-size:.85rem;font-weight:700">!</div>
                        <div>
                            <div class="fw-semibold small text-warning">Catatan Penting</div>
                            <div class="text-muted small">Baris yang memiliki NIS duplikat atau data tidak valid akan dilewati dan ditampilkan di daftar error. Baris lain yang valid tetap akan diimport.</div>
                        </div>
                    </div>

                </div>

            </div>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <small class="text-muted">
                    <i class="bi bi-people me-1"></i>
                    Tidak ada batasan jumlah baris per file
                </small>
                <a href="/student" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-arrow-left me-1"></i>Kembali ke Daftar Siswa
                </a>
            </div>
        </div>

    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
// Preview file yang dipilih
document.getElementById('excelFile').addEventListener('change', function() {
    const file = this.files[0];
    if (!file) {
        document.getElementById('filePreview').classList.add('d-none');
        return;
    }

    const sizeMB = (file.size / 1024 / 1024).toFixed(2);
    document.getElementById('fileName').textContent = file.name;
    document.getElementById('fileSize').textContent = sizeMB + ' MB';
    document.getElementById('filePreview').classList.remove('d-none');
});

function clearFile() {
    document.getElementById('excelFile').value = '';
    document.getElementById('filePreview').classList.add('d-none');
}

// Loading state saat submit
document.getElementById('importForm').addEventListener('submit', function() {
    const btn = document.getElementById('btnImport');
    btn.disabled = true;
    btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Memproses...';
});
</script>
<?= $this->endSection() ?>