<?= $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>';
};

$paymentStatuses = ['issued', 'partially_paid', 'paid', 'overdue'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Payment Status',
    'workspaceTitle'        => 'Payment Status',
    'workspaceDesc'         => 'Record payments and update invoice payment status.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Invoice Payments</h2>
        <p class="enterprise-data-panel__subtitle">Issued, partially paid, paid, and overdue invoices.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/payment-status') ?>" 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">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($paymentStatuses as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
                                <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </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>Invoice Date</th>
                        <th>Due Date</th>
                        <th class="text-end">Total</th>
                        <th>Status</th>
                        <th style="min-width:280px;">Update Payment</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($invoices as $inv): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($inv['number']) ?></code></span></td>
                            <td><?= esc($inv['company_name'] ?? '—') ?></td>
                            <td><?= esc($inv['invoice_date'] ?? '—') ?></td>
                            <td><?= ! empty($inv['due_date']) ? esc($inv['due_date']) : '—' ?></td>
                            <td class="text-end"><?= number_format((float) ($inv['total'] ?? 0), 2) ?></td>
                            <td><?= $statusPill($inv['status']) ?></td>
                            <td>
                                <form action="<?= base_url('sale/payment-status/' . $inv['id']) ?>" method="post" class="d-flex flex-column gap-2">
                                    <?= csrf_field() ?>
                                    <div class="d-flex gap-2 align-items-end flex-wrap">
                                        <div class="flex-grow-1" style="min-width:100px;">
                                            <label class="enterprise-field-label">Amount Paid</label>
                                            <input type="number" name="amount_paid" class="form-control form-control-sm"
                                                   step="0.01" min="0"
                                                   value="<?= esc($inv['amount_paid'] ?? '0') ?>" required>
                                        </div>
                                        <button type="submit" class="btn btn-sm btn-primary">Save</button>
                                    </div>
                                    <div>
                                        <label class="enterprise-field-label">Payment Notes</label>
                                        <input type="text" name="payment_notes" class="form-control form-control-sm"
                                               placeholder="Optional notes"
                                               value="<?= esc($inv['payment_notes'] ?? '') ?>">
                                    </div>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($invoices) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No invoices found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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