/* ═══════════════════════════════════════════════════════════════════
   Evidence Attachments
   ═══════════════════════════════════════════════════════════════════ */
async function loadEvidence(nodeId) {
  if (!currentProject || !nodeId) return;
  const listEl = document.getElementById('evidence-file-list');
  const emptyEl = document.getElementById('evidence-empty');
  try {
    const files = await apiFetch(API + '/projects/' + currentProject + '/nodes/' + nodeId + '/evidence');
    if (!files || files.length === 0) {
      listEl.innerHTML = '<div class="evidence-empty" id="evidence-empty">暂无附件</div>';
      return;
    }
    listEl.innerHTML = files.map(f => {
      const name = typeof f === 'string' ? f : (f.filename || f.name || 'unknown');
      const size = f.size ? formatFileSize(f.size) : '';
      const fname = typeof f === 'string' ? f : (f.filename || f.name || '');
      return '<div class="evidence-file-item">' +
        '<span class="evidence-file-icon">📄</span>' +
        '<span class="evidence-file-name" onclick="downloadEvidence(\'' + escHtml(nodeId) + '\',\'' + escHtml(fname) + '\')">' + escHtml(name) + '</span>' +
        (size ? '<span class="evidence-file-size">' + size + '</span>' : '') +
        '<button class="evidence-file-delete" onclick="deleteEvidence(\'' + escHtml(nodeId) + '\',\'' + escHtml(fname) + '\')" title="删除">✕</button>' +
      '</div>';
    }).join('');
  } catch (e) {
    listEl.innerHTML = '<div class="evidence-empty">加载失败</div>';
  }
}

function uploadEvidence() {
  const id = window._currentNodeId;
  if (!id || !currentProject) { toast('请先选择节点'); return; }
  const input = document.createElement('input');
  input.type = 'file';
  input.multiple = true;
  input.onchange = async () => {
    if (!input.files || !input.files.length) return;
    const form = new FormData();
    for (const f of input.files) form.append('file', f);
    try {
      const res = await authFetch(API + '/projects/' + currentProject + '/nodes/' + id + '/evidence', {
        method: 'POST',
        body: form,
      });
      const j = await res.json();
      if (j.success) {
        toast('已上传 ' + (j.data ? j.data.length : input.files.length) + ' 个文件');
        loadEvidence(id);
      } else {
        toast('上传失败: ' + (j.error || ''));
      }
    } catch (e) { toast('上传失败: ' + e.message); }
  };
  input.click();
}

function downloadEvidence(nodeId, filename) {
  if (!currentProject || !nodeId || !filename) return;
  const url = API + '/projects/' + currentProject + '/nodes/' + nodeId + '/evidence/' + encodeURIComponent(filename);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click();
}

async function deleteEvidence(nodeId, filename) {
  if (!currentProject || !nodeId || !filename) return;
  if (!confirm('确定删除附件「' + filename + '」？')) return;
  try {
    await apiFetch(API + '/projects/' + currentProject + '/nodes/' + nodeId + '/evidence/' + encodeURIComponent(filename), {
      method: 'DELETE'
    });
    toast('附件已删除');
    loadEvidence(nodeId);
  } catch (e) { /* already toasted */ }
}
