<?php $this->extend("layouts/main"); $this->section("content"); $title = "Edit Quote: ".$q["number"];
$sym = ["GBP"=>"£","EUR"=>"€","USD"=>"$"][$currency] ?? "£";
?>
<script>window.currencySymbol="<?php echo $sym ?>";window.fieldlyTaxRates=<?php echo json_encode(array_map(fn($t)=>["rate"=>$t["rate"],"name"=>$t["name"]],$taxRates)) ?>;</script>
<div class="row justify-content-center"><div class="col-lg-10">
<div class="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> Back</a></div>
<form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q['id'] ?>/edit"><?php echo csrf_field() ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Quote Details</h6></div><div class="card-body"><div class="row g-3">
    <div class="col-md-6"><label class="form-label">Customer *</label><input type="text" id="customer-search" class="form-control" placeholder="Search customer..."/><input type="hidden" name="customer_id" id="customer_id" value="<?php echo esc($prefill["customer_id"]??"") ?>" required/><div id="customer-results" class="border rounded bg-white shadow-sm" style="display:none;position:absolute;z-index:999;width:400px"></div></div>
    <div class="col-md-6"><label class="form-label">Quote Number</label><input type="text" class="form-control" value="<?php echo esc($nextNum) ?>" readonly/></div>
    <div class="col-md-8"><label class="form-label">Title</label><input type="text" name="title" class="form-control"/></div>
    <div class="col-md-4"><label class="form-label">Valid Until</label><input type="date" name="valid_until" class="form-control" value="<?php echo date("Y-m-d",strtotime("+30 days")) ?>"/></div>
</div></div></div>
<div class="card mb-3"><div class="card-header d-flex justify-content-between align-items-center"><h6 class="mb-0">Line Items</h6></div>
<div class="card-body p-0">
<table class="table mb-0 line-items-table"><thead><tr><th style="width:40%">Description</th><th>Qty</th><th>Unit Price</th><th>Discount</th><th>Tax Rate</th><th>Total</th><th></th></tr></thead>
<tbody id="line-items-container"><tr class="line-item-row"><td><input type="text" class="form-control form-control-sm item-desc" name="items[0][description]" placeholder="Description" required/></td><td style="width:70px"><input type="number" class="form-control form-control-sm item-qty" name="items[0][qty]" value="1" min="0" step="0.5"/></td><td style="width:120px"><div class="input-group input-group-sm"><span class="input-group-text"><?php echo $sym ?></span><input type="number" class="form-control item-price" name="items[0][unit_price]" min="0" step="0.01" placeholder="0.00"/></div></td><td style="width:80px"><div class="input-group input-group-sm"><input type="number" class="form-control item-discount" name="items[0][discount]" value="0" min="0" max="100"/><span class="input-group-text">%</span></div></td><td style="width:130px"><select class="form-select form-select-sm item-tax" name="items[0][tax_rate]"><?php foreach ($taxRates as $tr): ?><option value="<?php echo $tr["rate"] ?>" <?php echo $tr["rate"]==20?"selected":"" ?>><?php echo esc($tr["name"]) ?> (<?php echo $tr["rate"] ?>%)</option><?php endforeach; ?></select></td><td style="width:100px" class="text-end fw-bold item-total"><?php echo $sym ?>0.00</td><td style="width:40px"><button type="button" class="btn btn-sm btn-outline-danger remove-line-item"><i class="bi bi-trash3"></i></button></td></tr></tbody>
</table>
<div class="p-3 border-top"><button type="button" class="btn btn-sm btn-outline-secondary" id="add-line-item"><i class="bi bi-plus me-1"></i> Add Line</button></div>
<div class="line-items-total"><input type="hidden" name="subtotal" id="subtotal-hidden"/><input type="hidden" name="tax_total" id="tax-hidden"/><input type="hidden" name="total" id="total-hidden"/>
<div class="d-flex justify-content-end gap-5">
    <div class="text-end"><div class="text-muted small">Subtotal</div><div class="fw-bold" id="subtotal-display"><?php echo $sym ?>0.00</div></div>
    <div class="text-end"><div class="text-muted small">Tax</div><div class="fw-bold" id="tax-display"><?php echo $sym ?>0.00</div></div>
    <div class="text-end"><div class="text-muted small">Total</div><div class="fw-bold fs-5" id="total-display"><?php echo $sym ?>0.00</div></div>
</div></div>
</div></div>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Notes & Terms</h6></div><div class="card-body"><div class="row g-3"><div class="col-md-6"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="3"></textarea></div><div class="col-md-6"><label class="form-label">Terms</label><textarea name="terms" class="form-control" rows="3"><?php echo esc($settings["quote_defaults"]["terms"]??"") ?></textarea></div></div></div></div>
<div class="d-flex gap-2"><button type="submit" class="btn btn-primary"><i class="bi bi-check-lg me-1"></i> Save Changes</button><a href="/fieldly/public/index.php/quotes" class="btn btn-outline-secondary">Cancel</a></div>
</form></div></div>
<?php $this->endSection(); ?>