<?php $this->start('body'); ?>
<?php $f = $this->data['form']; $editId = (int)($this->data['edit_id'] ?? 0);
$action = $editId > 0 ? (PROOT . 'Suppliers/update/' . $editId) : (PROOT . 'Suppliers/save'); ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-industry me-2 text-muted"></i><?= $editId ? 'Edit Supplier' : 'Add Supplier'; ?></h5>
  <a href="<?= PROOT; ?>Suppliers" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

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

<form method="POST" action="<?= $action; ?>" class="card border-0 shadow-sm">
  <?= 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-5"><label class="form-label">Supplier 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-3"><label class="form-label">Category</label><input type="text" name="vendor_category" class="form-control" value="<?= htmlspecialchars($f['vendor_category'] ?? ''); ?>" placeholder="e.g. Fuel · Spare parts"></div>
      <div class="col-md-2"><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-2"><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-3"><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-3"><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-2"><label class="form-label">Status</label>
        <select name="status" class="form-select">
          <option value="ACTIVE"   <?= ($f['status'] ?? 'ACTIVE') === 'ACTIVE'   ? 'selected' : ''; ?>>Active</option>
          <option value="INACTIVE" <?= ($f['status'] ?? '')       === 'INACTIVE' ? 'selected' : ''; ?>>Inactive</option>
        </select>
      </div>

      <div class="col-md-6"><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">City</label><input type="text" name="vendor_city" class="form-control" value="<?= htmlspecialchars($f['vendor_city'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Country</label><input type="text" name="vendor_country" class="form-control" value="<?= htmlspecialchars($f['vendor_country'] ?? 'TANZANIA'); ?>"></div>
      <div class="col-md-2"><label class="form-label">P.O. Box</label><input type="text" name="vendor_po_box" class="form-control" value="<?= htmlspecialchars($f['vendor_po_box'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Contact person</h6>
    <div class="row g-3">
      <div class="col-md-5"><label class="form-label">Point of contact</label><input type="text" name="vendor_poc" class="form-control" value="<?= htmlspecialchars($f['vendor_poc'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">POC phone</label><input type="text" name="vendor_poc_phone" class="form-control" value="<?= htmlspecialchars($f['vendor_poc_phone'] ?? ''); ?>"></div>
      <div class="col-md-3"><label class="form-label">POC email</label><input type="email" name="vendor_poc_email" class="form-control" value="<?= htmlspecialchars($f['vendor_poc_email'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Banking</h6>
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Bank name</label><input type="text" name="bank_name" class="form-control" value="<?= htmlspecialchars($f['bank_name'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Account number</label><input type="text" name="bank_account_no" class="form-control" value="<?= htmlspecialchars($f['bank_account_no'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">SWIFT</label><input type="text" name="bank_swift" class="form-control" value="<?= htmlspecialchars($f['bank_swift'] ?? ''); ?>"></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>Suppliers" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i><?= $editId ? 'Save changes' : 'Add supplier'; ?></button>
  </div>
</form>
<?php $this->end(); ?>
