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

<div class="container-fluid px-4 py-3">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h5 class="fw-bold mb-0">Import Leger Nilai</h5>
            <small class="text-muted">Upload file leger dari aplikasi Rapor Kemenag</small>
        </div>
        <div class="d-flex gap-2">
            <a href="/leger-import/view" class="btn btn-outline-primary btn-sm">
                <i class="bi bi-table me-1"></i>Lihat Leger
            </a>
            <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modalUpload">
                <i class="bi bi-upload me-1"></i>Upload Leger
            </button>
        </div>
    </div>

    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <i class="bi bi-check-circle-fill me-2"></i><?= session()->getFlashdata('success') ?>
            <?php $warnings = session()->getFlashdata('import_warnings'); ?>
            <?php if ($warnings): ?>
                <div class="mt-2"><strong>Siswa tidak ditemukan di sistem:</strong>
                    <ul class="mb-0 mt-1"><?php foreach ($warnings as $w): ?><li><?= esc($w) ?></li><?php endforeach; ?></ul>
                </div>
            <?php endif; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>
    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show">
            <i class="bi bi-exclamation-circle-fill me-2"></i><?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <!-- Info cara import -->
    <div class="alert alert-info border-0 mb-4" style="background:#e8f4fd;">
        <div class="d-flex gap-3">
            <i class="bi bi-info-circle-fill text-info mt-1 fs-5"></i>
            <div>
                <strong>Cara Import Leger:</strong>
                <ol class="mb-0 mt-1 ps-3">
                    <li>Export leger dari aplikasi Rapor Kemenag (format .xlsx), satu file per kelas</li>
                    <li>Klik <strong>Upload Leger</strong>, pilih file</li>
                    <li>Preview tampil → pilih kelas & periode yang sesuai di SIMAK</li>
                    <li>Klik <strong>Konfirmasi Import</strong> untuk menyimpan</li>
                </ol>
                <div class="mt-2">
                    <strong>Penting:</strong> Pastikan kode mapel di <a href="/school-subject">Master Mapel</a>
                    sudah disesuaikan dengan kode di leger (QH, AA, FIK, SKI, BAR, PP, BINDO, MTK, dll).
                </div>
            </div>
        </div>
    </div>

    <!-- Riwayat Import -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white border-bottom py-3">
            <h6 class="mb-0 fw-semibold"><i class="bi bi-clock-history me-2 text-primary"></i>Riwayat Import</h6>
        </div>
        <div class="card-body p-0">
            <?php if (empty($imports)): ?>
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                    Belum ada leger yang diimport
                </div>
            <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Tahun Ajaran</th>
                            <th>Semester</th>
                            <th>Kelas</th>
                            <th>Info File</th>
                            <th class="text-center">Siswa</th>
                            <th class="text-center">Mapel</th>
                            <th>Diimport</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($imports as $i => $imp): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td><strong><?= esc($imp['year_name'] ?? $imp['year_raw'] ?? '-') ?></strong></td>
                            <td><?= esc($imp['semester'] ?? $imp['semester_raw'] ?? '-') ?></td>
                            <td>
                                <span class="fw-semibold"><?= esc($imp['classroom_name'] ?? '-') ?></span>
                                <small class="text-muted d-block"><?= esc($imp['grade_name'] ?? '') ?></small>
                            </td>
                            <td>
                                <small class="text-muted">
                                    <?= esc($imp['class_name_raw']) ?> |
                                    <?= esc($imp['semester_raw']) ?> |
                                    <?= esc($imp['year_raw']) ?>
                                </small>
                            </td>
                            <td class="text-center"><span class="badge bg-primary rounded-pill"><?= $imp['total_students'] ?></span></td>
                            <td class="text-center"><span class="badge bg-secondary rounded-pill"><?= $imp['total_subjects'] ?></span></td>
                            <td>
                                <small><?= esc($imp['imported_by_name'] ?? '-') ?></small>
                                <small class="text-muted d-block"><?= date('d/m/Y H:i', strtotime($imp['created_at'])) ?></small>
                            </td>
                            <td class="text-center">
                                <div class="d-flex gap-1 justify-content-center">
                                    <a href="/leger-import/view?year_id=<?= $imp['academic_period_id'] ? '' : '' ?>&period_id=<?= $imp['academic_period_id'] ?>&classroom_id=<?= $imp['classroom_id'] ?>"
                                       class="btn btn-sm btn-outline-primary" title="Lihat">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    <a href="/leger-import/print?classroom_id=<?= $imp['classroom_id'] ?>&period_id=<?= $imp['academic_period_id'] ?>"
                                       class="btn btn-sm btn-outline-secondary" title="Cetak" target="_blank">
                                        <i class="bi bi-printer"></i>
                                    </a>
                                    <button class="btn btn-sm btn-outline-danger"
                                            onclick="confirmDelete(<?= $imp['id'] ?>, '<?= esc($imp['classroom_name']) ?>', '<?= esc($imp['semester_raw']) ?> <?= esc($imp['year_raw']) ?>')"
                                            title="Hapus">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Modal Upload -->
<div class="modal fade" id="modalUpload" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="/leger-import/upload" method="post" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-upload me-2"></i>Upload File Leger</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label fw-semibold">File Leger Excel <span class="text-danger">*</span></label>
                        <input type="file" name="leger_file" class="form-control" accept=".xlsx,.xls" required>
                        <div class="form-text">Satu file = satu kelas. Format: hasil export leger dari aplikasi Rapor Kemenag.</div>
                    </div>
                    <div class="alert alert-warning py-2 mb-0">
                        <small><i class="bi bi-exclamation-triangle me-1"></i>
                        Jangan ubah format atau struktur file hasil export. Upload apa adanya.</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-search me-1"></i>Preview Data</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
function confirmDelete(id, kelas, periode) {
    Swal.fire({
        title: 'Hapus Leger?',
        html: `Semua nilai leger kelas <strong>${kelas}</strong> periode <strong>${periode}</strong> akan dihapus permanen.`,
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#dc3545',
        confirmButtonText: 'Ya, Hapus!',
        cancelButtonText: 'Batal'
    }).then(r => { if (r.isConfirmed) window.location.href = '/leger-import/delete/' + id; });
}
</script>
<?= $this->endSection() ?>