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

<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 Supplier Bill</h5>
  <a href="<?= PROOT; ?>SupplierBills" 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; ?>SupplierBills/save" class="card border-0 shadow-sm">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-5">
        <label class="form-label">Supplier <span class="text-danger">*</span></label>
        <select name="vendor_id" class="form-select" required>
          <option value="">— pick —</option>
          <?php foreach ($suppliers as $s): ?>
            <option value="<?= (int)$s['c0d']; ?>" <?= ((int)($f['vendor_id'] ?? 0) === (int)$s['c0d']) ? 'selected' : ''; ?>>
              <?= htmlspecialchars($s['vendor_name']); ?> <?php if ($s['vendor_tin']): ?>· TIN <?= htmlspecialchars($s['vendor_tin']); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select>
        <div class="form-text small">Not listed? <a href="<?= PROOT; ?>Suppliers/add" target="_blank">Add a supplier</a> first.</div>
      </div>
      <div class="col-md-3"><label class="form-label">Supplier's ref</label><input type="text" name="supplier_ref" class="form-control" value="<?= htmlspecialchars($f['supplier_ref'] ?? ''); ?>" placeholder="INV-1234"></div>
      <div class="col-md-2"><label class="form-label">Bill date</label><input type="date" name="bill_date" class="form-control" value="<?= htmlspecialchars($f['bill_date'] ?? date('Y-m-d')); ?>"></div>
      <div class="col-md-2">
        <label class="form-label">Terms</label>
        <select name="payment_terms" class="form-select">
          <?php foreach (['net_7'=>'Net 7','net_14'=>'Net 14','net_30'=>'Net 30','net_45'=>'Net 45','net_60'=>'Net 60','net_90'=>'Net 90'] as $k => $v): ?>
            <option value="<?= $k; ?>" <?= ($f['payment_terms'] ?? 'net_30') === $k ? 'selected' : ''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-md-5">
        <label class="form-label">Expense account <span class="text-danger">*</span></label>
        <select name="expense_account_id" class="form-select" required>
          <option value="">— pick —</option>
          <?php foreach ($expenses as $e): ?>
            <option value="<?= (int)$e['c0d']; ?>" <?= ((int)($f['expense_account_id'] ?? 0) === (int)$e['c0d']) ? 'selected' : ''; ?>>
              <?= htmlspecialchars($e['account_code']); ?> · <?= htmlspecialchars($e['account_name']); ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2"><label class="form-label">Subtotal (net)</label><input type="number" step="0.01" min="0" name="subtotal" id="fSub" class="form-control text-end" value="<?= htmlspecialchars($f['subtotal'] ?? '0.00'); ?>" required></div>
      <div class="col-md-2"><label class="form-label">VAT amount</label><input type="number" step="0.01" min="0" name="vat_amount" id="fVat" class="form-control text-end" value="<?= htmlspecialchars($f['vat_amount'] ?? '0.00'); ?>"></div>
      <div class="col-md-3"><label class="form-label">Total</label><div id="fTotal" class="form-control text-end fw-bold" style="background:#F5F7FB;">0.00</div></div>

      <div class="col-md-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; ?>SupplierBills" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save draft</button>
  </div>
</form>

<div class="alert alert-info small mt-3">
  <i class="fas fa-info-circle me-1"></i>
  Saving creates a <strong>draft</strong>. Nothing hits the ledger until you review the bill and click <strong>Post</strong> — you'll get a bill number (BILL-YYYY-NNNN) at that point.
</div>
<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function updateTotal() {
    const s = parseFloat(document.getElementById('fSub').value) || 0;
    const v = parseFloat(document.getElementById('fVat').value) || 0;
    document.getElementById('fTotal').textContent = (s + v).toFixed(2);
}
document.getElementById('fSub').addEventListener('input', updateTotal);
document.getElementById('fVat').addEventListener('input', updateTotal);
updateTotal();
</script>
<?php $this->end(); ?>
