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

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

        <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, isi data guru, lalu upload kembali.
                </p>
                <a href="/teacher/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">
                    <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>NIP</td><td><span class="text-muted">Tidak</span></td></tr>
                            <tr><td>B</td><td>Nama Lengkap</td><td><span class="badge bg-danger">Ya</span></td></tr>
                            <tr><td>C</td><td>JK (L/P)</td><td><span class="badge bg-danger">Ya</span></td></tr>
                            <tr><td>D</td><td>No. HP</td><td><span class="text-muted">Tidak</span></td></tr>
                            <tr><td>E</td><td>Email</td><td><span class="text-muted">Tidak</span></td></tr>
                            <tr><td>F</td><td>Kode Guru (aSc)</td><td><span class="text-muted">Tidak</span></td></tr>
                        </tbody>
                    </table>
                    <div class="small text-muted mt-2">
                        <i class="bi bi-info-circle me-1"></i>
                        Kode guru digunakan untuk sinkronisasi jadwal dari aSc Timetables.
                    </div>
                </div>
            </div>
        </div>

        <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; ?>

                <form action="/teacher/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</div>
                    </div>

                    <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()">
                                <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 -->
    <div class="col-md-7">

        <?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 (<?= count($importErrors) ?>)</span>
            </div>
            <div class="card-body p-0">
                <div class="list-group list-group-flush" style="max-height:200px;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; ?>

        <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">Catatan Penting</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">Data yang Diimport</div>
                            <div class="text-muted small">NIP, nama, jenis kelamin, no HP, email, dan kode guru (aSc). Akun login tidak dibuat otomatis — buat setelah import lewat halaman Daftar Guru.</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">Buat Akun Massal</div>
                            <div class="text-muted small">Setelah import, gunakan fitur <strong>Buat Akun Terpilih</strong> di halaman Daftar Guru. Password default = NIP masing-masing (atau guru123 jika NIP kosong).</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">3</div>
                        <div>
                            <div class="fw-semibold small text-warning">Format JK & Kode</div>
                            <div class="text-muted small">Isi kolom Jenis Kelamin dengan <code>L</code> atau <code>P</code>. Kode guru cukup angka 2 digit (01, 02, dst).</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-footer d-flex justify-content-end">
                <a href="/teacher" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-arrow-left me-1"></i>Kembali ke Daftar Guru
                </a>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
document.getElementById('excelFile').addEventListener('change', function() {
    const file = this.files[0];
    if (!file) { document.getElementById('filePreview').classList.add('d-none'); return; }
    document.getElementById('fileName').textContent = file.name;
    document.getElementById('fileSize').textContent = (file.size / 1024 / 1024).toFixed(2) + ' MB';
    document.getElementById('filePreview').classList.remove('d-none');
});

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

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() ?>