<?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; ?>

<?php if ($reportType === 'inventory'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Products</div><div class="value"><?= number_format($data['summary']['total_products']) ?></div></div>
    <div class="summary-card"><div class="label">Stock Value</div><div class="value text-blue"><?= money($data['summary']['total_stock_value']) ?></div></div>
    <div class="summary-card"><div class="label">Low Stock Items</div><div class="value"><?= number_format($data['summary']['low_stock_items']) ?></div></div>
    <div class="summary-card"><div class="label">Out of Stock</div><div class="value text-red"><?= number_format($data['summary']['out_of_stock']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>SKU</th>
            <th>Product</th>
            <th>Category</th>
            <th class="text-center">Stock Qty</th>
            <th class="text-right">Cost Price</th>
            <th class="text-right">Selling Price</th>
            <th class="text-right">Total Value</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['inventory'] as $item): ?>
        <tr>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['name']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($item['stock_quantity']) ?></td>
            <td class="text-right"><?= money($item['cost_price']) ?></td>
            <td class="text-right"><?= money($item['selling_price']) ?></td>
            <td class="text-right"><?= money($item['total_value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['inventory'], 'stock_quantity'))) ?></td>
            <td></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['total_stock_value']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'stock-valuation'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Items</div><div class="value"><?= number_format($data['summary']['total_items']) ?></div></div>
    <div class="summary-card"><div class="label">Cost Value</div><div class="value"><?= money($data['summary']['total_cost_value']) ?></div></div>
    <div class="summary-card"><div class="label">Retail Value</div><div class="value text-blue"><?= money($data['summary']['total_retail_value']) ?></div></div>
    <div class="summary-card"><div class="label">Potential Profit</div><div class="value <?= $data['summary']['total_potential_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_potential_profit']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th>Category</th>
            <th>Location</th>
            <th class="text-center">Qty</th>
            <th class="text-right">Cost Price</th>
            <th class="text-right">Cost Value</th>
            <th class="text-right">Selling Price</th>
            <th class="text-right">Retail Value</th>
            <th class="text-right">Potential Profit</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['stock_data'] as $item): ?>
        <tr>
            <td><?= e($item['product']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td><?= e($item['location']) ?></td>
            <td class="text-center"><?= number_format($item['quantity']) ?></td>
            <td class="text-right"><?= money($item['cost_price']) ?></td>
            <td class="text-right"><?= money($item['cost_value']) ?></td>
            <td class="text-right"><?= money($item['selling_price']) ?></td>
            <td class="text-right"><?= money($item['retail_value']) ?></td>
            <td class="text-right <?= $item['potential_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($item['potential_profit']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="4" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['stock_data'], 'quantity'))) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['total_cost_value']) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['total_retail_value']) ?></td>
            <td class="text-right <?= $data['summary']['total_potential_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_potential_profit']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'sales'): ?>
<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">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>
<table class="data-table">
    <thead>
        <tr>
            <th>Sale No</th>
            <th>Date</th>
            <th>Customer</th>
            <th>Location</th>
            <th>Created By</th>
            <th class="text-center">Items</th>
            <th>Payment</th>
            <th>Status</th>
            <th class="text-right">Total</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'] ?? 'N/A') ?></td>
            <td><?= e($sale['created_by'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($sale['item_count']) ?></td>
            <td><?= e($sale['payment_method'] ?? 'N/A') ?></td>
            <td><?= e(ucfirst($sale['status'] ?? 'N/A')) ?></td>
            <td class="text-right"><?= money($sale['total']) ?></td>
        </tr>
        <?php endforeach; ?>
        <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($data['summary']['total_items_sold']) ?></td>
            <td></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['total_revenue']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'stock-movement'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Movements</div><div class="value"><?= number_format($data['summary']['total_movements']) ?></div></div>
    <div class="summary-card"><div class="label">Sale Movements</div><div class="value"><?= number_format($data['summary']['total_sales']) ?></div></div>
    <div class="summary-card"><div class="label">Purchase Movements</div><div class="value"><?= number_format($data['summary']['total_purchases']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Type</th>
            <th>Date</th>
            <th>Reference</th>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Qty Change</th>
            <th>Location</th>
            <th>Customer / Supplier</th>
            <th>Created By</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['movements'] as $movement): ?>
        <tr>
            <td><?= e($movement['movement_type']) ?></td>
            <td><?= date('Y-m-d H:i', strtotime($movement['created_at'])) ?></td>
            <td><?= e($movement['reference']) ?></td>
            <td><?= e($movement['product']) ?></td>
            <td><?= e($movement['sku']) ?></td>
            <td class="text-center"><?= number_format($movement['quantity_change']) ?></td>
            <td><?= e($movement['location']) ?></td>
            <td><?= e($movement['customer_or_supplier'] ?? 'N/A') ?></td>
            <td><?= e($movement['created_by'] ?? 'N/A') ?></td>
        </tr>
        <?php endforeach; ?>
        <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(array_sum(array_column($data['movements'], 'quantity_change'))) ?></td>
            <td colspan="3"></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'stock-trending'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Period</div><div class="value"><?= number_format($data['summary']['period_days']) ?> days</div></div>
    <div class="summary-card"><div class="label">Total Sold</div><div class="value"><?= number_format($data['summary']['total_sold']) ?></div></div>
    <div class="summary-card"><div class="label">Total Purchased</div><div class="value"><?= number_format($data['summary']['total_purchased']) ?></div></div>
    <div class="summary-card"><div class="label">Transactions</div><div class="value"><?= number_format($data['summary']['total_transactions']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Date</th>
            <th class="text-center">Sold Qty</th>
            <th class="text-center">Purchased Qty</th>
            <th class="text-center">Transactions</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['trends'] as $trend): ?>
        <tr>
            <td><?= e($trend['date']) ?></td>
            <td class="text-center"><?= number_format($trend['sold_quantity']) ?></td>
            <td class="text-center"><?= number_format($trend['purchased_quantity']) ?></td>
            <td class="text-center"><?= number_format($trend['transaction_count']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['summary']['total_sold']) ?></td>
            <td class="text-center"><?= number_format($data['summary']['total_purchased']) ?></td>
            <td class="text-center"><?= number_format($data['summary']['total_transactions']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'profit-loss'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Revenue</div><div class="value text-green"><?= money($data['summary']['total_revenue']) ?></div></div>
    <div class="summary-card"><div class="label">COGS</div><div class="value"><?= money($data['summary']['total_cogs']) ?></div></div>
    <div class="summary-card"><div class="label">Gross Profit</div><div class="value <?= $data['summary']['total_gross_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_gross_profit']) ?></div></div>
    <div class="summary-card"><div class="label">Profit Margin</div><div class="value"><?= number_format($data['summary']['profit_margin'], 2) ?>%</div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Date</th>
            <th class="text-right">Revenue</th>
            <th class="text-right">COGS</th>
            <th class="text-right">Gross Profit</th>
            <th class="text-right">Discounts</th>
            <th class="text-right">Tax</th>
            <th class="text-center">Transactions</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['profit_loss'] as $row): ?>
        <tr>
            <td><?= e($row['date']) ?></td>
            <td class="text-right"><?= money($row['revenue']) ?></td>
            <td class="text-right"><?= money($row['cost_of_goods_sold']) ?></td>
            <td class="text-right <?= $row['gross_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($row['gross_profit']) ?></td>
            <td class="text-right"><?= money($row['total_discounts']) ?></td>
            <td class="text-right"><?= money($row['total_tax']) ?></td>
            <td class="text-center"><?= number_format($row['transaction_count']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td class="text-right"><?= money($data['summary']['total_revenue']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_cogs']) ?></td>
            <td class="text-right <?= $data['summary']['total_gross_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_gross_profit']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_discounts']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_tax']) ?></td>
            <td class="text-center"><?= number_format(count($data['profit_loss'])) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'ai-analytics'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Analysis Period</div><div class="value"><?= e($data['summary']['analysis_period']) ?></div></div>
    <div class="summary-card"><div class="label">Top Category</div><div class="value"><?= e($data['summary']['top_performing_category']) ?></div></div>
    <div class="summary-card"><div class="label">Customers</div><div class="value"><?= number_format($data['summary']['total_customers_analyzed']) ?></div></div>
    <div class="summary-card"><div class="label">Slow Moving Items</div><div class="value"><?= number_format($data['summary']['slow_moving_items']) ?></div></div>
</div>
<h2 style="margin: 10px 0 8px 0;">Top Performing Products</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Units Sold</th>
            <th class="text-right">Average Price</th>
            <th class="text-right">Revenue</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['top_products'] as $row): ?>
        <tr>
            <td><?= e($row['name']) ?></td>
            <td><?= e($row['sku']) ?></td>
            <td class="text-center"><?= number_format($row['total_sold']) ?></td>
            <td class="text-right"><?= money($row['avg_price']) ?></td>
            <td class="text-right"><?= money($row['total_revenue']) ?></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(array_sum(array_column($data['top_products'], 'total_sold'))) ?></td>
            <td></td>
            <td class="text-right"><?= money(array_sum(array_column($data['top_products'], 'total_revenue'))) ?></td>
        </tr>
    </tbody>
</table>
<h2 style="margin: 18px 0 8px 0;">Slow Moving Products</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Stock Qty</th>
            <th class="text-center">Sold Qty</th>
            <th class="text-right">Cost Price</th>
            <th class="text-right">Selling Price</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['slow_moving'] as $row): ?>
        <tr>
            <td><?= e($row['name']) ?></td>
            <td><?= e($row['sku']) ?></td>
            <td class="text-center"><?= number_format($row['stock_quantity']) ?></td>
            <td class="text-center"><?= number_format($row['sold_quantity']) ?></td>
            <td class="text-right"><?= money($row['cost_price']) ?></td>
            <td class="text-right"><?= money($row['selling_price']) ?></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(array_sum(array_column($data['slow_moving'], 'stock_quantity'))) ?></td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['slow_moving'], 'sold_quantity'))) ?></td>
            <td colspan="2"></td>
        </tr>
    </tbody>
</table>
<div class="page-break"></div>
<h2 style="margin: 10px 0 8px 0;">Category Performance</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Category</th>
            <th class="text-center">Transactions</th>
            <th class="text-right">Revenue</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['category_sales'] as $row): ?>
        <tr>
            <td><?= e($row['name']) ?></td>
            <td class="text-center"><?= number_format($row['transaction_count']) ?></td>
            <td class="text-right"><?= money($row['revenue']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['category_sales'], 'transaction_count'))) ?></td>
            <td class="text-right"><?= money(array_sum(array_column($data['category_sales'], 'revenue'))) ?></td>
        </tr>
    </tbody>
