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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Customer Analysis',
    'workspaceTitle'        => 'Customer Analysis',
    'workspaceDesc'         => 'Revenue concentration, receivables, returns, and engagement by customer.',
]) ?>

<?= 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">Segment by period, company, or customer group.</p>
    </div>
    <?= view('Modules\Sale\Views\reports\_filters', [
        'actionPath'  => 'sale/reports/customer-analysis',
        'filters'     => $filters,
        'companies'   => $companies,
        'groups'      => $groups,
        'showCompany' => true,
        'showGroup'   => true,
    ]) ?>
</div>

<section class="enterprise-charts-grid mb-4">
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">TOP CUSTOMERS BY REVENUE</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="customerRevenueChart" height="220"></canvas>
        </div>
    </article>
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">REVENUE BY GROUP</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="customerGroupChart" 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">Top Customers</h2>
        <p class="enterprise-data-panel__subtitle">Invoiced revenue in the selected period (excluding drafts and cancelled).</p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Company</th>
                        <th>Group</th>
                        <th class="text-end">Invoices</th>
                        <th class="text-end">Revenue</th>
                        <th class="text-end">Paid</th>
                        <th class="text-end">Outstanding</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($report['topCustomers'] ?? [] as $row): ?>
                        <?php $outstanding = max(0, (float) $row['revenue'] - (float) $row['paid']); ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('sale/companies/' . $row['id']) ?>" class="enterprise-data-table__primary text-decoration-none">
                                    <?= esc($row['company_name']) ?>
                                </a>
                            </td>
                            <td><?= esc($row['group_name'] ?: '—') ?></td>
                            <td class="text-end"><?= (int) $row['invoice_count'] ?></td>
                            <td class="text-end fw-semibold"><?= number_format((float) $row['revenue'], 2) ?></td>
                            <td class="text-end"><?= number_format((float) $row['paid'], 2) ?></td>
                            <td class="text-end"><?= number_format($outstanding, 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($report['topCustomers'])): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No invoiced customers in this period.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="enterprise-data-panel h-100">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Outstanding Receivables</h2>
                <p class="enterprise-data-panel__subtitle">Open balances by company.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Company</th>
                                <th class="text-end">Invoices</th>
                                <th class="text-end">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['outstanding'] ?? [] as $row): ?>
                                <tr>
                                    <td class="enterprise-data-table__primary"><?= esc($row['company_name']) ?></td>
                                    <td class="text-end"><?= (int) $row['invoice_count'] ?></td>
                                    <td class="text-end fw-semibold"><?= number_format((float) $row['balance'], 2) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['outstanding'])): ?>
                                <tr><td colspan="3" class="text-center text-muted py-4">No outstanding balances.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="enterprise-data-panel h-100">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Activity Volume</h2>
                <p class="enterprise-data-panel__subtitle">Calls, emails, meetings, and tasks by company.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Company</th>
                                <th>Type</th>
                                <th class="text-end">Count</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['activity'] ?? [] as $row): ?>
                                <tr>
                                    <td class="enterprise-data-table__primary"><?= esc($row['company_name'] ?: '—') ?></td>
                                    <td><?= esc(ucwords(str_replace('_', ' ', $row['activity_type'] ?? ''))) ?></td>
                                    <td class="text-end"><?= (int) $row['total'] ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['activity'])): ?>
                                <tr><td colspan="3" class="text-center text-muted py-4">No activities in this period.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</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() ?>
