<?php
$summary = $summary ?? [];
$directionLabels = [
    'output'           => 'Output',
    'output_reduction' => 'Output reduction',
    'input'            => 'Input',
    'input_reduction'  => 'Input reduction',
];
?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Summary by Document Type</h2>
        <p class="enterprise-data-panel__subtitle"><?= esc($summary['from'] ?? '') ?> → <?= esc($summary['to'] ?? '') ?></p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Document Type</th>
                        <th>Direction</th>
                        <th class="text-end">Documents</th>
                        <th class="text-end">Tax Total</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach (($summary['breakdown'] ?? []) as $block): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($block['label']) ?></span></td>
                            <td><?= esc($directionLabels[$block['direction']] ?? $block['direction']) ?></td>
                            <td class="text-end"><?= (int) $block['count'] ?></td>
                            <td class="text-end"><?= number_format((float) $block['tax_total'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php foreach (($summary['breakdown'] ?? []) as $block): ?>
    <?php if ((int) $block['count'] === 0) {
        continue;
    } ?>
    <div class="enterprise-data-panel mb-4">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title"><?= esc($block['label']) ?></h2>
            <p class="enterprise-data-panel__subtitle">
                <?= (int) $block['count'] ?> document(s) · Tax <?= number_format((float) $block['tax_total'], 2) ?>
            </p>
        </div>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Number</th>
                            <th>Date</th>
                            <th>Party</th>
                            <th>Status</th>
                            <th class="text-end">Tax</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($block['rows'] as $row): ?>
                            <tr>
                                <td>
                                    <?php if (! empty($block['path'])): ?>
                                        <a href="<?= base_url(rtrim($block['path'], '/') . '/' . (int) $row['id']) ?>" class="enterprise-data-table__primary">
                                            <code><?= esc($row['number']) ?></code>
                                        </a>
                                    <?php else: ?>
                                        <code><?= esc($row['number']) ?></code>
                                    <?php endif; ?>
                                </td>
                                <td><?= esc($row['doc_date'] ?? '—') ?></td>
                                <td><?= esc($row['party_name'] ?? '—') ?></td>
                                <td><?= esc(ucfirst(str_replace('_', ' ', $row['status'] ?? ''))) ?></td>
                                <td class="text-end"><?= number_format((float) ($row['tax_amount'] ?? 0), 2) ?></td>
                                <td class="text-end"><?= number_format((float) ($row['total'] ?? 0), 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endforeach; ?>
