<?php $pageTitle = 'Factoring Management'; ?>
<div class="page-heading">
    <div><p class="eyebrow">Receivables Funding</p><h2>Factoring</h2><p>Track submissions, advances, fees, reserves, and chargebacks.</p></div>
</div>
<div class="content-grid two-col">
    <section class="card">
        <div class="card-header"><h3>Add or update factoring</h3></div>
        <form method="post" action="<?= url('/accounting/factoring/store') ?>" class="form-grid compact-form">
            <?= csrf_field() ?>
            <label class="field field-full"><span>Invoice</span><select name="invoice_id" required><option value="">Select invoice</option><?php foreach ($invoices as $invoice): ?><option value="<?= (int) $invoice['id'] ?>"><?= e($invoice['invoice_number'] . ' — ' . ($invoice['broker_name'] ?: 'Customer') . ' — ' . money($invoice['total'])) ?></option><?php endforeach; ?></select></label>
            <label class="field"><span>Factoring company</span><select name="vendor_id"><option value="">Select vendor</option><?php foreach ($vendors as $vendor): ?><option value="<?= (int) $vendor['id'] ?>"><?= e($vendor['company_name']) ?></option><?php endforeach; ?></select></label>
            <label class="field"><span>Status</span><select name="status"><?php foreach (['not_factored','submitted','funded','reserve_pending','completed','charged_back'] as $status): ?><option value="<?= e($status) ?>"><?= e(readable_status($status)) ?></option><?php endforeach; ?></select></label>
            <label class="field"><span>Submitted at</span><input type="datetime-local" name="submitted_at" value="<?= date('Y-m-d\TH:i') ?>"></label>
            <label class="field"><span>Advance %</span><input type="number" step="0.01" min="0" max="100" name="advance_percent"></label>
            <label class="field"><span>Advance received</span><input type="number" step="0.01" min="0" name="advance_received"></label>
            <label class="field"><span>Factoring fee</span><input type="number" step="0.01" min="0" name="fee_amount"></label>
            <label class="field"><span>Reserve amount</span><input type="number" step="0.01" min="0" name="reserve_amount"></label>
            <label class="field"><span>Reserve released</span><input type="number" step="0.01" min="0" name="reserve_released"></label>
            <label class="field"><span>Chargeback</span><input type="number" step="0.01" min="0" name="chargeback_amount"></label>
            <label class="field field-full"><span>Notes</span><textarea name="notes" rows="3"></textarea></label>
            <div class="field field-full"><button class="btn btn-primary" type="submit">Save factoring record</button></div>
        </form>
    </section>
    <section class="card">
        <div class="card-header"><h3>Factoring workflow</h3></div>
        <div class="info-list">
            <div><strong>Submitted</strong><span>Invoice and supporting documents sent.</span></div>
            <div><strong>Funded</strong><span>Advance received from factoring company.</span></div>
            <div><strong>Reserve pending</strong><span>Waiting for reserve release.</span></div>
            <div><strong>Completed</strong><span>Invoice fully settled.</span></div>
        </div>
    </section>
</div>
<section class="card table-card">
    <div class="card-header"><h3>Factoring records</h3></div>
    <div class="table-wrap"><table><thead><tr><th>Invoice</th><th>Customer</th><th>Company</th><th>Status</th><th>Advance</th><th>Fee</th><th>Reserve</th><th>Chargeback</th></tr></thead><tbody>
    <?php foreach ($records as $record): ?><tr><td><?= e($record['invoice_number']) ?></td><td><?= e($record['broker_name'] ?: '-') ?></td><td><?= e($record['factoring_company'] ?: '-') ?></td><td><span class="badge badge-<?= e(status_class($record['status'])) ?>"><?= e(readable_status($record['status'])) ?></span></td><td><?= money($record['advance_received']) ?></td><td><?= money($record['fee_amount']) ?></td><td><?= money($record['reserve_amount'] - $record['reserve_released']) ?></td><td><?= money($record['chargeback_amount']) ?></td></tr><?php endforeach; ?>
    <?php if (!$records): ?><tr><td colspan="8" class="empty-state">No factoring records yet.</td></tr><?php endif; ?>
    </tbody></table></div>
</section>
