/* ═══════════════════════════════════════════════════════════════════
   Import Tool
   ═══════════════════════════════════════════════════════════════════ */
document.getElementById('btn-import').addEventListener('click', () => {
  if (!currentProject) return;
  showImportModal();
});

function showImportModal() {
  document.getElementById('import-modal').classList.add('active');
  document.getElementById('import-textarea').value = '';
  document.getElementById('import-preview').classList.remove('visible');
  document.getElementById('import-result').classList.remove('visible');
  document.getElementById('import-result').className = 'import-result';
  document.getElementById('btn-do-import').disabled = false;
}

function hideImportModal() {
  document.getElementById('import-modal').classList.remove('active');
}

function previewImport() {
  const textarea = document.getElementById('import-textarea');
  const preview = document.getElementById('import-preview');
  const result = document.getElementById('import-result');
  result.classList.remove('visible');
  result.className = 'import-result';
  try {
    const data = JSON.parse(textarea.value);
    const nodes = data.nodes || [];
    const edges = data.edges || [];
    document.getElementById('import-node-count').textContent = nodes.length;
    document.getElementById('import-edge-count').textContent = edges.length;
    preview.classList.add('visible');
  } catch (e) {
    preview.classList.remove('visible');
  }
}

async function executeImport() {
  if (!currentProject) return;
  const textarea = document.getElementById('import-textarea');
  const result = document.getElementById('import-result');
  let data;
  try {
    data = JSON.parse(textarea.value);
  } catch (e) {
    result.textContent = 'JSON 格式错误: ' + e.message;
    result.className = 'import-result visible error';
    return;
  }
  document.getElementById('btn-do-import').disabled = true;
  try {
    const res = await authFetch(API + '/projects/' + currentProject + '/import', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data),
    });
    const j = await res.json();
    if (j.success) {
      const d = j.data || {};
      const lines = [];
      if (d.created_nodes) lines.push('✓ 创建 ' + d.created_nodes + ' 个节点');
      if (d.created_edges) lines.push('✓ 创建 ' + d.created_edges + ' 条连线');
      if (d.skipped && d.skipped.length > 0) lines.push('⚠ 跳过 ' + d.skipped.length + ' 项');
      if (d.errors && d.errors.length > 0) lines.push('✗ 错误: ' + d.errors.join('; '));
      if (lines.length === 0) lines.push('✓ 导入完成');
      result.innerHTML = lines.join('<br>');
      result.className = 'import-result visible success';
      toast('导入成功');
      loadGraph();
      // If in timeline view, refresh it too
      if (currentView === 'timeline') loadTimeline();
      setTimeout(() => hideImportModal(), 1500);
    } else {
      result.textContent = '导入失败: ' + (j.error || '未知错误');
      result.className = 'import-result visible error';
    }
  } catch (e) {
    result.textContent = '导入失败: ' + e.message;
    result.className = 'import-result visible error';
  }
  document.getElementById('btn-do-import').disabled = false;
}
