<?php
$filtersToShow = array_filter($data['filters'] ?? [], function ($value) {
    return $value !== '' && $value !== null;
});

$formatFilterLabel = function (string $key): string {
    $key = str_replace('_id', '', $key);
    return ucwords(str_replace('_', ' ', $key));
};
?>
<?php if (!empty($filtersToShow)): ?>
<div class="filters-info">
    <strong>Filters:</strong>
    <?php foreach ($filtersToShow as $key => $value): ?>
        <span style="margin-left:12px;"><?= e($formatFilterLabel($key)) ?>: <?= e((string)$value) ?></span>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card">
        <div class="label">Opening Stock Value</div>
        <div class="value"><?= money($data['totals']['opening_value']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Current Stock Value</div>
        <div class="value text-blue"><?= money($data['totals']['current_value']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Closing Stock Value</div>
        <div class="value text-green"><?= money($data['totals']['closing_value']) ?></div>
    </div>
</div>

<h2 style="margin: 10px 0 8px 0;">Opening Stock</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Qty</th>
            <th class="text-right">Unit Cost</th>
            <th class="text-right">Value</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['opening_stock'] as $item): ?>
        <tr>
            <td><?= e($item['name']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td class="text-center"><?= number_format($item['opening_quantity']) ?></td>
            <td class="text-right"><?= money($item['cost_price']) ?></td>
            <td class="text-right"><?= money($item['opening_value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="2" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['totals']['opening_quantity']) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['totals']['opening_value']) ?></td>
        </tr>
    </tbody>
</table>

<h2 style="margin: 18px 0 8px 0;">Current Stock</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Qty</th>
            <th class="text-right">Unit Cost</th>
            <th class="text-right">Value</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['current_stock'] as $item): ?>
        <tr>
            <td><?= e($item['name']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td class="text-center"><?= number_format($item['current_quantity']) ?></td>
            <td class="text-right"><?= money($item['cost_price']) ?></td>
            <td class="text-right"><?= money($item['current_value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="2" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['totals']['current_quantity']) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['totals']['current_value']) ?></td>
        </tr>
    </tbody>
</table>

<h2 style="margin: 18px 0 8px 0;">Closing Stock</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Qty</th>
            <th class="text-right">Unit Cost</th>
            <th class="text-right">Value</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['closing_stock'] as $item): ?>
        <tr>
            <td><?= e($item['name']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td class="text-center"><?= number_format($item['closing_quantity']) ?></td>
            <td class="text-right"><?= money($item['cost_price']) ?></td>
            <td class="text-right"><?= money($item['closing_value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="2" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['totals']['closing_quantity']) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['totals']['closing_value']) ?></td>
        </tr>
    </tbody>
</table>
