<?= view('admin/layouts/header', ['title' => 'Penyesuaian Stok']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'stok-penyesuaian',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Penyesuaian Stok',
    'pageSubtitle' => 'Koreksi stok berdasarkan hasil stock opname atau kondisi fisik barang.',
    'topbarAction' => '<a href="' . site_url('admin/stok-penyesuaian/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Penyesuaian</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
    <?php endif; ?>

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

    <div class="panel p-4">
        <div class="table-responsive">
            <table class="table table-hover align-middle datatable">
                <thead>
                    <tr>
                        <th>Tanggal</th>
                        <th>No. Penyesuaian</th>
                        <th>Barang</th>
                        <th class="text-end">Stok Sistem</th>
                        <th class="text-end">Stok Fisik</th>
                        <th class="text-end">Selisih</th>
                        <th>Alasan</th>
                        <th>Status</th>
                        <th class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($penyesuaian as $item) : ?>
                        <?php
                        $selisih = (int) $item['selisih'];
                        $selisihClass = $selisih > 0 ? 'text-success' : ($selisih < 0 ? 'text-danger' : 'text-secondary');
                        ?>
                        <tr>
                            <td><?= esc(date('d/m/Y', strtotime($item['tanggal_penyesuaian']))) ?></td>
                            <td>
                                <span class="fw-semibold d-block"><?= esc($item['no_penyesuaian']) ?></span>
                                <span><i class="bi bi-person small"></i> <?= esc($item['nama_user'] ?? '-') ?></span>
                            </td>
                            <td>
                                <span class="fw-semibold d-block"><?= esc($item['kode_barang']) ?></span>
                                <span><?= esc($item['nama_barang']) ?></span>
                            </td>
                            <td class="text-end"><?= esc((string) $item['stok_sistem']) ?></td>
                            <td class="text-end"><?= esc((string) $item['stok_fisik']) ?></td>
                            <td class="text-end fw-semibold <?= esc($selisihClass, 'attr') ?>"><?= esc((string) $selisih) ?></td>
                            <td><?= esc($item['alasan']) ?></td>
                            <td>
                                <?php if (($item['status'] ?? '') === 'aktif') : ?>
                                    <span class="badge text-bg-success">Aktif</span>
                                <?php else : ?>
                                    <span class="badge text-bg-secondary">Dibatalkan</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/stok-penyesuaian/detail/' . $item['id']) ?>" class="btn btn-sm btn-info btn-icon" title="Detail penyesuaian" aria-label="Detail penyesuaian">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <?php if (($item['status'] ?? '') === 'aktif') : ?>
                                    <form action="<?= site_url('admin/stok-penyesuaian/cancel/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Batalkan penyesuaian stok ini? Stok barang akan dikembalikan sesuai selisih penyesuaian.">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-warning btn-icon" title="Batalkan penyesuaian" aria-label="Batalkan penyesuaian">
                                            <i class="bi bi-arrow-counterclockwise"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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