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

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

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

<form method="POST" action="<?= PROOT; ?>Expenses/update/<?= (int)$e['c0d']; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <h6 class="text-uppercase text-muted small mb-3">Expense Type &amp; Amount</h6>
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Expense type</label>
        <select name="expense_type_id" class="form-select">
          <?php foreach ($this->data['types'] as $et): ?>
            <option value="<?= (int)$et['c0d']; ?>" <?= ($f['expense_type_id'] ?? '')==$et['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($et['display_name']); ?><?php if ($et['gl_code']): ?> · <?= htmlspecialchars($et['gl_code']); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label">Date</label><input type="date" name="expense_date" class="form-control" value="<?= htmlspecialchars($f['expense_date'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Currency</label>
        <select name="currency" class="form-select">
          <?php foreach (['TZS','USD','EUR','KES','UGX'] as $c): ?>
            <option value="<?= $c; ?>" <?= ($f['currency'] ?? 'TZS')===$c?'selected':''; ?>><?= $c; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label">Gross amount</label>
        <input type="text" name="gross_amount" class="form-control js-money" value="<?= htmlspecialchars((string)($f['gross_amount'] ?? '')); ?>"></div>

      <div class="col-md-12"><label class="form-label">Description</label>
        <input type="text" name="description" class="form-control" value="<?= htmlspecialchars($f['description'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Cost Attribution</h6>
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Truck</label>
        <select name="truck_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['trucks'] as $t): ?>
            <option value="<?= (int)$t['c0d']; ?>" <?= ($f['truck_id'] ?? '')==$t['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($t['reg_no']); ?><?php if ($t['make']): ?> · <?= htmlspecialchars(trim($t['make'] . ' ' . $t['model'])); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-4"><label class="form-label">Driver</label>
        <select name="driver_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['drivers'] as $d): ?>
            <option value="<?= (int)$d['c0d']; ?>" <?= ($f['driver_id'] ?? '')==$d['c0d']?'selected':''; ?>><?= htmlspecialchars($d['full_name']); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-4"><label class="form-label">Trip</label>
        <select name="trip_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['trips'] as $t): ?>
            <option value="<?= (int)$t['c0d']; ?>" <?= ($f['trip_id'] ?? '')==$t['c0d']?'selected':''; ?>><?= htmlspecialchars($t['trip_no']); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-6"><label class="form-label">Vendor</label>
        <select name="vendor_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['vendors'] as $v): ?>
            <option value="<?= (int)$v['c0d']; ?>" <?= ($f['vendor_id'] ?? '')==$v['c0d']?'selected':''; ?>><?= htmlspecialchars($v['name']); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-6"><label class="form-label">Customer</label>
        <select name="customer_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['customers'] as $c): ?>
            <option value="<?= (int)$c['c0d']; ?>" <?= ($f['customer_id'] ?? '')==$c['c0d']?'selected':''; ?>><?= htmlspecialchars($c['fname']); ?></option>
          <?php endforeach; ?>
        </select></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Payment &amp; Receipt</h6>
    <div class="row g-3">
      <div class="col-md-3"><label class="form-label">Payment method</label>
        <select name="payment_method" class="form-select">
          <option value="">— select on payment —</option>
          <?php foreach (['cash'=>'Cash','petty_cash'=>'Petty Cash','mobile_money'=>'Mobile Money','bank_transfer'=>'Bank Transfer','cheque'=>'Cheque','fuel_card'=>'Fuel Card'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['payment_method'] ?? '')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-4"><label class="form-label">Payment reference</label>
        <input type="text" name="payment_reference" class="form-control" value="<?= htmlspecialchars($f['payment_reference'] ?? ''); ?>"></div>
      <div class="col-md-5"><label class="form-label">Receipt URL</label>
        <input type="text" name="receipt_url" class="form-control" value="<?= htmlspecialchars($f['receipt_url'] ?? ''); ?>"></div>
      <div class="col-12"><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-end gap-2 py-3">
    <a href="<?= PROOT; ?>Expenses/view/<?= (int)$e['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>
</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 }));
</script>
<?php $this->end(); ?>
