<?php $this->extend("layouts/main"); $this->section("content"); $title = "Edit Job: ".$job["number"]; ?>
<div class="row justify-content-center"><div class="col-lg-9">
<div class="mb-3"><a href="/fieldly/public/index.php/jobs" 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/jobs/<?php echo $job['id'] ?>/edit">
<?php echo csrf_field() ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Job Details</h6></div><div class="card-body">
<div class="row g-3">
    <div class="col-12">
        <label class="form-label">Customer *</label>
        <input type="text" id="customer-search" class="form-control" placeholder="Search customer..." value="<?php if (!empty($prefill["customer_id"])): $c=db_connect()->table("customers")->where("id",$prefill["customer_id"])->get()->getRowArray(); echo esc($c["company_name"]?:$c["first_name"]." ".$c["last_name"]); endif; ?>"/>
        <input type="hidden" name="customer_id" id="customer_id" value="<?php echo $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-8"><label class="form-label">Job Title *</label><input type="text" name="title" class="form-control" required/></div>
    <div class="col-md-4"><label class="form-label">Priority</label><select name="priority" class="form-select"><option value="normal">Normal</option><option value="low">Low</option><option value="high">High</option><option value="urgent">Urgent</option></select></div>
    <div class="col-md-6"><label class="form-label">Job Type</label><select name="type_id" class="form-select"><option value="">— None —</option><?php foreach ($jobTypes as $t): ?><option value="<?php echo $t["id"] ?>"><?php echo esc($t["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-6"><label class="form-label">Status *</label><select name="status_id" class="form-select" required><?php foreach ($jobStatuses as $s): ?><option value="<?php echo $s["id"] ?>" <?php echo $s["is_default"]?"selected":"" ?>><?php echo esc($s["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-6"><label class="form-label">Pricing Type</label><select name="pricing_type_id" class="form-select"><option value="">— None —</option><?php foreach ($pricingTypes as $p): ?><option value="<?php echo $p["id"] ?>"><?php echo esc($p["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-6"><label class="form-label">Rate</label><div class="input-group"><span class="input-group-text">£</span><input type="number" name="pricing_rate" class="form-control" step="0.01" min="0" placeholder="0.00"/></div></div>
    <div class="col-md-6"><label class="form-label">Scheduled Date</label><input type="date" name="scheduled_date" class="form-control"/></div>
    <div class="col-md-6"><label class="form-label">Scheduled Time</label><input type="time" name="scheduled_time" class="form-control"/></div>
    <div class="col-md-6"><label class="form-label">Estimated Duration</label><div class="input-group"><input type="number" name="estimated_mins" class="form-control" min="0" placeholder="90"/><span class="input-group-text">mins</span></div></div>
    <div class="col-md-6"><label class="form-label">Linked Quote</label><select name="source_quote_id" class="form-select"><option value="">— None —</option><?php foreach ($quotes as $q): ?><option value="<?php echo $q["id"] ?>"><?php echo esc($q["number"]) ?> — <?php echo esc($q["title"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-12"><label class="form-label">Description</label><textarea name="description" class="form-control" rows="3"></textarea></div>
    <div class="col-12"><label class="form-label">Internal Notes <span class="text-muted small">(not visible to customer)</span></label><textarea name="internal_notes" class="form-control" rows="2"></textarea></div>
</div>
</div></div>

<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Assign Engineers</h6></div><div class="card-body">
<div class="row g-2">
<?php foreach ($users as $u): ?>
<div class="col-md-4 col-6">
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="assigned_to[]" value="<?php echo $u["id"] ?>" id="user_<?php echo $u["id"] ?>"/>
        <label class="form-check-label" for="user_<?php echo $u["id"] ?>">
            <span class="badge me-1" style="background:<?php echo esc($u["color"]) ?>"><?php echo esc($u["initials"]) ?></span>
            <?php echo esc($u["first_name"]." ".$u["last_name"]) ?>
        </label>
    </div>
</div>
<?php endforeach; ?>
</div>
</div></div>

<?php if ($templates): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Checklist Template</h6></div><div class="card-body">
<select name="checklist_template_id" class="form-select"><option value="">— No checklist —</option><?php foreach ($templates as $t): ?><option value="<?php echo $t["id"] ?>"><?php echo esc($t["name"]) ?></option><?php endforeach; ?></select>
<div class="form-text">A checklist will be automatically created from the selected template.</div>
</div></div>
<?php endif; ?>

<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/jobs" class="btn btn-outline-secondary">Cancel</a>
</div>
</form>
</div></div>
<?php $this->endSection(); ?>