<?php
/**
 * Partial view: a single reference row for the submission form.
 * Variables: $i (int index), $ref (array from ReferenceModel)
 */
$type = $ref['reference_type'] ?? 'external';
?>
<?php if ($type === 'internal'): ?>
<div class="ref-row border rounded p-3 mb-2 bg-light" data-type="internal">
  <input type="hidden" name="refs[<?= $i ?>][reference_type]" value="internal">
  <input type="hidden" name="refs[<?= $i ?>][cited_document_id]" class="ref-doc-id"
         value="<?= esc($ref['cited_document_id'] ?? '') ?>">
  <div class="d-flex align-items-start gap-2">
    <div class="flex-grow-1">
      <div class="input-group input-group-sm mb-1">
        <input type="text" class="form-control ref-search-input"
               placeholder="Cari judul di repositori..."
               autocomplete="off"
               style="<?= !empty($ref['cited_document_id']) ? 'display:none;' : '' ?>">
        <span class="input-group-text bg-gold border-0"><i class="bi bi-database"></i></span>
      </div>
      <div class="ref-search-results list-group"
           style="display:none;position:absolute;z-index:200;width:90%;"></div>
      <div class="ref-selected-label small text-success fw-semibold mt-1"
           <?= !empty($ref['cited_document_id']) ? '' : 'style="display:none;"' ?>>
        <?php if (!empty($ref['cited_document_id'])): ?>
          ✓ <?= esc($ref['title'] ?? 'Dokumen #' . $ref['cited_document_id']) ?>
        <?php endif; ?>
      </div>
      <input type="hidden" name="refs[<?= $i ?>][title]" class="ref-title-hidden"
             value="<?= esc($ref['title'] ?? '') ?>">
    </div>
    <button type="button" class="btn btn-sm btn-outline-danger"
            onclick="this.closest('.ref-row').remove()">
      <i class="bi bi-trash"></i>
    </button>
  </div>
</div>
<?php else: ?>
<div class="ref-row border rounded p-3 mb-2" data-type="external">
  <input type="hidden" name="refs[<?= $i ?>][reference_type]" value="external">
  <div class="d-flex gap-2 align-items-start">
    <div class="flex-grow-1 row g-2">
      <div class="col-12">
        <input type="text" name="refs[<?= $i ?>][raw_text]"
               class="form-control form-control-sm"
               placeholder="Teks referensi lengkap (APA/Turabian)..."
               value="<?= esc($ref['raw_text'] ?? '') ?>">
      </div>
      <div class="col-md-6">
        <input type="text" name="refs[<?= $i ?>][doi]"
               class="form-control form-control-sm"
               placeholder="DOI (opsional, contoh: 10.1234/...)"
               value="<?= esc($ref['doi'] ?? '') ?>">
      </div>
      <div class="col-md-6">
        <input type="url" name="refs[<?= $i ?>][url]"
               class="form-control form-control-sm"
               placeholder="URL (opsional)"
               value="<?= esc($ref['url'] ?? '') ?>">
      </div>
    </div>
    <button type="button" class="btn btn-sm btn-outline-danger"
            onclick="this.closest('.ref-row').remove()">
      <i class="bi bi-trash"></i>
    </button>
  </div>
</div>
<?php endif; ?>