</table>
<h2 style="margin: 18px 0 8px 0;">Customer Insights</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Customer</th>
            <th class="text-center">Purchases</th>
            <th class="text-right">Total Spent</th>
            <th class="text-right">Average Purchase</th>
            <th>Last Purchase</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['customer_insights'] as $row): ?>
        <tr>
            <td><?= e($row['name']) ?></td>
            <td class="text-center"><?= number_format($row['purchase_count']) ?></td>
            <td class="text-right"><?= money($row['total_spent']) ?></td>
            <td class="text-right"><?= money($row['avg_purchase']) ?></td>
            <td><?= $row['last_purchase'] ? date('Y-m-d', strtotime($row['last_purchase'])) : 'N/A' ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['customer_insights'], 'purchase_count'))) ?></td>
            <td class="text-right"><?= money(array_sum(array_column($data['customer_insights'], 'total_spent'))) ?></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'expiry'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Batches</div><div class="value"><?= number_format($data['summary']['total_batches']) ?></div></div>
    <div class="summary-card"><div class="label">Expired</div><div class="value text-red"><?= number_format($data['summary']['expired_count']) ?></div></div>
    <div class="summary-card"><div class="label">Expiring Soon</div><div class="value"><?= number_format($data['summary']['expiring_soon_count']) ?></div></div>
    <div class="summary-card"><div class="label">Safe</div><div class="value text-green"><?= number_format($data['summary']['safe_count']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Batch No</th>
            <th>Expiry Date</th>
            <th>MFG Date</th>
            <th>Product</th>
            <th>SKU</th>
            <th>Category</th>
            <th class="text-center">Qty</th>
            <th>Location</th>
            <th class="text-center">Days Left</th>
            <th>Status</th>
            <th class="text-right">Cost Value</th>
            <th class="text-right">Retail Value</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['expiry_data'] as $item): ?>
        <tr>
            <td><?= e($item['batch_no']) ?></td>
            <td><?= $item['expiry_date'] ? date('Y-m-d', strtotime($item['expiry_date'])) : 'N/A' ?></td>
            <td><?= $item['mfg_date'] ? date('Y-m-d', strtotime($item['mfg_date'])) : 'N/A' ?></td>
            <td><?= e($item['product']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($item['quantity']) ?></td>
            <td><?= e($item['location']) ?></td>
            <td class="text-center"><?= number_format($item['days_to_expiry']) ?></td>
            <td><?= e(ucwords(str_replace('_', ' ', $item['expiry_status']))) ?></td>
            <td class="text-right"><?= money($item['cost_value']) ?></td>
            <td class="text-right"><?= money($item['retail_value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="6" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['expiry_data'], 'quantity'))) ?></td>
            <td colspan="3"></td>
            <td class="text-right"><?= money($data['summary']['total_cost_value']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_retail_value']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'tax'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Tax</div><div class="value text-green"><?= money($data['total_tax_collected']) ?></div></div>
    <div class="summary-card"><div class="label">Total Sales</div><div class="value"><?= money($data['total_sales']) ?></div></div>
    <div class="summary-card"><div class="label">Tax Groups</div><div class="value"><?= number_format(count($data['summary'])) ?></div></div>
    <div class="summary-card"><div class="label">Effective Rate</div><div class="value"><?= $data['total_sales'] > 0 ? number_format(($data['total_tax_collected'] / $data['total_sales']) * 100, 2) : 0 ?>%</div></div>
</div>
<h2 style="margin: 10px 0 8px 0;">Tax Group Summary</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Tax Group</th>
            <th>Type</th>
            <th class="text-center">Rate</th>
            <th class="text-center">Sales Count</th>
            <th class="text-right">Total Sales</th>
            <th class="text-right">Tax Collected</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['summary'] as $row): ?>
        <tr>
            <td><?= e($row['tax_group_name']) ?></td>
            <td><?= strtoupper($row['tax_type']) ?></td>
            <td class="text-center"><?= number_format($row['tax_rate'], 2) ?><?= $row['tax_type'] === 'percentage' ? '%' : '' ?></td>
            <td class="text-center"><?= number_format($row['sale_count']) ?></td>
            <td class="text-right"><?= money($row['total_sales_amount']) ?></td>
            <td class="text-right"><?= money($row['total_tax_collected']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td></td>
            <td></td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['summary'], 'sale_count'))) ?></td>
            <td class="text-right"><?= money(array_sum(array_column($data['summary'], 'total_sales_amount'))) ?></td>
            <td class="text-right"><?= money(array_sum(array_column($data['summary'], 'total_tax_collected'))) ?></td>
        </tr>
    </tbody>
</table>
<?php if (!empty($data['tax_by_date'])): ?>
<h2 style="margin: 18px 0 8px 0;">Tax by Date</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Date</th>
            <th class="text-center">Sales Count</th>
            <th class="text-right">Total Sales</th>
            <th class="text-right">Tax Collected</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['tax_by_date'] as $row): ?>
        <tr>
            <td><?= date('Y-m-d', strtotime($row['tax_date'])) ?></td>
            <td class="text-center"><?= number_format($row['sale_count']) ?></td>
            <td class="text-right"><?= money($row['total_sales_amount']) ?></td>
            <td class="text-right"><?= money($row['total_tax_collected']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>
<?php if (!empty($data['tax_by_location'])): ?>
<h2 style="margin: 18px 0 8px 0;">Tax by Location</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Location</th>
            <th class="text-center">Sales Count</th>
            <th class="text-right">Total Sales</th>
            <th class="text-right">Tax Collected</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['tax_by_location'] as $row): ?>
        <tr>
            <td><?= e($row['location_name'] ?? 'Main Location') ?></td>
            <td class="text-center"><?= number_format($row['sale_count']) ?></td>
            <td class="text-right"><?= money($row['total_sales_amount']) ?></td>
            <td class="text-right"><?= money($row['total_tax_collected']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>
<?php if (!empty($data['tax_by_category'])): ?>
<h2 style="margin: 18px 0 8px 0;">Tax by Product Category</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Category</th>
            <th class="text-center">Sales Count</th>
            <th class="text-right">Total Sales</th>
            <th class="text-right">Tax Collected</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['tax_by_category'] as $row): ?>
        <tr>
            <td><?= e($row['category_name'] ?? 'Uncategorized') ?></td>
            <td class="text-center"><?= number_format($row['sale_count']) ?></td>
            <td class="text-right"><?= money($row['total_sales_amount']) ?></td>
            <td class="text-right"><?= money($row['total_tax_collected']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>
<div class="page-break"></div>
<h2 style="margin: 10px 0 8px 0;">Detailed Tax Breakdown</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Sale #</th>
            <th>Date</th>
            <th>Customer</th>
            <th>Location</th>
            <th>Tax Group</th>
            <th class="text-center">Items</th>
            <th class="text-right">Subtotal</th>
            <th class="text-right">Total</th>
            <th class="text-center">Payment Status</th>
            <th class="text-right">Tax Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['details'] as $detail): ?>
        <tr>
            <td><?= e($detail['sale_no']) ?></td>
            <td><?= date('Y-m-d', strtotime($detail['created_at'])) ?></td>
            <td><?= e($detail['customer_name'] ?? 'Walk-in') ?></td>
            <td><?= e($detail['location_name'] ?? 'Main') ?></td>
            <td><?= e($detail['tax_group_name']) ?></td>
            <td class="text-center"><?= number_format($detail['item_count']) ?></td>
            <td class="text-right"><?= money($detail['subtotal']) ?></td>
            <td class="text-right"><?= money($detail['total']) ?></td>
            <td class="text-center"><?= strtoupper($detail['payment_status']) ?></td>
            <td class="text-right"><?= money($detail['tax_amount']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php elseif ($reportType === 'stock-summary'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Opening Stock Value</div><div class="value"><?= money($data['summary']['opening_stock_value']) ?></div></div>
    <div class="summary-card"><div class="label">Current Stock Value</div><div class="value text-blue"><?= money($data['summary']['current_stock_value']) ?></div></div>
    <div class="summary-card"><div class="label">Closing Stock Value</div><div class="value text-green"><?= money($data['summary']['closing_stock_value']) ?></div></div>
    <div class="summary-card"><div class="label">Stock Movement</div><div class="value <?= $data['summary']['stock_movement'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['stock_movement']) ?></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>Category</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['product']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($item['quantity']) ?></td>
            <td class="text-right"><?= money($item['unit_cost']) ?></td>
            <td class="text-right"><?= money($item['value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['opening_stock'], 'quantity'))) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['opening_stock_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>Category</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['product']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($item['quantity']) ?></td>
            <td class="text-right"><?= money($item['unit_cost']) ?></td>
            <td class="text-right"><?= money($item['value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['current_stock'], 'quantity'))) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['current_stock_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>Category</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['product']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category'] ?? 'N/A') ?></td>
            <td class="text-center"><?= number_format($item['quantity']) ?></td>
            <td class="text-right"><?= money($item['unit_cost']) ?></td>
            <td class="text-right"><?= money($item['value']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format(array_sum(array_column($data['closing_stock'], 'quantity'))) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['closing_stock_value']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'top-suppliers'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Suppliers</div><div class="value"><?= number_format($data['summary']['total_suppliers']) ?></div></div>
    <div class="summary-card"><div class="label">Purchases</div><div class="value"><?= number_format($data['summary']['total_purchases']) ?></div></div>
    <div class="summary-card"><div class="label">Quantity</div><div class="value"><?= number_format($data['summary']['total_quantity']) ?></div></div>
    <div class="summary-card"><div class="label">Total Cost</div><div class="value"><?= money($data['summary']['total_cost']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Supplier</th>
            <th class="text-center">Purchase Count</th>
            <th class="text-center">Total Quantity</th>
            <th class="text-center">Unique Products</th>
            <th class="text-right">Avg Unit Cost</th>
            <th class="text-right">Total Cost</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['suppliers'] as $supplier): ?>
        <tr>
            <td><?= e($supplier['supplier'] ?? 'Direct') ?></td>
            <td class="text-center"><?= number_format($supplier['purchase_count']) ?></td>
            <td class="text-center"><?= number_format($supplier['total_quantity']) ?></td>
            <td class="text-center"><?= number_format($supplier['unique_products']) ?></td>
            <td class="text-right"><?= money($supplier['avg_unit_cost']) ?></td>
            <td class="text-right"><?= money($supplier['total_cost']) ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['summary']['total_purchases']) ?></td>
            <td class="text-center"><?= number_format($data['summary']['total_quantity']) ?></td>
            <td></td>
            <td class="text-right"><?= money($data['summary']['avg_unit_cost']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_cost']) ?></td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'product-sales'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Products</div><div class="value"><?= number_format($data['summary']['total_products']) ?></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">Total Units Sold</div><div class="value"><?= number_format($data['summary']['total_units_sold']) ?></div></div>
    <div class="summary-card"><div class="label">Average Price</div><div class="value"><?= money($data['summary']['average_price']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th>Category</th>
            <th class="text-center">Units Sold</th>
            <th class="text-right">Revenue</th>
            <th class="text-right">Cost</th>
            <th class="text-right">Profit</th>
            <th class="text-right">Margin</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['product_sales'] as $product): ?>
        <tr>
            <td><?= e($product['name']) ?></td>
            <td><?= e($product['sku']) ?></td>
            <td><?= e($product['category']) ?></td>
            <td class="text-center"><?= number_format($product['units_sold']) ?></td>
            <td class="text-right"><?= money($product['revenue']) ?></td>
            <td class="text-right"><?= money($product['cost']) ?></td>
            <td class="text-right <?= $product['profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($product['profit']) ?></td>
            <td class="text-right <?= $product['margin'] >= 0 ? 'text-green' : 'text-red' ?>"><?= number_format($product['margin'], 1) ?>%</td>
        </tr>
        <?php endforeach; ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($data['summary']['total_units_sold']) ?></td>
            <td class="text-right"><?= money($data['summary']['total_revenue']) ?></td>
            <td class="text-right"><?= money(array_sum(array_column($data['product_sales'], 'cost'))) ?></td>
            <td class="text-right <?= $data['summary']['total_revenue'] - array_sum(array_column($data['product_sales'], 'cost')) >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_revenue'] - array_sum(array_column($data['product_sales'], 'cost'))) ?></td>
            <td class="text-right"><?= number_format(($data['summary']['total_revenue'] - array_sum(array_column($data['product_sales'], 'cost'))) / $data['summary']['total_revenue'] * 100, 1) ?>%</td>
        </tr>
    </tbody>
</table>
<?php elseif ($reportType === 'daily-stock-monitoring'): ?>
<div class="summary-cards" style="margin-bottom:20px;">
    <div class="summary-card"><div class="label">Total Products</div><div class="value"><?= number_format($data['summary']['total_products']) ?></div></div>
    <div class="summary-card"><div class="label">Opening Stock</div><div class="value text-blue"><?= number_format($data['summary']['total_opening_stock']) ?></div></div>
    <div class="summary-card"><div class="label">Total Sold</div><div class="value text-red"><?= number_format($data['summary']['total_sold']) ?></div></div>
    <div class="summary-card"><div class="label">Closing Stock</div><div class="value text-green"><?= number_format($data['summary']['total_closing_stock']) ?></div></div>
</div>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th>Category</th>
            <th class="text-center">Opening Stock</th>
            <th class="text-center">Total Sold</th>
            <th class="text-center">Closing Stock</th>
            <th class="text-center">Current Stock</th>
            <th class="text-center">Stock Movement</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['stock_data'] as $item): ?>
        <tr>
            <td><?= e($item['name']) ?></td>
            <td><?= e($item['sku']) ?></td>
            <td><?= e($item['category']) ?></td>
            <td class="text-center"><?= number_format($item['opening_stock']) ?></td>
            <td class="text-center"><?= number_format($item['total_sold']) ?></td>
            <td class="text-center"><?= number_format($item['closing_stock']) ?></td>
            <td class="text-center"><?= number_format($item['current_stock']) ?></td>
            <td class="text-center"><?= money($item['stock_movement']) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php
        $total_opening = array_sum(array_column($data['stock_data'], 'opening_stock'));
        $total_sold = array_sum(array_column($data['stock_data'], 'total_sold'));
        $total_closing = array_sum(array_column($data['stock_data'], 'closing_stock'));
        $total_current = array_sum(array_column($data['stock_data'], 'current_stock'));
        $total_movement = array_sum(array_column($data['stock_data'], 'stock_movement'));
        ?>
        <tr class="font-bold bg-gray-50 border-t-2 border-gray-200">
            <td colspan="3" class="text-right">TOTALS:</td>
            <td class="text-center"><?= number_format($total_opening) ?></td>
            <td class="text-center"><?= number_format($total_sold) ?></td>
            <td class="text-center"><?= number_format($total_closing) ?></td>
            <td class="text-center"><?= number_format($total_current) ?></td>
            <td class="text-center"><?= money($total_movement) ?></td>
        </tr>
    </tbody>
</table>
<?php else: ?>
<div class="filters-info">
    Printable view is not available for this report type yet.
</div>
<?php endif; ?>
