<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/products" class="btn btn-ghost btn-sm mb-4" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Products
        </a>
        <div class="flex items-center gap-3">
            <h1><?= e($product['name']) ?></h1>
            <?php if (!$product['is_active']): ?>
                <span class="badge badge-muted">Inactive</span>
            <?php endif; ?>
        </div>
        <div class="page-subtitle">SKU: <?= e($product['sku']) ?> &bull; Barcode: <?= e($product['barcode'] ?: 'N/A') ?></div>
    </div>
    <div class="flex gap-2">
        <a href="<?= APP_URL ?>/products/<?= $product['id'] ?>/edit" class="btn btn-outline">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>
            Edit Product
        </a>
        <a href="<?= APP_URL ?>/inventory/adjust?product_id=<?= $product['id'] ?>" class="btn btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>
            Adjust Stock
        </a>
    </div>
</div>

<div class="grid-3 mb-6">
    <div class="card" style="grid-column: span 1;">
        <?php if ($product['image']): ?>
            <div style="width:100%; aspect-ratio:1; border-radius:var(--radius); overflow:hidden; margin-bottom:20px; background:white;">
                <img src="<?= APP_URL ?>/uploads/<?= Auth::tenantId() ?>/<?= e($product['image']) ?>" style="width:100%; height:100%; object-fit:contain;">
            </div>
        <?php else: ?>
            <div style="width:100%; aspect-ratio:1; border-radius:var(--radius); background:var(--bg-raised); display:flex; align-items:center; justify-content:center; margin-bottom:20px;">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:48px;height:48px;color:var(--text-muted)"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>
            </div>
        <?php endif; ?>

        <div class="mb-4">
            <div class="text-xs text-muted font-semibold uppercase tracking-wide mb-1">Pricing</div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Cost Price</span>
                <strong><?= money($product['cost_price']) ?></strong>
            </div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Selling Price</span>
                <strong class="text-primary-color"><?= money($product['selling_price']) ?></strong>
            </div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Wholesale Price</span>
                <strong><?= money($product['wholesale_price']) ?> <span class="text-xs text-muted">/ <?= e($product['wholesale_qty']) ?> <?= e($product['unit']) ?></span></strong>
            </div>
        </div>

        <div>
            <div class="text-xs text-muted font-semibold uppercase tracking-wide mb-1">Details</div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Category</span>
                <span><?= e($product['category_name'] ?: 'None') ?></span>
            </div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Brand</span>
                <span><?= e($product['brand_name'] ?: 'None') ?></span>
            </div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Base Unit</span>
                <span><?= e($product['unit']) ?></span>
            </div>
            <div class="flex justify-between items-center py-2 border-b border-soft">
                <span>Tracking</span>
                <span>
                    <?php if($product['batch_tracking']) echo '<span class="badge badge-info mb-1">Batch</span>'; ?>
                    <?php if($product['serial_tracking']) echo '<span class="badge badge-info">Serial</span>'; ?>
                    <?php if(!$product['batch_tracking'] && !$product['serial_tracking']) echo 'Basic'; ?>
                </span>
            </div>
        </div>
    </div>

    <div class="card" style="grid-column: span 2; display:flex; flex-direction:column;">
        <div class="card-header">
            <h3 class="card-title">Stock by Location</h3>
        </div>
        
        <?php 
        $locations = db()->prepare("SELECT * FROM locations WHERE tenant_id=?");
        $locations->execute([Auth::tenantId()]);
        $locations = $locations->fetchAll();
        
        $totalStock = 0;
        ?>

        <div class="table-wrapper mb-6">
            <table>
                <thead>
                    <tr>
                        <th>Location</th>
                        <th>Type</th>
                        <th class="text-right">Current Stock</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($locations as $loc): 
                        // Find stock for this location
                        $locStock = 0;
                        foreach ($product['stock'] as $s) {
                            if ($s['location_id'] == $loc['id']) {
                                $locStock += $s['quantity'];
                            }
                        }
                        $totalStock += $locStock;
                    ?>
                    <tr>
                        <td><strong><?= e($loc['name']) ?></strong> <?php if ($loc['is_default'] ?? false) echo '<span class="badge badge-primary ml-2">Default</span>'; ?></td>
                        <td><?= ucfirst($loc['type']) ?></td>
                        <td class="text-right">
                            <span style="font-weight:700; font-size:1.1rem; <?= $locStock <= $product['alert_qty'] ? 'color:var(--danger)' : 'color:var(--success)' ?>">
                                <?= $locStock ?>
                            </span>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr style="background:var(--bg-raised)">
                        <td colspan="2"><strong>Total Enterprise Stock</strong></td>
                        <td class="text-right"><strong style="font-size:1.2rem;"><?= $totalStock ?> <?= e($product['unit']) ?></strong></td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <?php if ($product['description']): ?>
            <div class="mt-auto">
                <h4 class="text-sm font-semibold mb-2">Description</h4>
                <div style="font-size:0.9rem; color:var(--text-secondary); background:var(--bg-raised); padding:16px; border-radius:var(--radius-sm);">
                    <?= nl2br(e($product['description'])) ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>
