<?php $this->extend("layouts/main"); $this->section("content");
$title = $inv["number"];
$sym = ["GBP"=>"£","EUR"=>"€","USD"=>"$"][$currency] ?? "£";
$statusColors = ["paid"=>"success","overdue"=>"danger","draft"=>"secondary","sent"=>"primary","part_paid"=>"warning","void"=>"dark"];
?>
<div class="d-flex justify-content-between align-items-start mb-3">
    <a href="/fieldly/public/index.php/invoices" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i> Invoices</a>
    <div class="d-flex gap-2 flex-wrap">
        <?php if (in_array("edit_invoices", $permissions) && $inv["status"]!=="void"): ?>
        <a href="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil me-1"></i> Edit</a>
        <?php if (in_array($inv["status"],["draft"])): ?>
        <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/send"><?php echo csrf_field() ?><button class="btn btn-sm btn-primary"><i class="bi bi-send me-1"></i> Mark Sent</button></form>
        <?php endif; ?>
        <?php if (in_array($inv["status"],["sent","part_paid","overdue"]) && in_array("record_payments", $permissions)): ?>
        <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#payModal"><i class="bi bi-credit-card me-1"></i> Record Payment</button>
        <?php endif; ?>
        <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/void" data-confirm="Void this invoice? This cannot be undone."><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-warning">Void</button></form>
        <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/delete" data-confirm="Move to recycle bin?" class="d-inline"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button></form>
        <?php endif; ?>
    </div>
</div>

<div class="card mb-3">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-start">
            <div>
                <h4 class="fw-bold mb-1"><?php echo esc($inv["number"]) ?></h4>
                <?php if ($inv["title"]): ?><p class="text-muted mb-0"><?php echo esc($inv["title"]) ?></p><?php endif; ?>
                <div class="mt-2">
                    <span class="badge bg-<?php echo $statusColors[$inv["status"]]??"secondary" ?> me-2"><?php echo ucfirst(str_replace("_"," ",$inv["status"])) ?></span>
                    <?php if ($inv["job"]): ?><a href="/fieldly/public/index.php/jobs/<?php echo $inv["job"]["id"] ?>" class="badge bg-light text-dark text-decoration-none">Job: <?php echo esc($inv["job"]["number"]) ?></a><?php endif; ?>
                </div>
            </div>
            <div class="text-end">
                <div class="display-6 fw-bold"><?php echo $sym.number_format((float)$inv["total"],2) ?></div>
                <?php if ((float)$inv["balance_due"]>0): ?><div class="text-danger fw-semibold">Balance: <?php echo $sym.number_format((float)$inv["balance_due"],2) ?></div><?php endif; ?>
            </div>
        </div>
        <hr/>
        <div class="row g-2">
            <div class="col-md-4"><div class="detail-row"><span class="detail-label">Customer</span><span class="detail-value"><a href="/fieldly/public/index.php/customers/<?php echo $inv["customer"]["id"] ?>"><?php echo esc($inv["customer"]["company_name"]?:$inv["customer"]["first_name"]." ".$inv["customer"]["last_name"]) ?></a></span></div></div>
            <?php if ($inv["due_date"]): ?><div class="col-md-4"><div class="detail-row"><span class="detail-label">Due Date</span><span class="detail-value"><?php echo date("d M Y",strtotime($inv["due_date"])) ?></span></div></div><?php endif; ?>
            <?php if ($inv["amount_paid"]>0): ?><div class="col-md-4"><div class="detail-row"><span class="detail-label">Amount Paid</span><span class="detail-value text-success fw-bold"><?php echo $sym.number_format((float)$inv["amount_paid"],2) ?></span></div></div><?php endif; ?>
        </div>
    </div>
</div>

