<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<div class="container mt-4">
  <h2>Edit Reimbursement #<?= esc($reimb['id']) ?></h2>
  <?php if (!empty($errors)): ?>
    <div class="alert alert-danger"><?= implode('<br>', array_map('esc', $errors)) ?></div>
  <?php endif; ?>

  <form method="post" action="<?= site_url('reimbursements/update/'.$reimb['id']) ?>" enctype="multipart/form-data">
    <?= csrf_field() ?>

    <div class="mb-3">
      <label>Amount</label>
      <input type="number" step="0.01" name="amount" class="form-control"
             value="<?= old('amount', $reimb['amount']) ?>" required>
    </div>

    <div class="mb-3">
      <label>Reimbursed To</label>
      <?php $currentRecipient = (string) (old('reimbursed_to', $reimb['reimbursed_to']) ?? ''); ?>
      <select name="reimbursed_to" class="form-control" required>
        <?php foreach ($users as $u): ?>
          <?php
            $id    = (string) ($u['id'] ?? '');
            $label = trim(($u['firstname'] ?? '') . ' ' . ($u['lastname'] ?? ''));
          ?>
          <option value="<?= esc($id) ?>" <?= $currentRecipient === $id ? 'selected' : '' ?>>
            <?= esc($label !== '' ? $label : 'Unknown') ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="mb-3">
      <label>Reimbursement Method</label>
      <select name="reimbursement_method" id="reimbursement_method" class="form-control" required>
        <option value="Cash"  <?= $reimb['reimbursement_method']==='Cash'?'selected':'' ?>>Cash</option>
        <option value="Check" <?= $reimb['reimbursement_method']==='Check'?'selected':'' ?>>Check</option>
      </select>
    </div>

    <div class="mb-3" id="check_number_group" style="<?= $reimb['reimbursement_method']==='Check'?'':'display:none' ?>">
      <label>Check Number</label>
      <input type="text" name="check_number" class="form-control" value="<?= old('check_number', $reimb['check_number']) ?>">
    </div>

    <div class="mb-3">
      <label>Receipt (optional to replace)</label>
      <?php if (!empty($receipt_url)): ?>
        <div class="mb-2"><a href="<?= esc($receipt_url) ?>" target="_blank">Current receipt</a></div>
      <?php endif; ?>
      <input type="file" name="receipt" class="form-control" accept=".jpg,.jpeg,.png,.webp,.gif,.pdf">
      <?php if (!empty($reimb['receipt_path'])): ?>
        <div class="form-check mt-2">
          <input class="form-check-input" type="checkbox" name="remove_receipt" value="1" id="rmr">
          <label class="form-check-label" for="rmr">Remove existing receipt</label>
        </div>
      <?php endif; ?>
    </div>

    <div class="mb-3">
      <label>Description</label>
      <textarea name="description" class="form-control"><?= old('description', $reimb['description']) ?></textarea>
    </div>

    <button class="btn btn-primary">Save</button>
    <a class="btn btn-secondary" href="<?= site_url('reimbursements') ?>">Cancel</a>
  </form>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
document.getElementById('reimbursement_method').addEventListener('change', e => {
  document.getElementById('check_number_group').style.display = (e.target.value === 'Check') ? '' : 'none';
});
</script>
<?= $this->endSection() ?>
