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

<?php $low = $item->quantity_in_stock <= $item->reorder_level; ?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0"><?= esc($item->name) ?></h1>
        <p class="text-muted mb-0">
            <?= esc($lookups->inventoryCategories[$item->category]['label'] ?? ucfirst($item->category)) ?> ·
            <span class="badge bg-<?= $low ? 'danger' : 'success' ?>"><?= $low ? 'Low Stock' : 'In Stock' ?></span>
        </p>
    </div>
    <a href="<?= site_url('inventory/' . $item->id . '/edit') ?>" class="btn btn-outline-secondary"><i class="bi bi-pencil me-1"></i>Edit</a>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <h2 class="h6">Item Details</h2>
                <div class="row row-cols-2 g-3 small">
                    <div class="col"><span class="text-muted">Quantity in Stock</span><br><span class="fw-semibold fs-5"><?= (int) $item->quantity_in_stock ?> <?= esc($item->unit) ?></span></div>
                    <div class="col"><span class="text-muted">Reorder Level</span><br><span class="fw-semibold"><?= (int) $item->reorder_level ?> <?= esc($item->unit) ?></span></div>
                    <div class="col"><span class="text-muted">Unit Cost</span><br><span class="fw-semibold"><?= format_money($item->unit_cost) ?> / <?= esc($item->unit) ?></span></div>
                    <div class="col"><span class="text-muted">Total Stock Value</span><br><span class="fw-semibold"><?= format_money($stockValue) ?></span></div>
                    <div class="col"><span class="text-muted">Last Updated</span><br><span class="fw-semibold"><?= esc(date('d M Y, g:i a', strtotime($item->updated_at))) ?></span></div>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mt-3">
            <div class="card-header">Stock Movement History</div>
            <?php if (empty($transactions)): ?>
                <div class="card-body text-muted small">No movements logged yet.</div>
            <?php else: ?>
            <div class="table-scroll">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light"><tr><th>When</th><th>Type</th><th class="text-end">Qty</th><th>Reference</th><th>By</th></tr></thead>
                    <tbody>
                        <?php foreach ($transactions as $tx): ?>
                            <tr>
                                <td class="small"><?= esc(date('d M Y, g:i a', strtotime($tx->created_at))) ?></td>
                                <td><span class="badge bg-<?= ['received' => 'success', 'consumed' => 'warning text-dark', 'adjusted' => 'secondary'][$tx->type] ?? 'secondary' ?>"><?= esc(ucfirst($tx->type)) ?></span></td>
                                <td class="text-end fw-semibold <?= $tx->quantity >= 0 ? 'text-success' : 'text-danger' ?>"><?= $tx->quantity >= 0 ? '+' : '' ?><?= (int) $tx->quantity ?></td>
                                <td class="small text-muted"><?= esc($tx->reference) ?></td>
                                <td class="small"><?= esc($tx->created_by_name ?? '—') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm mb-3">
            <div class="card-header">Stock Level</div>
            <div class="card-body">
                <?php
                    // Reorder level as the "full" reference point (matches
                    // the Low Stock alert logic on the index page) — there's
                    // no separate "max stock" concept in this schema.
                    $ceiling = max($item->reorder_level * 2, $item->quantity_in_stock, 1);
                    $pct     = min(100, round($item->quantity_in_stock / $ceiling * 100));
                ?>
                <div class="progress mb-2" style="height: 8px;">
                    <div class="progress-bar bg-<?= $low ? 'danger' : 'success' ?>" style="width: <?= $pct ?>%;"></div>
                </div>
                <p class="text-muted small mb-3"><?= (int) $item->quantity_in_stock ?> <?= esc($item->unit) ?> on hand · reorder at <?= (int) $item->reorder_level ?></p>
                <a href="<?= site_url('inventory') ?>" class="btn btn-outline-secondary btn-sm w-100">Back to Inventory</a>
            </div>
        </div>

        <div class="card shadow-sm">
            <div class="card-header">Log Stock Movement</div>
            <div class="card-body">
                <ul class="nav nav-tabs nav-tabs-sm mb-3" id="stockTabs" style="font-size: 0.8rem;">
                    <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-receive">Receive</a></li>
                    <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-consume">Consume</a></li>
                    <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-adjust">Adjust</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane fade show active" id="tab-receive">
                        <form action="<?= site_url('inventory/' . $item->id . '/receive') ?>" method="post">
                            <?= csrf_field() ?>
                            <label class="form-label small">Quantity received</label>
                            <input type="number" min="1" class="form-control form-control-sm mb-2" name="quantity" required>
                            <label class="form-label small">Reference <span class="text-muted fw-normal">(supplier/invoice #)</span></label>
                            <input type="text" class="form-control form-control-sm mb-2" name="reference">
                            <button type="submit" class="btn btn-sm btn-success w-100">Receive Stock</button>
                        </form>
                    </div>
                    <div class="tab-pane fade" id="tab-consume">
                        <form action="<?= site_url('inventory/' . $item->id . '/consume') ?>" method="post">
                            <?= csrf_field() ?>
                            <label class="form-label small">Quantity consumed</label>
                            <input type="number" min="1" max="<?= (int) $item->quantity_in_stock ?>" class="form-control form-control-sm mb-2" name="quantity" required>
                            <label class="form-label small">Reference <span class="text-muted fw-normal">(department/use)</span></label>
                            <input type="text" class="form-control form-control-sm mb-2" name="reference">
                            <button type="submit" class="btn btn-sm btn-warning w-100">Log Consumption</button>
                        </form>
                    </div>
                    <div class="tab-pane fade" id="tab-adjust">
                        <form action="<?= site_url('inventory/' . $item->id . '/adjust') ?>" method="post">
                            <?= csrf_field() ?>
                            <label class="form-label small">New quantity on hand</label>
                            <input type="number" min="0" class="form-control form-control-sm mb-2" name="new_quantity" value="<?= (int) $item->quantity_in_stock ?>" required>
                            <label class="form-label small">Reason</label>
                            <input type="text" class="form-control form-control-sm mb-2" name="reference" placeholder="e.g. stock count correction">
                            <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Adjust Stock</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
