<?= view('admin/layouts/header', ['title' => 'Laporan Stok Barang']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'laporan-stok',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?php
$query = array_filter([
    'kategori_id' => $filters['kategori_id'] ?? '',
    'status'      => $filters['status'] ?? '',
], static fn ($value) => $value !== '');
$pdfUrl = site_url('admin/laporan/stok/pdf') . ($query ? '?' . http_build_query($query) : '');
$excelUrl = site_url('admin/laporan/stok/excel') . ($query ? '?' . http_build_query($query) : '');
?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Laporan Stok Barang',
    'pageSubtitle' => 'Pantau stok saat ini, stok minimum, dan status ketersediaan barang.',
    'topbarAction' => '<a href="' . $excelUrl . '" class="btn btn-success"><i class="bi bi-file-earmark-excel me-1"></i>Export Excel</a>'
        . '<a href="' . $pdfUrl . '" target="_blank" class="btn btn-dark"><i class="bi bi-file-earmark-pdf me-1"></i>Export PDF</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-3 mb-4">
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Barang</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_barang']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Stok</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_stok']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Hampir Habis</div>
                <div class="h4 fw-bold mb-0 text-warning"><?= esc((string) $summary['hampir_habis']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Stok Kosong</div>
                <div class="h4 fw-bold mb-0 text-danger"><?= esc((string) $summary['kosong']) ?></div>
            </div>
        </div>
    </div>

    <div class="panel p-4 mb-4">
        <form action="<?= site_url('admin/laporan/stok') ?>" method="get" class="row g-3 align-items-end">
            <div class="col-12 col-md-5">
                <label for="kategori_id" class="form-label fw-semibold">Kategori</label>
                <select class="form-select select2" id="kategori_id" name="kategori_id" data-placeholder="Semua kategori">
                    <option value="">Semua kategori</option>
                    <?php foreach ($kategori as $item) : ?>
                        <option value="<?= esc($item['id'], 'attr') ?>" <?= ($filters['kategori_id'] ?? '') === (string) $item['id'] ? 'selected' : '' ?>>
                            <?= esc($item['nama_kategori']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-12 col-md-4">
                <label for="status" class="form-label fw-semibold">Status Stok</label>
                <select class="form-select" id="status" name="status">
                    <option value="" <?= ($filters['status'] ?? '') === '' ? 'selected' : '' ?>>Semua status</option>
                    <option value="aman" <?= ($filters['status'] ?? '') === 'aman' ? 'selected' : '' ?>>Aman</option>
                    <option value="hampir-habis" <?= ($filters['status'] ?? '') === 'hampir-habis' ? 'selected' : '' ?>>Hampir habis</option>
                    <option value="kosong" <?= ($filters['status'] ?? '') === 'kosong' ? 'selected' : '' ?>>Kosong</option>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-dark flex-fill">
                        <i class="bi bi-funnel me-1"></i>Filter
                    </button>
                    <a href="<?= site_url('admin/laporan/stok') ?>" class="btn btn-light">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <div class="panel p-4">
        <div class="table-responsive">
            <table class="table table-hover align-middle datatable">
                <thead>
                    <tr>
                        <th>Kode</th>
                        <th>Nama Barang</th>
                        <th>Kategori</th>
                        <th>Lokasi</th>                                             
                        <th class="text-end">Stok Saat Ini</th>                        
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($barang as $item) : ?>
                        <?php
                        $stok = (int) ($item['stok'] ?? 0);
                        $stokMinimum = (int) ($item['stok_minimum'] ?? 0);
                        $statusLabel = $stok <= 0 ? 'Kosong' : ($stok <= $stokMinimum ? 'Hampir habis' : 'Aman');
                        $statusClass = $stok <= 0 ? 'danger' : ($stok <= $stokMinimum ? 'warning' : 'success');
                        ?>
                        <tr>
                            <td class="fw-semibold"><?= esc($item['kode_barang']) ?></td>
                            <td><?= esc($item['nama_barang']) ?></td>
                            <td><?= esc($item['nama_kategori'] ?? '-') ?></td>
                            <td><?= ! empty($item['lokasi_barang']) ? esc($item['lokasi_barang']) : '<span class="text-secondary">-</span>' ?></td>                            
                            <td class="text-end fw-semibold"><?= esc((string) $stok). ' ' . esc($item['satuan']) ?> </td>                            
                            <td><span class="badge text-bg-<?= esc($statusClass, 'attr') ?>"><?= esc($statusLabel) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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