<?php
requirePermission($conn, 'customers', 'edit');
$id=(int)($_GET['id']??0);
$customer=$conn->query("SELECT * FROM customers WHERE id=$id AND account_id=$account_id LIMIT 1")->fetch_assoc();
if(!$customer){ echo '<div class="card" style="padding:30px">Customer not found</div>'; return; }
$countries=$conn->query("SELECT c.id,c.name,c.phone_code,ca.is_default FROM country_account ca INNER JOIN countries c ON c.id=ca.country_id WHERE ca.account_id=$account_id ORDER BY ca.is_default DESC,c.name ASC");
$sourcesRes = $conn->query("SELECT id,name FROM `source` WHERE account_id=$account_id AND datetime_deleted IS NULL AND status=1 ORDER BY name");
$sourcesList=[]; while($sr=$sourcesRes->fetch_assoc()) $sourcesList[]=$sr;
$countriesList=[]; while($r=$countries->fetch_assoc()) $countriesList[]=$r;
$contacts=$conn->query("SELECT * FROM customer_contacts WHERE customer_id=$id ORDER BY id");
?>
<div class="fade-in"><div class="toolbar" style="justify-content:space-between"><div><h3 style="margin:0">Edit Customer</h3><div class="text-xs text-muted"><?= htmlspecialchars($customer['customer_name']) ?></div></div><a href="index.php?page=customers" class="btn btn-secondary">← Back</a></div><div class="card" style="padding:20px;max-width:980px"><form id="customerPageForm"><input type="hidden" name="edit_id" value="<?= $id ?>"><div class="form-grid"><div class="form-group"><label class="form-label">Customer Name *</label><input type="text" name="customer_name" class="form-control" required value="<?= htmlspecialchars($customer['customer_name']) ?>"></div><div class="form-group"><label class="form-label">Business Name</label><input type="text" name="business_name" class="form-control" value="<?= htmlspecialchars($customer['business_name']??'') ?>"></div><div class="form-group"><label class="form-label">Phone</label><div class="d-flex" style="gap:8px"><select name="phone_code" id="phone_code" class="form-control" style="width:120px"><?php foreach($countriesList as $country): ?><option value="<?= htmlspecialchars($country['phone_code']) ?>" data-country-id="<?= $country['id'] ?>"><?= htmlspecialchars($country['phone_code']) ?></option><?php endforeach; ?></select><input type="text" name="phone" id="phone_number" class="form-control" value="<?= htmlspecialchars($customer['phone']??'') ?>"></div></div><div class="form-group"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= htmlspecialchars($customer['email']??'') ?>"></div><div class="form-group"><label class="form-label">Country</label><select name="country_id" id="customer_country_id" class="form-control"><option value="">Select</option><?php foreach($countriesList as $country): ?><option value="<?= $country['id'] ?>" <?= ((int)$customer['country_id']==(int)$country['id'])?'selected':'' ?>><?= htmlspecialchars($country['name']) ?></option><?php endforeach; ?></select></div><div class="form-group"><label class="form-label">Priority</label><select name="priority" class="form-control"><?php for($i=1;$i<=5;$i++): ?><option value="<?= $i ?>" <?= ((int)$customer['priority']==$i)?'selected':'' ?>><?= str_repeat('★',$i).' '.$i ?></option><?php endfor; ?></select></div><div class="form-group"><label class="form-label">Source</label><select name="source_id" class="form-control"><option value="">Select source</option><?php foreach($sourcesList as $src): ?><option value="<?= $src['id'] ?>" <?= (isset($customer) && (int)($customer['source_id'] ?? 0)===(int)$src['id'])?'selected':'' ?>><?= htmlspecialchars($src['name']) ?></option><?php endforeach; ?></select></div></div><div class="form-grid"><div class="form-group"><label class="form-label">Address</label><textarea name="address" class="form-control" rows="3"><?= htmlspecialchars($customer['address']??'') ?></textarea></div><div class="form-group"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="3"><?= htmlspecialchars($customer['notes']??'') ?></textarea></div></div><div class="hr"></div><div class="d-flex justify-between align-center mb-12"><label class="form-label mb-0">Contacts</label><button type="button" class="btn btn-secondary btn-sm" onclick="addContactRowPage()">+ Add Contact</button></div><div id="contactsContainer"></div><div class="d-flex gap-8" style="justify-content:flex-end;margin-top:18px"><a href="index.php?page=customers" class="btn btn-secondary">Cancel</a><button type="submit" class="btn btn-primary">Update Customer</button></div></form></div></div>
<script>
const countriesList=<?= json_encode($countriesList) ?>; let contactCount=0;
function addContactRowPage(c={}){contactCount++;const i=contactCount;const div=document.createElement('div');div.id='contact_row_'+i;div.style.cssText='background:var(--bg-base);border:1px solid var(--border);border-radius:8px;padding:12px;margin-bottom:8px';div.innerHTML=`<div class="form-grid"><div class="form-group mb-0"><label class="form-label">Name</label><input type="text" name="contacts[${i}][name]" class="form-control" value="${escapeHtml(c.name||'')}"></div><div class="form-group mb-0"><label class="form-label">Job Title</label><input type="text" name="contacts[${i}][job_title]" class="form-control" value="${escapeHtml(c.job_title||'')}"></div><div class="form-group mb-0"><label class="form-label">Phone</label><input type="text" name="contacts[${i}][phone]" class="form-control" value="${escapeHtml(c.phone||'')}"></div><div class="form-group mb-0"><label class="form-label">Email</label><input type="email" name="contacts[${i}][email]" class="form-control" value="${escapeHtml(c.email||'')}"></div></div><button type="button" class="btn btn-danger btn-sm" style="margin-top:8px" onclick="document.getElementById('contact_row_${i}').remove()">Remove</button>`;document.getElementById('contactsContainer').appendChild(div)}
<?php while($ct=$contacts->fetch_assoc()): ?>addContactRowPage(<?= json_encode($ct) ?>);<?php endwhile; ?>
document.getElementById('customerPageForm').addEventListener('submit',async function(e){e.preventDefault();const fd=new FormData(this);fd.append('action','edit');const res=await apiCall('handlers/customers.php',fd); if(res.success){showToast('Customer updated','success'); setTimeout(()=>location.href='index.php?page=customers',500)} else showToast(res.message||'Error','error');});
</script>
