<?php
$onHand = (float) $part['on_hand'];
$minStock = (float) $part['min_stock'];
if ($onHand == 0.0) { $sBadge = ['bg-danger', 'OUT OF STOCK']; }
elseif ($onHand < $minStock) { $sBadge = ['bg-warning text-dark', 'BELOW MIN']; }
else { $sBadge = ['bg-success', 'IN STOCK']; }
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-boxes-stacked me-2 text-primary"></i><?= e($part['name']) ?> <code class="small text-muted"><?= e($part['sku']) ?></code></h5>
    <div class="d-flex gap-2">
        <?php if ($canManage): ?><a class="btn btn-sm btn-outline-primary" href="<?= url('/Parts/edit/' . (int) $part['part_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a><?php endif; ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Parts') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="rs-card p-3 mb-3">
    <dl class="rs-def row g-2 mb-0">
        <div class="col-md-3"><dt>On hand</dt><dd><strong class="fs-5"><?= rtrim(rtrim(number_format($onHand, 3), '0'), '.') ?></strong> <span class="text-muted"><?= e($part['unit']) ?></span> <span class="badge <?= $sBadge[0] ?> ms-1"><?= $sBadge[1] ?></span></dd></div>
        <div class="col-md-3"><dt>Min / Max</dt><dd><?= number_format($minStock, 3) ?> / <?= $part['max_stock'] !== null ? number_format((float) $part['max_stock'], 3) : '—' ?></dd></div>
        <div class="col-md-3"><dt>Reorder qty</dt><dd><?= $part['reorder_qty'] !== null ? number_format((float) $part['reorder_qty'], 3) : '—' ?></dd></div>
        <div class="col-md-3"><dt>Unit cost</dt><dd><?= number_format((float) $part['unit_cost'], 2) ?></dd></div>
        <div class="col-md-3"><dt>Category</dt><dd><?= e($part['category'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Location</dt><dd><?= e($part['location'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Supplier</dt><dd><?= e($part['supplier'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Active</dt><dd><span class="badge <?= $part['is_active'] ? 'bg-success' : 'bg-secondary' ?>"><?= $part['is_active'] ? 'active' : 'inactive' ?></span></dd></div>
        <?php if (!empty($part['description'])): ?><div class="col-12"><dt>Description</dt><dd><?= nl2br(e($part['description'])) ?></dd></div><?php endif; ?>
    </dl>
</div>

<?php if ($canManage): ?>
<div class="row g-3 mb-3">
    <div class="col-md-4"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-success"><i class="fas fa-arrow-down me-1"></i>Receive stock</h6>
        <form method="post" action="<?= url('/Parts/receive/' . (int) $part['part_id']) ?>">
            <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
            <div class="mb-2"><label class="form-label">Quantity</label><input type="number" step="0.001" name="quantity" class="form-control form-control-sm" required></div>
            <div class="mb-2"><label class="form-label">Unit cost (this receipt)</label><input type="number" step="0.01" name="unit_cost" class="form-control form-control-sm"></div>
            <div class="mb-2"><label class="form-label">Reason / PO#</label><input type="text" name="reason" class="form-control form-control-sm" placeholder="PO-1234"></div>
            <button type="submit" class="btn btn-sm btn-success w-100"><i class="fas fa-check me-1"></i>Receive</button>
        </form>
    </div></div>
    <div class="col-md-4"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-warning"><i class="fas fa-arrow-up me-1"></i>Issue stock</h6>
        <form method="post" action="<?= url('/Parts/issue/' . (int) $part['part_id']) ?>">
            <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
            <div class="mb-2"><label class="form-label">Quantity</label><input type="number" step="0.001" name="quantity" class="form-control form-control-sm" required></div>
            <div class="mb-2"><label class="form-label">Work Order #</label><input type="number" name="work_order_id" class="form-control form-control-sm" placeholder="optional"></div>
            <div class="mb-2"><label class="form-label">Reason</label><input type="text" name="reason" class="form-control form-control-sm" placeholder="Van stock / consumed"></div>
            <button type="submit" class="btn btn-sm btn-warning text-dark w-100"><i class="fas fa-arrow-up me-1"></i>Issue</button>
        </form>
    </div></div>
    <div class="col-md-4"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-primary"><i class="fas fa-sliders me-1"></i>Adjust (stock-take)</h6>
        <form method="post" action="<?= url('/Parts/adjust/' . (int) $part['part_id']) ?>">
            <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
            <div class="mb-2"><label class="form-label">Delta (+/−)</label><input type="number" step="0.001" name="delta_qty" class="form-control form-control-sm" required placeholder="e.g. -2 or +5"></div>
            <div class="mb-2"><label class="form-label">Reason</label><input type="text" name="reason" class="form-control form-control-sm" placeholder="Cycle count / damage"></div>
            <button type="submit" class="btn btn-sm btn-primary w-100"><i class="fas fa-check me-1"></i>Adjust</button>
        </form>
    </div></div>
</div>
<?php endif; ?>

<div class="rs-card p-3">
    <h6 class="fw-bold mb-2"><i class="fas fa-clock-rotate-left me-1"></i>Stock Movement History</h6>
    <?php if (empty($movements)): ?>
        <p class="text-muted small mb-0">No movements yet.</p>
    <?php else: ?>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>When</th><th class="text-center">Direction</th><th class="text-end">Qty</th><th class="text-end">Unit cost</th><th>Reason</th><th>WO</th><th>By</th></tr></thead>
            <tbody>
                <?php foreach ($movements as $m):
                    $inn = $m['direction'] === 'in' ? 'text-success' : ($m['direction'] === 'out' ? 'text-danger' : 'text-primary');
                    $arrow = $m['direction'] === 'in' ? 'arrow-down' : ($m['direction'] === 'out' ? 'arrow-up' : 'sliders');
                ?>
                    <tr>
                        <td class="small text-muted"><?= e(substr((string) $m['created_at'], 0, 16)) ?></td>
                        <td class="text-center"><span class="<?= $inn ?>"><i class="fas fa-<?= $arrow ?> me-1"></i><?= e($m['direction']) ?></span></td>
                        <td class="text-end"><?= rtrim(rtrim(number_format((float) $m['quantity'], 3), '0'), '.') ?></td>
                        <td class="text-end small"><?= $m['unit_cost'] !== null ? number_format((float) $m['unit_cost'], 2) : '—' ?></td>
                        <td class="small"><?= e($m['reason'] ?? '—') ?></td>
                        <td class="small"><?php if (!empty($m['work_order_id'])): ?><a href="<?= url('/WorkOrders/show/' . (int) $m['work_order_id']) ?>">WO #<?= (int) $m['work_order_id'] ?></a><?php else: ?>—<?php endif; ?></td>
                        <td class="small text-muted"><?= e($m['username'] ?? '—') ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
