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

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-plus me-2 text-muted"></i>New Expense</h5>
  <a href="<?= PROOT; ?>Expenses" 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="<?= PROOT; ?>Expenses/save" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">

    <!-- ── Type + amount ── -->
    <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 <span class="text-danger">*</span></label>
        <select name="expense_type_id" id="typeSel" class="form-select" required>
          <option value="">— select expense type —</option>
          <?php foreach ($this->data['types'] as $et): ?>
            <option value="<?= (int)$et['c0d']; ?>"
                    data-scope="<?= htmlspecialchars($et['scope']); ?>"
                    data-gl="<?= htmlspecialchars($et['gl_code'] . ' ' . $et['gl_name']); ?>"
                    data-req-truck="<?= (int)$et['requires_truck']; ?>"
                    data-req-driver="<?= (int)$et['requires_driver']; ?>"
                    data-req-vendor="<?= (int)$et['requires_vendor']; ?>"
                    data-req-trip="<?= (int)$et['requires_trip']; ?>"
                    <?= ($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 id="glHint" class="form-text small mt-1"></div>
      </div>
      <div class="col-md-2">
        <label class="form-label">Date <span class="text-danger">*</span></label>
        <input type="date" name="expense_date" class="form-control" required value="<?= htmlspecialchars($f['expense_date'] ?? date('Y-m-d')); ?>">
      </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 <span class="text-danger">*</span></label>
        <input type="text" name="gross_amount" class="form-control js-money" required 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'] ?? ''); ?>" placeholder="Brief description of what this expense is for">
      </div>
    </div>

    <hr class="my-4">

    <!-- ── Analytical tags ── -->
    <h6 class="text-uppercase text-muted small mb-3">Cost Attribution — tag what this expense belongs to</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']); ?><?php if ($d['employee_no']): ?> · <?= htmlspecialchars($d['employee_no']); ?><?php endif; ?>
            </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']); ?><?php if ($t['cargo_description']): ?> · <?= htmlspecialchars($t['cargo_description']); ?><?php endif; ?>
            </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">

    <!-- ── Payment intent + receipt ── -->
    <h6 class="text-uppercase text-muted small mb-3">Payment Method &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'] ?? ''); ?>" placeholder="Cheque #, M-Pesa txn, etc"></div>
      <div class="col-md-5"><label class="form-label">Receipt URL (attach later)</label>
        <input type="text" name="receipt_url" class="form-control" value="<?= htmlspecialchars($f['receipt_url'] ?? ''); ?>" placeholder="File-upload UI ships in a follow-up polish pass"></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>

    <hr class="my-4">
    <!-- ── Save-as ── -->
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Save as</label>
        <select name="status" class="form-select">
          <option value="draft" <?= ($f['status'] ?? 'draft')==='draft'?'selected':''; ?>>Draft (keep editing)</option>
          <option value="submitted" <?= ($f['status'] ?? '')==='submitted'?'selected':''; ?>>Submitted (send for review)</option>
        </select></div>
    </div>

  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>Expenses" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save expense</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 }));

// Show GL code + soft-warn on missing recommended tags when expense type changes.
const typeSel = document.getElementById('typeSel');
const glHint  = document.getElementById('glHint');
function refreshTypeHint() {
    const opt = typeSel.options[typeSel.selectedIndex]; if (!opt || !opt.value) { glHint.innerHTML = ''; return; }
    const gl  = opt.getAttribute('data-gl') || '';
    const reqs = [];
    if (opt.getAttribute('data-req-truck')  === '1') reqs.push('Truck');
    if (opt.getAttribute('data-req-driver') === '1') reqs.push('Driver');
    if (opt.getAttribute('data-req-vendor') === '1') reqs.push('Vendor');
    if (opt.getAttribute('data-req-trip')   === '1') reqs.push('Trip');
    glHint.innerHTML = '<i class="fas fa-book fa-xs me-1 text-muted"></i>Posts to <strong>' + gl + '</strong>' +
        (reqs.length ? ' · <span class="text-warning">recommended to tag:</span> ' + reqs.join(', ') : '');
}
typeSel?.addEventListener('change', refreshTypeHint);
refreshTypeHint();
</script>
<?php $this->end(); ?>
