<?php $this->start('body'); ?>
<?php $f = $this->data['form']; $c = $this->data['c']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-pen me-2 text-muted"></i>Edit · <?= htmlspecialchars($c['fname']); ?></h5>
  <a href="<?= PROOT; ?>Customers/view/<?= (int)$c['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to customer</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="POST" action="<?= PROOT; ?>Customers/update/<?= (int)$c['c0d']; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">

    <h6 class="text-uppercase text-muted small mb-3">Identity</h6>
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Company name <span class="text-danger">*</span></label>
        <input type="text" name="fname" class="form-control" required value="<?= htmlspecialchars($f['fname'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Trading / brand name</label>
        <input type="text" name="lname" class="form-control" value="<?= htmlspecialchars($f['lname'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Customer code</label>
        <input type="text" name="customer_code" class="form-control text-uppercase" value="<?= htmlspecialchars($f['customer_code'] ?? ''); ?>"></div>

      <div class="col-md-3"><label class="form-label">Tier</label>
        <select name="customer_tier" class="form-select">
          <?php foreach (['CORPORATE'=>'Corporate','SME'=>'SME','RETAIL'=>'Retail','GOVERNMENT'=>'Government','NGO'=>'NGO'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['customer_tier'] ?? 'CORPORATE')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Sector</label>
        <select name="customer_sector" class="form-select">
          <option value="">— select —</option>
          <?php foreach (['CONSTRUCTION'=>'Construction','MANUFACTURING'=>'Manufacturing','MINING'=>'Mining','RETAIL'=>'Retail','GOVERNMENT'=>'Government','LOGISTICS'=>'Logistics','FMCG'=>'FMCG','AGRICULTURE'=>'Agriculture','OTHER'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['customer_sector'] ?? '')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Status</label>
        <select name="status" class="form-select">
          <?php foreach (['ACTIVE'=>'Active','INACTIVE'=>'Inactive','ON_HOLD'=>'On hold'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['status'] ?? 'ACTIVE')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Tax &amp; Compliance</h6>
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">TIN</label><input type="text" name="customer_tin" class="form-control" value="<?= htmlspecialchars($f['customer_tin'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">VRN</label><input type="text" name="customer_vrn" class="form-control" value="<?= htmlspecialchars($f['customer_vrn'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Address &amp; Contact</h6>
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Phone 1</label><input type="text" name="phone_1" class="form-control" value="<?= htmlspecialchars($f['phone_1'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Phone 2</label><input type="text" name="phone_2" class="form-control" value="<?= htmlspecialchars($f['phone_2'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= htmlspecialchars($f['email'] ?? ''); ?>"></div>
      <div class="col-md-6"><label class="form-label">Physical address</label><input type="text" name="physical_address" class="form-control" value="<?= htmlspecialchars($f['physical_address'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">PO Box</label><input type="text" name="po_box" class="form-control" value="<?= htmlspecialchars($f['po_box'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">City</label><input type="text" name="city" class="form-control" value="<?= htmlspecialchars($f['city'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Country</label><input type="text" name="country" class="form-control" value="<?= htmlspecialchars($f['country'] ?? 'TANZANIA'); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Primary Contact Person</h6>
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Full name</label>       <input type="text"  name="customer_poc"       class="form-control" value="<?= htmlspecialchars($f['customer_poc']       ?? ''); ?>"></div>
      <div class="col-md-3"><label class="form-label">Role / Title</label>    <input type="text"  name="customer_poc_role"  class="form-control" value="<?= htmlspecialchars($f['customer_poc_role']  ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Phone</label>           <input type="text"  name="customer_poc_phone" class="form-control" value="<?= htmlspecialchars($f['customer_poc_phone'] ?? ''); ?>"></div>
      <div class="col-md-3"><label class="form-label">Email</label>           <input type="email" name="customer_poc_email" class="form-control" value="<?= htmlspecialchars($f['customer_poc_email'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Billing &amp; Credit</h6>
    <div class="row g-3">
      <div class="col-md-3"><label class="form-label">Currency</label>
        <select name="currency" class="form-select">
          <?php foreach (['TZS','USD','EUR','KES','UGX'] as $c2): ?>
            <option value="<?= $c2; ?>" <?= ($f['currency'] ?? 'TZS')===$c2?'selected':''; ?>><?= $c2; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Credit limit</label>
        <input type="text" name="credit_limit" class="form-control js-money" value="<?= htmlspecialchars((string)($f['credit_limit'] ?? '0')); ?>"></div>
      <div class="col-md-2"><label class="form-label">Payment terms (days)</label>
        <input type="text" name="payment_terms_days" class="form-control js-int" value="<?= htmlspecialchars((string)($f['payment_terms_days'] ?? '0')); ?>"></div>
      <div class="col-md-4"><label class="form-label">Billing email</label>
        <input type="email" name="billing_email" class="form-control" value="<?= htmlspecialchars($f['billing_email'] ?? ''); ?>"></div>

      <div class="col-md-3"><label class="form-label">Opening balance (AR)</label>
        <input type="text" name="opening_balance" class="form-control js-money" value="<?= htmlspecialchars((string)($f['opening_balance'] ?? '0')); ?>"></div>
      <div class="col-md-9"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control"><?= htmlspecialchars($f['notes'] ?? ''); ?></textarea></div>
    </div>

  </div>
  <div class="card-footer bg-white d-flex justify-content-between py-3">
    <button type="button" class="btn btn-outline-danger" id="btnDelete"><i class="fas fa-trash me-1"></i>Remove customer</button>
    <div class="d-flex gap-2">
      <a href="<?= PROOT; ?>Customers/view/<?= (int)$c['c0d']; ?>" class="btn btn-outline-secondary">Cancel</a>
      <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save changes</button>
    </div>
  </div>
</form>

<form id="deleteForm" method="POST" action="<?= PROOT; ?>Customers/delete/<?= (int)$c['c0d']; ?>" class="d-none">
  <?= csrf_field(); ?>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
document.querySelectorAll('.js-money').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 2 }));
document.querySelectorAll('.js-int').forEach(el   => new Cleave(el, { numericOnly: true, blocks: [4] }));

document.getElementById('btnDelete')?.addEventListener('click', function () {
  $.confirm({
    title: 'Remove customer?',
    content: 'Soft-deletes the customer. Historic trips, invoices and AR balances are preserved but the customer no longer appears in the register.',
    type: 'red',
    buttons: {
      remove: { text: 'Remove', btnClass: 'btn-danger', action: () => document.getElementById('deleteForm').submit() },
      cancel: function () {}
    }
  });
});
</script>
<?php $this->end(); ?>
