<?= view('admin/layouts/header', ['title' => 'Import Barang']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'barang',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Import Barang',
    'pageSubtitle' => 'Upload data barang dari file Excel XLSX.',
    'topbarAction' => '<div class="d-flex flex-wrap gap-2 justify-content-end">'
        . '<a href="' . site_url('admin/barang/import/template') . '" class="btn btn-success"><i class="bi bi-file-earmark-excel me-1"></i>Download Format XLSX</a>'
        . '<a href="' . site_url('admin/barang') . '" class="btn btn-outline-dark"><i class="bi bi-arrow-left me-1"></i>Kembali</a>'
        . '</div>',
]) ?>

<section class="content-area">
    <?php $errors = session()->getFlashdata('errors'); ?>
    <?php $importErrors = session()->getFlashdata('import_errors'); ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
    <?php endif; ?>

    <?php if ($errors) : ?>
        <div class="alert alert-danger">
            <?php foreach ($errors as $error) : ?>
                <div><?= esc($error) ?></div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <?php if ($importErrors) : ?>
        <div class="alert alert-danger">
            <div class="fw-bold mb-2">Import gagal. Perbaiki data berikut:</div>
            <div style="max-height: 260px; overflow-y: auto;">
                <?php foreach ($importErrors as $error) : ?>
                    <div><?= esc($error) ?></div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <div class="col-12 col-xl-7">
            <div class="panel p-4">
                <form action="<?= site_url('admin/barang/import/store') ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label for="file_import" class="form-label fw-semibold">File Excel XLSX</label>
                        <input type="file" class="form-control" id="file_import" name="file_import" accept=".xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" required>
                        <div class="form-text">Gunakan format XLSX dari tombol Download Format XLSX. Maksimal 5 MB.</div>
                    </div>

                    <div class="d-flex justify-content-end gap-2">
                        <a href="<?= site_url('admin/barang') ?>" class="btn btn-light">Batal</a>
                        <button type="submit" class="btn btn-dark">
                            <i class="bi bi-upload me-1"></i>Import Data
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <div class="col-12 col-xl-5">
            <div class="panel p-4">
                <div class="fw-bold mb-3">Ketentuan Format</div>
                <ul class="mb-3">
                    <li>Baris ke-2 pada sheet <strong>Barang</strong> berisi contoh. Ubah datanya atau hapus jika tidak dipakai.</li>
                    <li>Isi data berikutnya mulai baris ke-3.</li>
                    <li>Kode barang wajib unik dan belum pernah digunakan.</li>
                    <li>Barcode boleh dikosongkan. Jika kosong, barcode otomatis memakai kode barang.</li>
                    <li>Nama kategori harus sama dengan data kategori di aplikasi.</li>
                    <li>Stok awal, stok minimum, harga beli, dan harga jual wajib angka 0 atau lebih.</li>
                    <li>Harga jual tidak boleh lebih kecil dari harga beli.</li>
                    <li>Foto barang tidak diimport dari Excel.</li>
                </ul>

                <div class="fw-bold mb-2">Kolom XLSX</div>
                <div class="table-responsive">
                    <table class="table table-sm table-bordered mb-0">
                        <tbody>
                            <?php foreach (\App\Libraries\BarangImportXlsx::HEADERS as $label) : ?>
                                <tr><td><?= esc($label) ?></td></tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
