<div class="filters-info">
    <strong>Filters:</strong>
    <span style="margin-left:6px;">Date From: <?= e($data['filters']['date_from']) ?></span>
    <span style="margin-left:12px;">Date To: <?= e($data['filters']['date_to']) ?></span>
</div>

<div class="summary-cards" style="margin-bottom: 20px;">
    <div class="summary-card">
        <div class="label">Total Items</div>
        <div class="value"><?= number_format($data['summary']['total_items']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Total Quantity</div>
        <div class="value text-blue"><?= number_format($data['summary']['total_quantity']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Total Cost</div>
        <div class="value text-red"><?= money($data['summary']['total_cost']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Avg Unit Cost</div>
        <div class="value"><?= money($data['summary']['avg_unit_cost']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Potential Revenue</div>
        <div class="value text-blue"><?= money($data['summary']['total_potential_revenue']) ?></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 class="summary-card">
        <div class="label">Unique Suppliers</div>
        <div class="value"><?= number_format($data['summary']['unique_suppliers']) ?></div>
    </div>
    <div class="summary-card">
        <div class="label">Unique Products</div>
        <div class="value"><?= number_format($data['summary']['unique_products']) ?></div>
    </div>
</div>

<h2 style="margin: 10px 0 8px 0;">Top Suppliers by Cost</h2>
<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-right">Avg Unit Cost</th>
            <th class="text-right">Total Cost</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['top_suppliers'] as $row): ?>
        <tr>
            <td><?= e($row['supplier'] ?? 'Direct') ?></td>
            <td class="text-center"><?= number_format($row['purchase_count']) ?></td>
            <td class="text-center"><?= number_format($row['total_quantity']) ?></td>
            <td class="text-right"><?= money($row['avg_unit_cost']) ?></td>
            <td class="text-right"><?= money($row['total_cost']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2 style="margin: 18px 0 8px 0;">Category Breakdown</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Category</th>
            <th class="text-center">Product Count</th>
            <th class="text-center">Total Quantity</th>
            <th class="text-right">Total Cost</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['category_breakdown'] as $row): ?>
        <tr>
            <td><?= e($row['category'] ?? 'Uncategorized') ?></td>
            <td class="text-center"><?= number_format($row['product_count']) ?></td>
            <td class="text-center"><?= number_format($row['total_quantity']) ?></td>
            <td class="text-right"><?= money($row['total_cost']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2 style="margin: 18px 0 8px 0;">Most Purchased Products</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Product</th>
            <th>SKU</th>
            <th class="text-center">Purchase Count</th>
            <th class="text-center">Total Quantity</th>
            <th class="text-right">Avg Unit Cost</th>
            <th class="text-right">Total Cost</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['top_products'] as $row): ?>
        <tr>
            <td><?= e($row['product']) ?></td>
            <td><?= e($row['sku']) ?></td>
            <td class="text-center"><?= number_format($row['purchase_count']) ?></td>
            <td class="text-center"><?= number_format($row['total_quantity']) ?></td>
            <td class="text-right"><?= money($row['avg_unit_cost']) ?></td>
            <td class="text-right"><?= money($row['total_cost']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="page-break"></div>

<h2 style="margin: 10px 0 8px 0;">Purchase Items Detail</h2>
<table class="data-table">
    <thead>
        <tr>
            <th>Purchase No</th>
            <th>Date</th>
            <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">Total Cost</th>
            <th>Supplier</th>
            <th>Location</th>
            <th class="text-right">Potential Revenue</th>
            <th class="text-right">Potential Profit</th>
            <th class="text-center">Margin</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['purchase_items'] as $item): ?>
        <tr>
            <td><?= e($item['purchase_no']) ?></td>
            <td><?= date('Y-m-d', strtotime($item['purchase_date'])) ?></td>
            <td><?= e($item['product_name']) ?></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['total']) ?></td>
            <td><?= e($item['supplier'] ?? 'Direct') ?></td>
            <td><?= e($item['location']) ?></td>
            <td class="text-right"><?= money($item['potential_revenue']) ?></td>
            <td class="text-right <?= $item['potential_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($item['potential_profit']) ?></td>
            <td class="text-center">
                <?php $margin = $item['unit_cost'] > 0 ? (($item['selling_price'] - $item['unit_cost']) / $item['unit_cost']) * 100 : 0; ?>
                <span class="badge <?= $margin >= 0 ? 'badge-green' : 'badge-red' ?>"><?= number_format($margin, 1) ?>%</span>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="5" class="text-bold">TOTALS</td>
            <td class="text-center text-bold"><?= number_format($data['summary']['total_quantity']) ?></td>
            <td class="text-right text-bold"><?= money($data['summary']['avg_unit_cost']) ?></td>
            <td class="text-right text-bold"><?= money($data['summary']['total_cost']) ?></td>
            <td colspan="2"></td>
            <td class="text-right text-bold"><?= money($data['summary']['total_potential_revenue']) ?></td>
            <td class="text-right text-bold <?= $data['summary']['total_potential_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= money($data['summary']['total_potential_profit']) ?></td>
            <td></td>
        </tr>
    </tfoot>
</table>
