<?php $__env->startSection('title', 'Piece #' . $purchaseProduct->id); ?>

<?php $__env->startSection('content'); ?>

<div class="container-fluid">

    <div class="page-title-head d-flex align-items-center">
        <div class="flex-grow-1 d-flex align-items-center gap-3">
            <a onclick="history.back()" class="btn btn-default btn-icon btn-sm" title="Go back" style="cursor:pointer">
                <i class="ti ti-arrow-left fs-md"></i>
            </a>
            <div>
                <h4 class="page-main-title m-0">
                    Piece <code>#<?php echo e($purchaseProduct->id); ?></code>
                    <?php if($purchaseProduct->resolved_product): ?>
                        <span class="text-muted fw-normal fs-sm ms-1">— <?php echo e($purchaseProduct->resolved_product->title); ?></span>
                    <?php endif; ?>
                </h4>
                <small class="text-muted">Per-piece movement history</small>
            </div>
        </div>
        <div class="text-end">
            <ol class="breadcrumb m-0 py-0">
                <li class="breadcrumb-item"><a href="<?php echo e(url('/')); ?>">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="<?php echo e(route('stock.index')); ?>">Stock</a></li>
                <?php if($purchaseProduct->resolved_product): ?>
                    <li class="breadcrumb-item">
                        <a href="<?php echo e(route('stock.product', $purchaseProduct->resolved_product)); ?>">
                            <?php echo e(Str::limit($purchaseProduct->resolved_product->title, 20)); ?>

                        </a>
                    </li>
                <?php endif; ?>
                <li class="breadcrumb-item active">Piece #<?php echo e($purchaseProduct->id); ?></li>
            </ol>
        </div>
    </div>

    <div class="row">

        
        <div class="col-lg-3">

            
            <div class="card">
                <div class="card-header border-light">
                    <h5 class="card-title mb-0">
                        <i class="ti ti-gem me-2 text-primary"></i>Piece Details
                    </h5>
                </div>
                <div class="card-body">
                    <dl class="row mb-0 small">
                        <?php if($purchaseProduct->resolved_product): ?>
                            <dt class="col-5 text-muted">Product</dt>
                            <dd class="col-7">
                                <a href="<?php echo e(route('products.show', $purchaseProduct->resolved_product)); ?>" class="text-primary">
                                    <?php echo e($purchaseProduct->resolved_product->title); ?>

                                </a>
                            </dd>
                            <dt class="col-5 text-muted">SKU</dt>
                            <dd class="col-7"><code><?php echo e($purchaseProduct->resolved_product->sku); ?></code></dd>
                        <?php endif; ?>
                        <dt class="col-5 text-muted">Barcode</dt>
                        <dd class="col-7"><code><?php echo e($purchaseProduct->barcode ?? '—'); ?></code></dd>
                        <dt class="col-5 text-muted">Cost</dt>
                        <dd class="col-7"><?php echo e(number_format((float) $purchaseProduct->price, 2)); ?></dd>
                        <?php if($purchaseProduct->line && $purchaseProduct->line->purchase): ?>
                            <dt class="col-5 text-muted">Purchase</dt>
                            <dd class="col-7">
                                <a href="<?php echo e(route('purchases.show', $purchaseProduct->line->purchase)); ?>" class="d-inline-flex align-items-center gap-1 text-decoration-none">
                                    <span class="badge badge-soft-success d-inline-flex align-items-center gap-1">
                                        <i class="ti ti-shopping-cart fs-xs"></i>
                                        <?php echo e($purchaseProduct->line->purchase->invoice_number); ?>

                                    </span>
                                    <i class="ti ti-external-link fs-xxs text-muted"></i>
                                </a>
                            </dd>
                        <?php endif; ?>
                        <?php if($purchaseProduct->expiry_date): ?>
                            <dt class="col-5 text-muted">Expiry</dt>
                            <dd class="col-7"><?php echo e($purchaseProduct->expiry_date->format('d M Y')); ?></dd>
                        <?php endif; ?>
                    </dl>
                </div>
            </div>

            
            <div class="card">
                <div class="card-header border-light">
                    <h5 class="card-title mb-0">
                        <i class="ti ti-map-pin me-2 text-info"></i>Where It Is Now
                    </h5>
                </div>
                <div class="card-body p-0">
                    <?php if(empty($byLocation)): ?>
                        <p class="text-muted small mb-0 p-3">
                            <i class="ti ti-info-circle me-1"></i>No stock on hand for this piece.
                        </p>
                    <?php else: ?>
                        <ul class="list-group list-group-flush">
                            <?php $__currentLoopData = $byLocation; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $locId => $balance): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $loc = \App\Models\Location::find($locId); ?>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="fw-semibold small"><?php echo e($loc ? $loc->name : 'Location #'.$locId); ?></div>
                                        <?php if($loc): ?><small class="text-muted"><?php echo e($loc->location_code); ?></small><?php endif; ?>
                                    </div>
                                    <span class="badge <?php echo e($balance <= 0 ? 'badge-soft-danger' : 'badge-soft-success'); ?> fs-sm">
                                        <?php echo e((int) $balance); ?>

                                    </span>
                                </li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>

        </div>

        
        <div class="col-lg-9">
            <div class="card">
                <div class="card-header border-light">
                    <h5 class="card-title mb-0">Movement History</h5>
                </div>
                <div class="table-responsive">
                    <table class="table table-custom table-centered align-middle mb-0">
                        <thead class="bg-light bg-opacity-25 thead-sm">
                            <tr class="text-uppercase fs-xxs">
                                <th>Date</th>
                                <th>Direction</th>
                                <th>Reason</th>
                                <th style="min-width:180px">Source Document</th>
                                <th>Location</th>
                                <th class="text-end">Qty</th>
                                <th class="text-end">Balance</th>
                                <th>By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $rows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <?php
                                    $m       = $row['movement'];
                                    $srcType = $m->source_type;
                                    $srcId   = $m->source_id;

                                    $srcLabel = null;
                                    $srcUrl   = null;
                                    $srcIcon  = null;
                                    $srcBadge = 'secondary';
                                    if ($srcType && $srcId) {
                                        switch ($srcType) {
                                            case 'purchase':
                                                $srcLabel = $sourceLabels['purchase'][$srcId] ?? ('PUR #'.$srcId);
                                                $srcUrl   = route('purchases.show', $srcId);
                                                $srcIcon  = 'ti-shopping-cart';
                                                $srcBadge = 'success';
                                                break;
                                            case 'sale':
                                                $srcLabel = $sourceLabels['sale'][$srcId] ?? ('SALE #'.$srcId);
                                                $srcUrl   = route('sales.show', $srcId);
                                                $srcIcon  = 'ti-receipt';
                                                $srcBadge = 'danger';
                                                break;
                                            case 'stock_transfer':
                                                $srcLabel = $sourceLabels['stock_transfer'][$srcId] ?? ('TRF #'.$srcId);
                                                $srcUrl   = route('stock-transfers.show', $srcId);
                                                $srcIcon  = 'ti-transfer';
                                                $srcBadge = 'info';
                                                break;
                                        }
                                    }
                                ?>
                                <tr>
                                    <td>
                                        <div class="fw-semibold small"><?php echo e(optional($m->movement_date)->format('d M Y')); ?></div>
                                        <?php if($m->notes): ?>
                                            <small class="text-muted d-block" title="<?php echo e($m->notes); ?>">
                                                <?php echo e(Str::limit($m->notes, 35)); ?>

                                            </small>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if($m->isIn()): ?>
                                            <span class="badge badge-soft-success"><i class="ti ti-arrow-down-circle me-1"></i>IN</span>
                                        <?php else: ?>
                                            <span class="badge badge-soft-danger"><i class="ti ti-arrow-up-circle me-1"></i>OUT</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge <?php echo e($m->reasonBadgeClass()); ?> fs-xxs"><?php echo e($m->reasonLabel()); ?></span>
                                    </td>
                                    <td>
                                        <?php if($srcUrl): ?>
                                            <a href="<?php echo e($srcUrl); ?>" class="d-inline-flex align-items-center gap-1 text-decoration-none src-link">
                                                <span class="badge badge-soft-<?php echo e($srcBadge); ?> d-inline-flex align-items-center gap-1">
                                                    <i class="ti <?php echo e($srcIcon); ?> fs-xs"></i>
                                                    <?php echo e($srcLabel); ?>

                                                </span>
                                                <i class="ti ti-external-link fs-xxs text-muted"></i>
                                            </a>
                                        <?php else: ?>
                                            <span class="text-muted">—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo e(optional($m->location)->name ?? '—'); ?></td>
                                    <td class="text-end <?php echo e($m->isIn() ? 'text-success' : 'text-danger'); ?> fw-semibold">
                                        <?php echo e($m->isIn() ? '+' : '−'); ?><?php echo e((int) $m->qty); ?>

                                    </td>
                                    <td class="text-end fw-semibold"><?php echo e((int) $row['balance_after']); ?></td>
                                    <td><small class="text-muted"><?php echo e(optional($m->creator)->name ?? '—'); ?></small></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="8" class="text-center text-muted py-5">
                                        <i class="ti ti-inbox d-block fs-2xl mb-2"></i>
                                        No movements recorded for this piece.
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>

</div>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('styles'); ?>
<style>
.src-link { transition: opacity .15s; }
.src-link:hover { opacity: .8; }
</style>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layout.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH F:\VScode LIve ftp\paces\resources\views/stock/piece.blade.php ENDPATH**/ ?>