document.addEventListener('DOMContentLoaded', () => { const TOPICS_STORAGE_KEY = 'kaixinapp_user_topics_v4'; let nextUserTopicId = 1; let aiTopics = [{ id: "ai-1", date: "2025年7月14日", title: "关于职业发展的思考", summary: "近期你在日记和聊天中多次提到对目前工作的倦怠感,并探索学习新技能(如编程、设计)的可能性。", keywords: ["职业倦怠", "新技能", "转行", "自我提升"], timeline: [ { stage_name: "探索阶段 (前期)", stage_icon: "search", items: [ { id: 2, date: '2025-07-10', content: '搜索了在线编程课程,感觉眼花缭乱。' }, { id: 3, date: '2025-07-12', content: '在日记里写下了对目前工作的厌烦,感觉陷入了瓶颈。' } ] }, { stage_name: "调研阶段 (当前)", stage_icon: "clipboard-list", items: [ { id: 1, date: '2025-07-14', content: '和开开聊了关于转行做设计师的可能性,开开给了一些建议。' } ] } ], next_suggestion: '尝试接触一些免费的设计工具(如Figma、Canva),完成一个名片设计或海报制作的小项目。这能帮你评估自己对设计工作的实际兴趣和基本感觉。' }, { id: "ai-2", date: "2025年7月10日", title: "周末出游计划", summary: "你似乎在计划一个短途旅行,多次查询关于海边城市的天气和美食推荐。", keywords: ["旅行", "海边", "美食", "放松"], timeline: [ { stage_name: "萌芽阶段 (前期)", stage_icon: "sprout", items: [ { id: 2, date: '2025-07-09', content: '天气好热,突然想去海边玩。' } ] }, { stage_name: "计划阶段 (当前)", stage_icon: "map", items: [ { id: 1, date: '2025-07-10', content: '问开开哪个海边城市人少又好玩,它推荐了几个小众地点。' }, ] } ], next_suggestion: '可以开始查看交通和住宿了,早点预定选择更多哦!如果需要,开开可以帮你对比价格。' }]; let userTopics = []; const aiSummaryContainer = document.getElementById('ai-summary-list'); const userTopicsListContainer = document.getElementById('user-topics-list'); const newTopicForm = document.getElementById('new-topic-form'); const modal = document.getElementById('topic-detail-modal'); const modalTitle = document.getElementById('modal-topic-title'); const modalDate = document.getElementById('modal-topic-date'); const closeModalBtn = document.getElementById('close-modal-btn'); const addEntryForm = document.getElementById('add-entry-form'); let activeTopicId = null; function loadTopicsFromStorage() { const stored = localStorage.getItem(TOPICS_STORAGE_KEY); if (stored) { userTopics = JSON.parse(stored); const maxId = userTopics.reduce((max, t) => Math.max(max, parseInt(t.id.split('-')[1])), 0); nextUserTopicId = maxId + 1; } else { userTopics = [{ id: `user-${nextUserTopicId++}`, title: "暑期健身计划", date: "2025年7月15日", keywords: ["健康", "运动", "自律"], timeline: [{ stage_name: "启动阶段 (当前)", stage_icon: "rocket", items: [ { id: 1, date: '2025-07-15', content: "今天制定了计划:每周至少三次有氧运动,两次力量训练。记录每日饮食,控制热量摄入。" } ] }], next_suggestion: '找一个伙伴一起监督,或者使用App记录进程,增加成就感。' }]; } } function saveTopicsToStorage() { localStorage.setItem(TOPICS_STORAGE_KEY, JSON.stringify(userTopics)); } function renderAiTopics() { if (!aiSummaryContainer) return; aiSummaryContainer.innerHTML = aiTopics.map(topic => `

${topic.date}

${topic.title}

${topic.summary}

${topic.keywords.map(k => `${k}`).join('')}
`).join(''); aiSummaryContainer.querySelectorAll('[data-topic-id]').forEach(el => el.addEventListener('click', () => openModal(el.dataset.topicId))); } function renderUserTopics() { if (!userTopicsListContainer) return; const getLastEntryContent = (topic) => { if (!topic.timeline || topic.timeline.length === 0) return "暂无内容"; const lastStage = topic.timeline[topic.timeline.length - 1]; if (!lastStage.items || lastStage.items.length === 0) return "暂无内容"; return lastStage.items[lastStage.items.length - 1].content; }; userTopicsListContainer.innerHTML = userTopics.length === 0 ? `

还没有创建话题哦

` : userTopics.map(topic => `

${topic.title}

${getLastEntryContent(topic)}

`).join(''); userTopicsListContainer.querySelectorAll('[data-topic-id]').forEach(el => el.addEventListener('click', () => openModal(el.dataset.topicId))); userTopicsListContainer.querySelectorAll('[data-delete-id]').forEach(el => el.addEventListener('click', (e) => { e.stopPropagation(); if(confirm('确定要删除这个话题吗?此操作无法撤销。')) { deleteTopic(el.dataset.deleteId); } })); if (typeof lucide !== 'undefined') lucide.createIcons(); } function deleteTopic(topicId) { userTopics = userTopics.filter(t => t.id !== topicId); saveTopicsToStorage(); renderUserTopics(); } function openModal(topicId) { const topic = [...aiTopics, ...userTopics].find(t => t.id === topicId); if (!topic) return; activeTopicId = topicId; const isUserTopic = activeTopicId.startsWith('user-'); document.getElementById('add-entry-form').style.display = isUserTopic ? 'flex' : 'none'; document.querySelector('#add-entry-form').previousElementSibling.style.display = isUserTopic ? 'block' : 'none'; modalTitle.textContent = topic.title; modalDate.textContent = `创建于 ${topic.date}`; renderTopicTimeline(topic); modal.classList.remove('hidden'); modal.classList.add('flex'); } function renderTopicTimeline(topic){ const timelineContainer = document.getElementById('modal-timeline-container'); const suggestionContainer = document.getElementById('modal-suggestion-container'); timelineContainer.innerHTML = ''; suggestionContainer.innerHTML = ''; const isUserTopic = topic.id.startsWith('user-'); if(topic.timeline && topic.timeline.length > 0) { topic.timeline.forEach((stage, stageIndex) => { const isCurrentStage = stageIndex === topic.timeline.length - 1; const stageHtml = `

${stage.stage_name}

${isCurrentStage ? '当前' : ''}
${stage.items.sort((a,b) => new Date(b.date) - new Date(a.date)).map(entry => `

${new Date(entry.date).toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' })}

${isUserTopic ? `` : ''}

${entry.content.replace(/\\n/g, '
')}

`).join('')}
`; timelineContainer.innerHTML += stageHtml; }); } if (topic.next_suggestion) { suggestionContainer.innerHTML = `

开开的下一步建议

${topic.next_suggestion}

`; } timelineContainer.querySelectorAll('[data-delete-entry-id]').forEach(btn => { btn.addEventListener('click', () => deleteTopicEntry(btn.dataset.deleteEntryId)); }); if (typeof lucide !== 'undefined') lucide.createIcons(); } function deleteTopicEntry(entryId) { const topic = userTopics.find(t => t.id === activeTopicId); if (!topic) return; const totalEntries = topic.timeline.reduce((acc, stage) => acc + stage.items.length, 0); if (totalEntries <= 1) { alert('每个话题至少需要一条记录。如不需此话题,可直接删除整个话题卡片。'); return; } topic.timeline.forEach(stage => { stage.items = stage.items.filter(entry => entry.id.toString() !== entryId.toString()); }); topic.timeline = topic.timeline.filter(stage => stage.items.length > 0); saveTopicsToStorage(); renderTopicTimeline(topic); } function closeModal() { activeTopicId = null; modal.classList.add('hidden'); modal.classList.remove('flex'); } newTopicForm.addEventListener('submit', (e) => { e.preventDefault(); const title = e.target.elements.title.value.trim(); const content = e.target.elements.content.value.trim(); if (title && content) { const newTopic = { id: `user-${nextUserTopicId++}`, title: title, date: new Date().toLocaleDateString('zh-CN'), keywords: ["自定义"], timeline: [{ stage_name: "启动阶段 (当前)", stage_icon: "rocket", items: [{ id: 1, date: new Date().toISOString().split('T')[0], content: content }] }], next_suggestion: '将大目标分解成几个可执行的小步骤吧!' }; userTopics.unshift(newTopic); saveTopicsToStorage(); renderUserTopics(); e.target.reset(); } }); addEntryForm.addEventListener('submit', (e) => { e.preventDefault(); const contentEl = e.target.elements['new-entry-content']; const content = contentEl.value.trim(); if (!content || !activeTopicId) return; const topic = userTopics.find(t => t.id === activeTopicId); if(!topic) return; let maxId = 0; topic.timeline.forEach(stage => { maxId = Math.max(maxId, ...stage.items.map(item => item.id)); }); const newEntry = { id: maxId + 1, date: new Date().toISOString().split('T')[0], content: content }; if (topic.timeline.length > 0) { topic.timeline[topic.timeline.length - 1].items.push(newEntry); } else { topic.timeline.push({ stage_name: "新进展 (当前)", stage_icon: "plus", items: [newEntry] }) } saveTopicsToStorage(); renderTopicTimeline(topic); contentEl.value = ''; }); closeModalBtn.addEventListener('click', closeModal); modal.addEventListener('click', (e) => { if (e.target === modal) closeModal(); }); loadTopicsFromStorage(); renderAiTopics(); renderUserTopics(); if (typeof lucide !== 'undefined') { lucide.createIcons(); } });