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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Customer Statements',
    'workspaceTitle'        => 'Customer Statements',
    'workspaceDesc'         => 'Review posted invoices, receipts, and credit notes with running balance.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Statement</h2>
        <p class="enterprise-data-panel__subtitle">Select a customer and optional date range.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/customer-statements') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Customer</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">— Select customer —</option>
                        <?php foreach ($companies as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string) ($filters['companyId'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                                <?= esc($c['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">From</label>
                    <input type="date" name="date_from" class="form-control form-control-sm" value="<?= esc($filters['dateFrom'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To</label>
                    <input type="date" name="date_to" class="form-control form-control-sm" value="<?= esc($filters['dateTo'] ?? '') ?>">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Generate</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <?php if (empty($filters['companyId'])): ?>
            <p class="text-center text-muted py-5 mb-0">Select a customer to generate a statement.</p>
        <?php else: ?>
            <?php if (! empty($company)): ?>
                <p class="small text-muted mb-3">Statement for <strong><?= esc($company['name']) ?></strong>
                    <?php if (! empty($filters['dateFrom']) || ! empty($filters['dateTo'])): ?>
                        · <?= esc($filters['dateFrom'] ?: '…') ?> → <?= esc($filters['dateTo'] ?: '…') ?>
                    <?php endif; ?>
                </p>
            <?php endif; ?>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Document</th>
                            <th>Type</th>
                            <th class="text-end">Debit</th>
                            <th class="text-end">Credit</th>
                            <th class="text-end">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <td><?= esc($row['date']) ?></td>
                                <td><code><?= esc($row['doc']) ?></code></td>
                                <td><?= esc($row['type']) ?></td>
                                <td class="text-end"><?= $row['debit'] > 0 ? number_format($row['debit'], 2) : '—' ?></td>
                                <td class="text-end"><?= $row['credit'] > 0 ? number_format($row['credit'], 2) : '—' ?></td>
                                <td class="text-end"><?= number_format($row['balance'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (count($rows) === 0): ?>
                            <tr><td colspan="6" class="text-center text-muted py-5">No posted activity in this range.</td></tr>
                        <?php else: ?>
                            <tr>
                                <td colspan="5" class="text-end fw-semibold">Closing balance</td>
                                <td class="text-end fw-semibold"><?= number_format((float) $balance, 2) ?></td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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