<?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">Total Sales</div>
        <div class="value"><?= number_format($data['summary']['total_sales']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Total Revenue</div>
        <div class="value text-green"><?= money($data['summary']['total_revenue']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Items Sold</div>
        <div class="value"><?= number_format($data['summary']['total_items_sold']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Average Sale</div>
        <div class="value"><?= money($data['summary']['average_sale_value']) ?></div>
    </div>
</div>

<h2 style="margin: 10px 0 8px 0;">Sales Details</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Sale No</th>
            <th>Date</th>
            <th>Customer</th>
            <th>Location</th>
            <th>User</th>
            <th class="text-center">Items</th>
            <th class="text-right">Discount</th>
            <th class="text-right">Total</th>
            <th>Payment</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['sales'] as $sale): ?>
        <tr>
            <td><?= e($sale['sale_no']) ?></td>
            <td><?= date('Y-m-d H:i', strtotime($sale['created_at'])) ?></td>
            <td><?= e($sale['customer'] ?? 'Walk-in') ?></td>
            <td><?= e($sale['location']) ?></td>
            <td><?= e($sale['created_by'] ?? 'System') ?></td>
            <td class="text-center"><?= number_format($sale['item_count']) ?></td>
            <td class="text-right"><?= money($sale['discount_amount']) ?></td>
            <td class="text-right text-bold"><?= money($sale['total']) ?></td>
            <td><?= e($sale['payment_method']) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php 
        $total_items = array_sum(array_column($data['sales'], 'item_count'));
        $total_discount = array_sum(array_column($data['sales'], 'discount_amount'));
        $total_amount = array_sum(array_column($data['sales'], 'total'));
        ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="5" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($total_items) ?></td>
            <td class="text-right"><?= money($total_discount) ?></td>
            <td class="text-right"><?= money($total_amount) ?></td>
            <td></td>
        </tr>
    </tbody>
</table>
