<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Sales Reports',
    'workspaceTitle'        => 'Sales Reports',
    'workspaceDesc'         => 'Operational summary across quotations, orders, invoices, deliveries, returns, and refunds.',
]) ?>

<?= view('Modules\Sale\Views\reports\_kpis', ['kpis' => $report['kpis'] ?? []]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Filters</h2>
        <p class="enterprise-data-panel__subtitle">Period and company scope for the operational rollup.</p>
    </div>
    <?= view('Modules\Sale\Views\reports\_filters', [
        'actionPath'  => 'sale/reports/sales-reports',
        'filters'     => $filters,
        'companies'   => $companies,
        'showCompany' => true,
    ]) ?>
</div>

<section class="enterprise-charts-grid mb-4">
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">INVOICE REVENUE TREND</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="opsMonthlyChart" height="220"></canvas>
        </div>
    </article>
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">INVOICE STATUS MIX</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="opsInvoiceStatusChart" height="220"></canvas>
        </div>
    </article>
</section>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Receivables Aging</h2>
        <p class="enterprise-data-panel__subtitle">Outstanding invoice balances by days past due.</p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <?php foreach (array_keys($report['aging'] ?? []) as $bucket): ?>
                            <th class="text-end"><?= esc($bucket) ?></th>
                        <?php endforeach; ?>
                        <th class="text-end">Total</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <?php
                        $agingTotal = 0.0;
                        foreach ($report['aging'] ?? [] as $amount):
                            $agingTotal += (float) $amount;
                        ?>
                            <td class="text-end"><?= number_format((float) $amount, 2) ?></td>
                        <?php endforeach; ?>
                        <td class="text-end fw-semibold"><?= number_format($agingTotal, 2) ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="row g-4">
    <?php foreach ($report['sections'] ?? [] as $section): ?>
        <div class="col-lg-6">
            <div class="enterprise-data-panel h-100">
                <div class="enterprise-data-panel__head">
                    <h2 class="enterprise-data-panel__title">
                        <a href="<?= base_url($section['path']) ?>" class="text-decoration-none text-reset">
                            <?= esc($section['label']) ?>
                        </a>
                    </h2>
                    <p class="enterprise-data-panel__subtitle">
                        <?= (int) $section['count'] ?> records
                        <?php if ($section['total'] !== null): ?>
                            · <?= number_format((float) $section['total'], 2) ?> value
                        <?php endif; ?>
                    </p>
                </div>
                <div class="enterprise-data-panel__body">
                    <div class="table-responsive">
                        <table class="enterprise-data-table">
                            <thead>
                                <tr>
                                    <th>Status</th>
                                    <th class="text-end">Count</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($section['mix']['rows'] ?? [] as $row): ?>
                                    <tr>
                                        <td><?= esc(ucwords(str_replace('_', ' ', $row['status']))) ?></td>
                                        <td class="text-end"><?= (int) $row['total'] ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (empty($section['mix']['rows'])): ?>
                                    <tr><td colspan="2" class="text-center text-muted py-4">No records.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
window.saleReportCharts = <?= json_encode($report['charts'] ?? [], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
</script>
<script src="<?= base_url('assets/js/sale-reports.js') ?>"></script>
<?= $this->endSection() ?>
