<?php $pageTitle='Invoice '.$invoice['invoice_number']; ?>
<div class="content-grid two-col">
  <div class="panel invoice-preview">
    <div class="panel-header no-print"><div><p class="eyebrow">Customer Invoice</p><h2><?= e($invoice['invoice_number']) ?></h2></div><div class="action-row"><a class="btn btn-light" href="<?= url('/invoices/pdf?id='.$invoice['id']) ?>">Download PDF</a><?php if(!empty($invoice['broker_id']) && can('accounting.view')):?><a class="btn btn-light" href="<?= url('/accounting/customer-statement?broker_id='.$invoice['broker_id']) ?>">Customer Statement</a><?php endif;?><button class="btn btn-primary" onclick="window.print()">Print</button></div></div>
    <div class="invoice-head"><div class="invoice-brand"><?php if(setting('company_logo','')):?><img src="<?= url('/branding/logo') ?>" alt="Company logo"><?php endif;?><div><h2><?= e(setting('company_name',APP_NAME)?:APP_NAME) ?></h2><p><?= e(setting('company_address','')?:'') ?></p><p><?= e(setting('company_email','')?:'') ?> · <?= e(setting('company_phone','')?:'') ?></p><small>MC <?= e(setting('company_mc','')?:'-') ?> · DOT <?= e(setting('company_dot','')?:'-') ?></small></div></div><div class="text-right"><h3>INVOICE</h3><p><strong><?= e($invoice['invoice_number']) ?></strong></p><span class="badge badge-<?= status_class($invoice['status']) ?>"><?= e(readable_status($invoice['status'])) ?></span></div></div>
    <div class="invoice-meta-grid"><div><small>Bill To</small><strong><?= e($invoice['broker_name']??'-') ?></strong><p><?= e($invoice['broker_address']??'') ?></p><p><?= e($invoice['broker_email']??'') ?> <?= e($invoice['broker_phone']??'') ?></p></div><div><small>Load Details</small><strong><?= e($invoice['load_number']??'-') ?> · Ref <?= e($invoice['reference_number']??'-') ?></strong><p><?= e($invoice['pickup_location']??'-') ?> → <?= e($invoice['delivery_location']??'-') ?></p><p><?= e($invoice['driver_name']??'-') ?> · <?= e($invoice['unit_number']??'-') ?> / <?= e($invoice['trailer_number']??'-') ?></p></div><div><small>Invoice Date</small><strong><?= e(date_display($invoice['invoice_date']??$invoice['created_at'])) ?></strong><small>Due Date</small><strong><?= e(date_display($invoice['due_date'])) ?></strong></div></div>
    <table class="invoice-table"><thead><tr><th>Description</th><th class="text-right">Amount</th></tr></thead><tbody><?php foreach($invoice['items'] as$item):?><tr><td><?= e($item['description']) ?></td><td class="text-right"><?= money($item['amount']) ?></td></tr><?php endforeach;?></tbody><tfoot><tr><td>Subtotal</td><td class="text-right"><?= money($invoice['subtotal']) ?></td></tr><?php if((float)$invoice['tax']):?><tr><td>Tax</td><td class="text-right"><?= money($invoice['tax']) ?></td></tr><?php endif;?><?php if((float)$invoice['discount']):?><tr><td>Discount</td><td class="text-right">- <?= money($invoice['discount']) ?></td></tr><?php endif;?><?php if((float)$invoice['deductions']):?><tr><td>Deductions</td><td class="text-right">- <?= money($invoice['deductions']) ?></td></tr><?php endif;?><tr><td><strong>Total</strong></td><td class="text-right"><strong><?= money($invoice['total']) ?></strong></td></tr><tr><td><strong>Balance Due</strong></td><td class="text-right"><strong><?= money($invoice['balance_due']) ?></strong></td></tr></tfoot></table>
    <div class="invoice-footer"><h4>Payment Instructions</h4><p><?= nl2br(e(setting('invoice_payment_instructions','')?:'')) ?></p><?php if(!empty($invoice['billing_notes'])):?><h4>Notes</h4><p><?= nl2br(e($invoice['billing_notes'])) ?></p><?php endif;?><small><?= e(setting('invoice_terms','Thank you for your business.')?:'') ?></small></div>
  </div>
  <div class="content-grid"><div class="panel"><div class="panel-header"><div><p class="eyebrow">Receivable</p><h2>Record Payment</h2></div></div><form method="post" action="<?= url('/invoices/payment') ?>" class="form-stack"><?= csrf_field() ?><input type="hidden" name="invoice_id" value="<?= e((string)$invoice['id']) ?>"><label><span>Amount</span><input type="number" step="0.01" max="<?= e((string)$invoice['balance_due']) ?>" name="amount" required></label><label><span>Payment Date</span><input type="date" name="payment_date" value="<?= date('Y-m-d') ?>"></label><label><span>Method</span><input name="method" placeholder="ACH, check, wire"></label><label><span>Reference</span><input name="reference"></label><label><span>Notes</span><textarea name="notes"></textarea></label><button class="btn btn-primary">Save Payment</button></form></div><div class="panel"><h2>Payment History</h2><div class="mini-list"><?php foreach($invoice['payments'] as$p):?><div class="mini-card"><strong><?= money($p['amount']) ?></strong><a class="btn btn-light btn-sm" href="<?= url('/invoices/payment-receipt?id='.$p['id']) ?>">Receipt PDF</a><span><?= e(date_display($p['payment_date'])) ?> · <?= e($p['method']??'-') ?> · <?= e($p['reference']??'') ?></span></div><?php endforeach;?><?php if(!$invoice['payments']):?><p class="empty">No payments recorded.</p><?php endif;?></div></div><div class="panel"><form method="post" action="<?= url('/invoices/status') ?>" class="form-stack"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string)$invoice['id']) ?>"><label><span>Invoice Status</span><select name="status"><?php foreach(Invoice::STATUSES as$s):?><option value="<?= e($s) ?>" <?= $invoice['status']===$s?'selected':'' ?>><?= e(readable_status($s)) ?></option><?php endforeach;?></select></label><button class="btn btn-light">Update Status</button></form></div></div>
</div>
