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

<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($v['vendor_name']); ?></h5>
  <a href="<?= PROOT; ?>Vendors/view/<?= (int)$v['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to vendor</a>
</div>

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

<form method="POST" action="<?= PROOT; ?>Vendors/update/<?= (int)$v['c0d']; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-8"><label class="form-label">Vendor name <span class="text-danger">*</span></label>
        <input type="text" name="vendor_name" class="form-control" required value="<?= htmlspecialchars($f['vendor_name'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Category</label>
        <select name="vendor_category" class="form-select">
          <option value="">— select —</option>
          <?php foreach (['fuel_supplier'=>'Fuel supplier','workshop'=>'External workshop','spare_parts'=>'Spare parts','tyres'=>'Tyres','lubricants'=>'Lubricants','insurance'=>'Insurance','landlord'=>'Landlord','utilities'=>'Utilities','telecoms'=>'Telecoms','professional'=>'Professional services','other'=>'Other'] as $k=>$val): ?>
            <option value="<?= $k; ?>" <?= ($f['vendor_category'] ?? '')===$k?'selected':''; ?>><?= $val; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-4"><label class="form-label">TIN</label><input type="text" name="vendor_tin" class="form-control" value="<?= htmlspecialchars($f['vendor_tin'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">VRN</label><input type="text" name="vendor_vrn" class="form-control" value="<?= htmlspecialchars($f['vendor_vrn'] ?? ''); ?>"></div>

      <div class="col-md-4"><label class="form-label">Phone 1</label><input type="text" name="vendor_phone_1" class="form-control" value="<?= htmlspecialchars($f['vendor_phone_1'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Phone 2</label><input type="text" name="vendor_phone_2" class="form-control" value="<?= htmlspecialchars($f['vendor_phone_2'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Email</label><input type="email" name="vendor_email" class="form-control" value="<?= htmlspecialchars($f['vendor_email'] ?? ''); ?>"></div>
      <div class="col-md-8"><label class="form-label">Address</label><input type="text" name="vendor_address" class="form-control" value="<?= htmlspecialchars($f['vendor_address'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">PO Box</label><input type="text" name="vendor_po_box" class="form-control" value="<?= htmlspecialchars($f['vendor_po_box'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">City</label><input type="text" name="vendor_city" class="form-control" value="<?= htmlspecialchars($f['vendor_city'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Country</label><input type="text" name="vendor_country" class="form-control" value="<?= htmlspecialchars($f['vendor_country'] ?? ''); ?>"></div>
      <div class="col-md-8"><label class="form-label">Website</label><input type="text" name="vendor_website" class="form-control" value="<?= htmlspecialchars($f['vendor_website'] ?? ''); ?>"></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 vendor</button>
    <div class="d-flex gap-2">
      <a href="<?= PROOT; ?>Vendors/view/<?= (int)$v['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; ?>Vendors/delete/<?= (int)$v['c0d']; ?>" class="d-none"><?= csrf_field(); ?></form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
document.getElementById('btnDelete')?.addEventListener('click', function () {
  $.confirm({ title: 'Remove vendor?', content: 'Soft-deletes the vendor. Existing work orders and expenses keep their reference.', type: 'red',
    buttons: { remove: { text: 'Remove', btnClass: 'btn-danger', action: () => document.getElementById('deleteForm').submit() }, cancel: function () {} }
  });
});
</script>
<?php $this->end(); ?>