<!-- Line Items -->
<div class="card mb-3">
    <div class="card-header"><h6 class="mb-0">Line Items</h6></div>
    <div class="card-body p-0">
        <table class="table mb-0">
            <thead><tr><th>Description</th><th>Qty</th><th>Unit Price</th><th>Discount</th><th>Tax</th><th class="text-end">Total</th></tr></thead>
            <tbody>
            <?php foreach ($inv["items"] as $item): ?>
            <tr>
                <td><?php echo esc($item["description"]) ?></td>
                <td><?php echo $item["qty"] ?></td>
                <td><?php echo $sym.number_format((float)$item["unit_price"],2) ?></td>
                <td><?php echo $item["discount"]>0?$item["discount"]."%":"—" ?></td>
                <td><?php echo $item["tax_rate"] ?>%</td>
                <td class="text-end fw-bold"><?php echo $sym.number_format((float)$item["line_total"],2) ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <div class="line-items-total">
            <div class="d-flex justify-content-end gap-5">
                <div class="text-end"><div class="text-muted small">Subtotal</div><div class="fw-bold"><?php echo $sym.number_format((float)$inv["subtotal"],2) ?></div></div>
                <div class="text-end"><div class="text-muted small">Tax</div><div class="fw-bold"><?php echo $sym.number_format((float)$inv["tax_total"],2) ?></div></div>
                <div class="text-end"><div class="text-muted small">Total</div><div class="fw-bold fs-5"><?php echo $sym.number_format((float)$inv["total"],2) ?></div></div>
            </div>
        </div>
    </div>
</div>

<?php if (!empty($inv["payments"])): ?>
<div class="card mb-3">
    <div class="card-header"><h6 class="mb-0">Payments</h6></div>
    <div class="card-body p-0">
        <table class="table mb-0">
            <thead><tr><th>Date</th><th>Method</th><th>Reference</th><th class="text-end">Amount</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($inv["payments"] as $pay): ?>
            <tr>
                <td style="font-size:13px"><?php echo date("d M Y",strtotime($pay["paid_at"])) ?></td>
                <td><?php echo ucfirst(str_replace("_"," ",$pay["method"])) ?></td>
                <td style="font-size:13px"><?php echo esc($pay["reference"]) ?></td>
                <td class="text-end fw-bold text-success"><?php echo $sym.number_format((float)$pay["amount"],2) ?></td>
                <td>
                    <?php if (in_array("edit_invoices", $permissions)): ?>
                    <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/payment/<?php echo $pay["id"] ?>/delete" data-confirm="Remove this payment?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if ($inv["notes"] || $inv["payment_terms"]): ?>
<div class="card mb-3"><div class="card-body">
    <?php if ($inv["payment_terms"]): ?><div class="mb-2"><strong class="small">Payment Terms:</strong> <span style="font-size:14px"><?php echo esc($inv["payment_terms"]) ?></span></div><?php endif; ?>
    <?php if ($inv["notes"]): ?><div><strong class="small">Notes:</strong> <span style="font-size:14px"><?php echo nl2br(esc($inv["notes"])) ?></span></div><?php endif; ?>
</div></div>
<?php endif; ?>

<!-- Record Payment Modal -->
<div class="modal fade" id="payModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/payment">
                <?php echo csrf_field() ?>
                <div class="modal-header"><h5 class="modal-title">Record Payment</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6"><label class="form-label">Amount *</label><div class="input-group"><span class="input-group-text"><?php echo $sym ?></span><input type="number" name="amount" class="form-control" value="<?php echo number_format((float)$inv["balance_due"],2,".","") ?>" step="0.01" min="0.01" required/></div></div>
                        <div class="col-md-6"><label class="form-label">Date *</label><input type="date" name="paid_at" class="form-control" value="<?php echo date("Y-m-d") ?>" required/></div>
                        <div class="col-md-6"><label class="form-label">Method *</label><select name="method" class="form-select" required><option value="bank_transfer">Bank Transfer</option><option value="card">Card</option><option value="cash">Cash</option><option value="cheque">Cheque</option><option value="stripe">Stripe</option><option value="bacs">BACS</option><option value="other">Other</option></select></div>
                        <div class="col-md-6"><label class="form-label">Reference</label><input type="text" name="reference" class="form-control" placeholder="Payment ref..."/></div>
                        <div class="col-12"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="2"></textarea></div>
                    </div>
                </div>
                <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-success">Record Payment</button></div>
            </form>
        </div>
    </div>
</div>
<?php $this->endSection(); ?>