<?php
$items = old('item_description') ? [] : ($invoice['items'] ?? []);
if (!$items) {
    $defaultDescription = 'Freight charge';
    $defaultAmount = $selectedLoad['rate'] ?? ($invoice['subtotal'] ?? 0);
    $items = [['description' => $defaultDescription, 'amount' => $defaultAmount]];
}
?>
<form method="post" action="<?= e($action) ?>" class="panel form-stack" id="invoice-form">
    <div class="form-grid three">
    <?= csrf_field() ?>
    <?php if (!empty($invoice['id'])): ?><input type="hidden" name="id" value="<?= e((string) $invoice['id']) ?>"><?php endif; ?>
    <input type="hidden" name="default_amount" value="<?= e((string) ($selectedLoad['rate'] ?? $invoice['subtotal'] ?? 0)) ?>">

    <label>
        <span>Invoice Number *</span>
        <input name="invoice_number" value="<?= e(old('invoice_number', $invoice['invoice_number'] ?? $nextNumber)) ?>" required>
    </label>
    <label>
        <span>Status</span>
        <select name="status">
            <?php foreach ($statuses as $status): ?>
                <option value="<?= e($status) ?>" <?= old('status', $invoice['status'] ?? 'draft') === $status ? 'selected' : '' ?>><?= e(readable_status($status)) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        <span>Load</span>
        <select name="load_id" id="invoice-load-select">
            <option value="">No load selected</option>
            <?php foreach ($loads as $load): ?>
                <?php $selected = (string) old('load_id', $invoice['load_id'] ?? $selectedLoad['id'] ?? '') === (string) $load['id']; ?>
                <option value="<?= e((string) $load['id']) ?>" data-broker="<?= e((string) ($load['broker_id'] ?? '')) ?>" data-rate="<?= e((string) $load['rate']) ?>" <?= $selected ? 'selected' : '' ?>><?= e($load['load_number']) ?> — <?= e($load['broker_name'] ?? '-') ?> — <?= money($load['rate']) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        <span>Broker / Customer</span>
        <select name="broker_id" id="invoice-broker-select">
            <option value="">Select broker</option>
            <?php foreach ($brokers as $broker): ?>
                <option value="<?= e((string) $broker['id']) ?>" <?= (string) old('broker_id', $invoice['broker_id'] ?? $selectedLoad['broker_id'] ?? '') === (string) $broker['id'] ? 'selected' : '' ?>><?= e($broker['company_name']) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        <span>Due Date</span>
        <input type="date" name="due_date" value="<?= e(old('due_date', $invoice['due_date'] ?? date('Y-m-d', strtotime('+30 days')))) ?>">
    </label>
    <label>
        <span>Deductions</span>
        <input type="number" step="0.01" name="deductions" value="<?= e(old('deductions', $invoice['deductions'] ?? '0')) ?>">
    </label>

    </div>
    <div class="line-items" id="invoice-items">
        <div class="section-title-row">
            <h3>Line Items</h3>
            <button class="btn btn-light btn-sm" type="button" data-add-invoice-item>Add Item</button>
        </div>
        <?php foreach ($items as $item): ?>
            <div class="line-item-row">
                <input name="item_description[]" value="<?= e(old('item_description.0', $item['description'] ?? 'Freight charge')) ?>" placeholder="Description">
                <input type="number" step="0.01" name="item_amount[]" value="<?= e((string) ($item['amount'] ?? 0)) ?>" placeholder="Amount">
                <button class="btn btn-danger btn-sm" type="button" data-remove-row>Remove</button>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="form-grid three">
    <label><span>Invoice Date</span><input type="date" name="invoice_date" value="<?= e(old('invoice_date', $invoice['invoice_date'] ?? date('Y-m-d'))) ?>"></label>
    <label><span>Tax</span><input type="number" step="0.01" name="tax" value="<?= e(old('tax', $invoice['tax'] ?? '0')) ?>"></label>
    <label><span>Discount</span><input type="number" step="0.01" name="discount" value="<?= e(old('discount', $invoice['discount'] ?? '0')) ?>"></label>
    <label><span>Payment Terms</span><input name="payment_terms" value="<?= e(old('payment_terms', $invoice['payment_terms'] ?? 'Net 30')) ?>"></label>
    </div>
    <label>
        <span>Internal Notes</span>
        <textarea name="notes" rows="3"><?= e(old('notes', $invoice['notes'] ?? '')) ?></textarea>
    </label>
    <label><span>Customer-facing Billing Notes</span><textarea name="billing_notes" rows="3"><?= e(old('billing_notes', $invoice['billing_notes'] ?? '')) ?></textarea></label>

    <div class="form-actions">
        <button class="btn btn-primary" type="submit">Save Invoice</button>
        <a class="btn btn-light" href="<?= url('/invoices') ?>">Cancel</a>
    </div>
</form>
