<?php
global $db, $config;
$basePath = rtrim(($config['app']['base_path'] ?? ''), '/');

$id = (int)($_GET['id'] ?? 0);
$stmt = $db->prepare("SELECT * FROM customers WHERE id=?");
$stmt->execute([$id]);
$c = $stmt->fetch();
if (!$c) { echo '<div class="glass rounded-3xl p-5">Customer not found.</div>'; return; }
?>

<div class="max-w-3xl">
  <div class="flex items-center justify-between mb-4">
    <h1 class="text-2xl font-semibold text-ink-900">Edit Customer</h1>
    <a class="px-4 py-2 rounded-2xl bg-white border border-orange-200 text-orange-700 hover:bg-orange-50 transition"
       href="<?= h($basePath) ?>/customers" data-nav>Back</a>
  </div>

  <form class="glass rounded-3xl p-6 grid md:grid-cols-2 gap-4"
        action="<?= h($basePath) ?>/api/customers/update" method="post" data-ajax>

    <input type="hidden" name="id" value="<?= (int)$c['id'] ?>" />

    <div class="md:col-span-2">
      <label class="text-sm font-semibold text-slate-800">Customer Name</label>
      <input name="name" required value="<?= h($c['name']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div>
      <label class="text-sm font-semibold text-slate-800">Contact Person</label>
      <input name="contact_person" value="<?= h($c['contact_person']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div>
      <label class="text-sm font-semibold text-slate-800">Phone</label>
      <input name="phone" value="<?= h($c['phone']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div>
      <label class="text-sm font-semibold text-slate-800">Email</label>
      <input name="email" type="email" value="<?= h($c['email']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div>
      <label class="text-sm font-semibold text-slate-800">KRA PIN</label>
      <input name="kra_pin" value="<?= h($c['kra_pin']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div class="md:col-span-2">
      <label class="text-sm font-semibold text-slate-800">Address</label>
      <input name="address" value="<?= h($c['address']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 placeholder:text-slate-400 outline-none focus:ring-2 focus:ring-orange-300" />
    </div>

    <div class="md:col-span-2">
      <button class="btn-primary px-5 py-3 rounded-2xl transition">
        Update Customer
      </button>
    </div>
  </form>
</div>