<?= view('admin/layouts/header', ['title' => 'Detail Barang']) ?>
<?php
$isAdministrator = ($userRole ?? '') === 'administrator';
$topbarAction = '<div class="d-flex flex-wrap gap-2 justify-content-end">'
    . '<a href="' . site_url('admin/barang') . '" class="btn btn-outline-dark"><i class="bi bi-arrow-left me-1"></i>Kembali</a>';

if ($isAdministrator) {
    $topbarAction .= '<a href="' . site_url('admin/barang/edit/' . $barang['id']) . '" class="btn btn-primary"><i class="bi bi-pencil-square me-1"></i>Edit Barang</a>';
}

$topbarAction .= '</div>';
?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'barang',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Detail Barang',
    'pageSubtitle' => $barang['kode_barang'] . ' - ' . $barang['nama_barang'],
    'topbarAction' => $topbarAction,
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-4">
        <div class="col-12 col-xl-4">
            <div class="panel p-4 h-100">
                <div class="text-center">
                    <?php if (! empty($barang['foto'])) : ?>
                        <img src="<?= base_url('uploads/barang/' . $barang['foto']) ?>" alt="Foto <?= esc($barang['nama_barang']) ?>" class="img-fluid rounded border mb-3" style="max-height: 260px; object-fit: contain;">
                    <?php else : ?>
                        <div class="d-grid place-items-center bg-light border rounded mb-3" style="height: 220px;">
                            <i class="bi bi-box-seam fs-1 text-secondary"></i>
                        </div>
                    <?php endif; ?>

                    <div class="h5 fw-bold mb-1"><?= esc($barang['nama_barang']) ?></div>
                    <div class="text-secondary"><?= esc($barang['kode_barang']) ?></div>
                </div>
            </div>
        </div>

        <div class="col-12 col-xl-8">
            <div class="panel p-4 h-100">
                <div class="row g-3">
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Kategori</div>
                        <div class="fw-semibold">
                            <i class="bi <?= esc($barang['icon_kategori'] ?? 'bi-tag', 'attr') ?> me-1"></i><?= esc($barang['nama_kategori'] ?? '-') ?>
                        </div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Satuan</div>
                        <div class="fw-semibold"><?= esc($barang['satuan']) ?></div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Lokasi Barang</div>
                        <div class="fw-semibold"><?= ! empty($barang['lokasi_barang']) ? esc($barang['lokasi_barang']) : '-' ?></div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Stok Minimum</div>
                        <div class="fw-semibold"><?= esc($barang['stok_minimum']) ?></div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Harga Beli</div>
                        <div class="fw-semibold">Rp <?= esc(number_format((float) ($barang['harga_beli'] ?? 0), 0, ',', '.')) ?></div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="small text-secondary">Harga Jual</div>
                        <div class="fw-semibold">Rp <?= esc(number_format((float) ($barang['harga_jual'] ?? 0), 0, ',', '.')) ?></div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="small text-secondary">Stok Awal</div>
                        <div class="fw-semibold"><?= esc($barang['stok_awal'] ?? 0) ?></div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="small text-secondary">Stok Saat Ini</div>
                        <div class="fw-semibold"><?= esc($barang['stok'] ?? 0) ?></div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="small text-secondary">Status Stok</div>
                        <?php if ((int) ($barang['stok'] ?? 0) <= (int) ($barang['stok_minimum'] ?? 0)) : ?>
                            <span class="badge text-bg-warning">Hampir habis</span>
                        <?php else : ?>
                            <span class="badge text-bg-success">Aman</span>
                        <?php endif; ?>
                    </div>
                    <div class="col-12">
                        <div class="small text-secondary">Deskripsi Barang</div>
                        <div><?= ! empty($barang['deskripsi_barang']) ? nl2br(esc($barang['deskripsi_barang'])) : '-' ?></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="panel p-4">
                <div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-3">
                    <div>
                        <div class="fw-bold">Riwayat Barang</div>
                        <div class="small text-secondary">Pergerakan stok dari stok awal, pembelian, penjualan, penyesuaian stok, sampai stok saat ini.</div>
                    </div>
                    <div class="badge text-bg-light border">
                        Stok saat ini: <?= esc((string) ($barang['stok'] ?? 0)) ?> <?= esc($barang['satuan']) ?>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle datatable">
                        <thead>
                            <tr>
                                <th style="width: 120px;">Tanggal</th>
                                <th style="width: 130px;">Jenis</th>
                                <th style="width: 160px;">Referensi</th>
                                <th>Keterangan</th>
                                <th class="text-end" style="width: 90px;">Masuk</th>
                                <th class="text-end" style="width: 90px;">Keluar</th>
                                <th class="text-end" style="width: 90px;">Saldo</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach (($stockHistory ?? []) as $row) : ?>
                                <?php
                                $badgeClass = match ($row['jenis']) {
                                    'masuk' => 'success',
                                    'keluar' => 'danger',
                                    'penyesuaian' => 'warning',
                                    'pembatalan' => 'dark',
                                    default => 'secondary',
                                };
                                ?>
                                <tr>
                                    <td>
                                        <?= ! empty($row['tanggal']) ? esc(date('d/m/Y', strtotime($row['tanggal']))) : '-' ?>
                                    </td>
                                    <td>
                                        <span class="badge text-bg-<?= esc($badgeClass, 'attr') ?>"><?= esc($row['label']) ?></span>
                                    </td>
                                    <td class="fw-semibold"><?= esc($row['referensi']) ?></td>
                                    <td><?= esc($row['keterangan']) ?></td>
                                    <td class="text-end"><?= $row['masuk'] !== null ? esc((string) $row['masuk']) : '<span class="text-secondary">-</span>' ?></td>
                                    <td class="text-end"><?= $row['keluar'] !== null ? esc((string) $row['keluar']) : '<span class="text-secondary">-</span>' ?></td>
                                    <td class="text-end fw-semibold"><?= esc((string) $row['saldo']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <?php
                $lastHistory = ! empty($stockHistory) ? end($stockHistory) : null;
                $lastSaldo = is_array($lastHistory) ? (int) $lastHistory['saldo'] : 0;
                ?>
                <?php if ($lastSaldo !== (int) ($barang['stok'] ?? 0)) : ?>
                    <div class="alert alert-warning mt-3 mb-0">
                        Saldo riwayat saat ini <?= esc((string) $lastSaldo) ?>, sedangkan stok master <?= esc((string) ($barang['stok'] ?? 0)) ?>.
                        Perbedaan ini biasanya terjadi karena koreksi stok langsung atau data lama sebelum riwayat transaksi lengkap.
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="col-12 col-lg-6">
            <div class="panel p-4 text-center h-100">
                <div class="fw-bold mb-3">QR Code</div>
                <img src="<?= esc($qrcode, 'attr') ?>" alt="QR Code <?= esc($barang['kode_barang'], 'attr') ?>" class="img-fluid mb-3" style="max-height: 260px;">
                <div>
                    <a href="<?= site_url('admin/barang/qrcode/' . $barang['id']) ?>" class="btn btn-outline-dark">
                        <i class="bi bi-download me-1"></i>Download QR Code
                    </a>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-6">
            <div class="panel p-4 text-center h-100">
                <div class="fw-bold mb-3">Barcode</div>
                <img src="<?= esc($barcode, 'attr') ?>" alt="Barcode <?= esc($barang['kode_barang'], 'attr') ?>" class="img-fluid mb-3" style="max-height: 220px;">
                <div>
                    <a href="<?= site_url('admin/barang/barcode/' . $barang['id']) ?>" class="btn btn-outline-dark">
                        <i class="bi bi-download me-1"></i>Download Barcode
                    </a>
                </div>
            </div>
        </div>
    </div>
</section>

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