<?php $this->extend("layouts/main"); $this->section("content");
$title = $q["number"];
$sym = ["GBP"=>"£","EUR"=>"€","USD"=>"$"][$currency] ?? "£";
?>
<div class="d-flex justify-content-between align-items-start mb-3">
    <a href="/fieldly/public/index.php/quotes" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i> Quotes</a>
    <div class="d-flex gap-2 flex-wrap">
        <?php if (in_array("edit_quotes", $permissions)): ?>
        <a href="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil me-1"></i> Edit</a>
        <?php if ($q["status"]==="draft"): ?><form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["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 ($q["status"]==="sent"): ?>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/accept"><?php echo csrf_field() ?><button class="btn btn-sm btn-success">Accept</button></form>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/decline"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger">Decline</button></form>
        <?php endif; ?>
        <?php if (in_array($q["status"],["sent","accepted"]) && in_array("edit_jobs", $permissions)): ?>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/convert-job"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-success">Convert to Job</button></form>
        <?php endif; ?>
        <?php if (in_array($q["status"],["sent","accepted"]) && in_array("edit_invoices", $permissions)): ?>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/convert-invoice"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-success">Convert to Invoice</button></form>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
<div class="card mb-3"><div class="card-body">
    <div class="d-flex justify-content-between">
        <div><h4 class="fw-bold mb-1"><?php echo esc($q["number"]) ?></h4><?php if ($q["title"]): ?><p class="text-muted mb-1"><?php echo esc($q["title"]) ?></p><?php endif; ?>
        <span class="badge bg-<?php echo ["draft"=>"secondary","sent"=>"primary","accepted"=>"success","declined"=>"danger","converted"=>"info"][$q["status"]]??"secondary" ?>"><?php echo ucfirst($q["status"]) ?></span>
        </div>
        <div class="text-end"><div class="display-6 fw-bold"><?php echo $sym.number_format((float)$q["total"],2) ?></div></div>
    </div>
    <hr/>
    <div class="detail-row"><span class="detail-label">Customer</span><span class="detail-value"><a href="/fieldly/public/index.php/customers/<?php echo $q["customer"]["id"] ?>"><?php echo esc($q["customer"]["company_name"]?:$q["customer"]["first_name"]." ".$q["customer"]["last_name"]) ?></a></span></div>
    <?php if ($q["valid_until"]): ?><div class="detail-row"><span class="detail-label">Valid Until</span><span class="detail-value"><?php echo date("d M Y",strtotime($q["valid_until"])) ?></span></div><?php endif; ?>
</div></div>
<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 ($q["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)$q["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)$q["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)$q["total"],2) ?></div></div>
</div></div>
</div></div>
<?php if ($q["notes"]||$q["terms"]): ?><div class="card mb-3"><div class="card-body"><?php if ($q["terms"]): ?><div class="mb-2"><strong class="small">Terms:</strong> <span style="font-size:14px"><?php echo nl2br(esc($q["terms"])) ?></span></div><?php endif; ?><?php if ($q["notes"]): ?><div><strong class="small">Notes:</strong> <span style="font-size:14px"><?php echo nl2br(esc($q["notes"])) ?></span></div><?php endif; ?></div></div><?php endif; ?>
<?php $this->endSection(); ?>