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

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

<?php
$statusPill = static function (string $status): string {
    $label    = ucwords(str_replace('_', ' ', $status));
    $active   = ['issued', 'partially_paid', 'paid'];
    $inactive = ['cancelled'];
    if (in_array($status, $active, true)) {
        $class = 'active';
    } elseif (in_array($status, $inactive, true)) {
        $class = 'inactive';
    } else {
        $class = 'default';
    }
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};

$totalOutstanding = $totalOutstanding ?? 0;
if ($totalOutstanding === 0 && ! empty($invoices)) {
    foreach ($invoices as $row) {
        $totalOutstanding += max(0, (float) ($row['total'] ?? 0) - (float) ($row['amount_paid'] ?? 0));
    }
}
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Outstanding Payments',
    'workspaceTitle'        => 'Outstanding Payments',
    'workspaceDesc'         => 'Review unpaid and overdue customer balances.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Outstanding Balances</h2>
        <p class="enterprise-data-panel__subtitle">
            Invoices with remaining balance.
            <?php if ($totalOutstanding > 0): ?>
                Total outstanding: <strong><?= number_format((float) $totalOutstanding, 2) ?></strong>
            <?php endif; ?>
        </p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/outstanding-payments') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Number, company"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Company</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">Any</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 class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Company</th>
                        <th>Due Date</th>
                        <th class="text-end">Total</th>
                        <th class="text-end">Paid</th>
                        <th class="text-end">Outstanding</th>
                        <th>Status</th>
                        <th class="text-end">Link</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($invoices as $inv): ?>
                        <?php
                        $outstanding = max(0, (float) ($inv['total'] ?? 0) - (float) ($inv['amount_paid'] ?? 0));
                        ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($inv['number']) ?></code></span></td>
                            <td><?= esc($inv['company_name'] ?? '—') ?></td>
                            <td><?= ! empty($inv['due_date']) ? esc($inv['due_date']) : '—' ?></td>
                            <td class="text-end"><?= number_format((float) ($inv['total'] ?? 0), 2) ?></td>
                            <td class="text-end"><?= number_format((float) ($inv['amount_paid'] ?? 0), 2) ?></td>
                            <td class="text-end"><strong><?= number_format($outstanding, 2) ?></strong></td>
                            <td><?= $statusPill($inv['status']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('sale/invoices') ?>?q=<?= urlencode($inv['number']) ?>" class="btn btn-sm btn-outline-secondary" title="View invoices">
                                    <i class="fa-solid fa-arrow-up-right-from-square"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($invoices) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No outstanding payments found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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