<?php $this->extend('layouts/main'); $this->section('content'); $title = 'Edit Customer'; ?>
<div class="row justify-content-center"><div class="col-lg-8">
<div class="mb-3"><a href="/fieldly/public/index.php/customers/<?= $customer['id'] ?>" 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/customers/<?= $customer['id'] ?>/edit">
<?= csrf_field() ?>
<div class="card mb-3">
    <div class="card-header"><h6 class="mb-0">Edit Customer</h6></div>
    <div class="card-body">
        <div class="row g-3">
            <div class="col-md-6"><label class="form-label">Type</label><select name="type" class="form-select" id="customer-type"><option value="individual" <?= $customer['type']==='individual'?'selected':'' ?>>Individual</option><option value="business" <?= $customer['type']==='business'?'selected':'' ?>>Business</option></select></div>
            <div class="col-md-6"><label class="form-label">Status</label><select name="status" class="form-select"><option value="active" <?= $customer['status']==='active'?'selected':'' ?>>Active</option><option value="inactive" <?= $customer['status']==='inactive'?'selected':'' ?>>Inactive</option></select></div>
            <div class="col-md-6"><label class="form-label">First Name *</label><input type="text" name="first_name" class="form-control" value="<?= esc($customer['first_name']) ?>" required/></div>
            <div class="col-md-6"><label class="form-label">Last Name *</label><input type="text" name="last_name" class="form-control" value="<?= esc($customer['last_name']) ?>" required/></div>
            <div class="col-md-6 business-only" <?= $customer['type']!=='business'?'style="display:none"':'' ?>><label class="form-label">Company Name</label><input type="text" name="company_name" class="form-control" value="<?= esc($customer['company_name']) ?>"/></div>
            <div class="col-md-6 business-only" <?= $customer['type']!=='business'?'style="display:none"':'' ?>><label class="form-label">VAT Number</label><input type="text" name="vat_number" class="form-control" value="<?= esc($customer['vat_number']) ?>"/></div>
            <div class="col-md-6"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= esc($customer['email']) ?>" style="font-size:16px"/></div>
        </div>
    </div>
</div>
<div class="card mb-3">
    <div class="card-header"><h6 class="mb-0">Phone Numbers</h6></div>
    <div class="card-body">
        <div id="phones-container">
            <?php foreach ($customer['phones'] as $i=>$p): ?>
            <div class="phone-row d-flex gap-2 mb-2">
                <select class="form-select form-select-sm" name="phones[<?= $i ?>][type]" style="width:110px;flex-shrink:0">
                    <?php foreach (['Mobile','Work','Home','Fax','Other'] as $t): ?><option <?= $p['type']===$t?'selected':'' ?>><?= $t ?></option><?php endforeach; ?>
                </select>
                <input type="tel" class="form-control form-control-sm" name="phones[<?= $i ?>][number]" value="<?= esc($p['number']) ?>" style="font-size:16px"/>
                <button type="button" class="btn btn-sm btn-outline-danger remove-phone"><i class="bi bi-trash3"></i></button>
            </div>
            <?php endforeach; ?>
        </div>
        <button type="button" class="btn btn-sm btn-outline-secondary mt-1" id="add-phone"><i class="bi bi-plus me-1"></i> Add Phone</button>
    </div>
</div>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Notes</h6></div><div class="card-body"><textarea name="notes" class="form-control" rows="3"><?= esc($customer['notes']) ?></textarea></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/customers/<?= $customer['id'] ?>" class="btn btn-outline-secondary">Cancel</a></div>
</form>
</div></div>
<script>document.getElementById('customer-type').addEventListener('change',function(){document.querySelectorAll('.business-only').forEach(el=>el.style.display=this.value==='business'?'':'none');});</script>
<?php $this->endSection(); ?>
