<template>
  <view class="record-view">
    <view class="input-card glass-card">
      <view class="card-header">
        <view class="dot"></view>
        <text class="card-title">记叙当下</text>
      </view>
      
      <input
        class="title-input"
        placeholder="为这段记忆命名"
        v-model="newEvent.title"
      />
      
      <picker class="date-picker" mode="date" :value="newEvent.time" @change="onDateChange">
        <view class="date-value">{{ newEvent.time || '选择日期' }}</view>
      </picker>
      
      <textarea
        class="content-textarea"
        placeholder="此刻的心境或发生的事..."
        v-model="newEvent.content"
        rows="3"
      />
      
      <button
        class="btn-primary save-btn"
        :disabled="!canSave"
        :class="{ disabled: !canSave }"
        @click="saveEvent"
      >
        镌刻至星海
      </button>
    </view>
    
    <view class="events-list">
      <view
        v-for="event in events"
        :key="event.id"
        class="event-card glass-card"
      >
        <view class="event-header">
          <text class="event-title">{{ event.title }}</text>
          <text class="event-date">{{ event.time }}</text>
        </view>
        
        <text class="event-content">{{ event.content }}</text>
        
        <view class="ai-reply">
          <view class="ai-header">
            <text class="ai-icon">✨</text>
            <text class="ai-title">Life Harmony AI</text>
          </view>
          <text class="ai-content" :class="{ typing: event.isNew }">
            {{ event.aiFeedback }}
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useAppStore } from '../../stores/app.js'

const store = useAppStore()

const newEvent = ref({
  title: '',
  time: getTodayDate(),
  content: ''
})

const events = computed(() => store.events)

const canSave = computed(() => {
  return newEvent.value.title && newEvent.value.content
})

function getTodayDate() {
  const today = new Date()
  return today.toISOString().split('T')[0]
}

const onDateChange = (e) => {
  newEvent.value.time = e.detail.value
}

const saveEvent = async () => {
  if (!canSave.value) return
  
  const eventData = {
    ...newEvent.value,
    aiFeedback: '星河守望者正在解读这段紫色波频...',
    isNew: true
  }
  
  await store.createEvent(eventData)
  
  newEvent.value = {
    title: '',
    time: getTodayDate(),
    content: ''
  }
  
  setTimeout(async () => {
    const fullReply = `这段记忆碎片在星海中漾起涟漪。你在${eventData.title}中展现的特质，正在重新定义你人生OS的底层代码。继续保持这份觉知。`
    eventData.aiFeedback = fullReply
    eventData.isNew = false
    await store.fetchEvents()
  }, 1500)
}

onMounted(() => {
  store.fetchEvents()
})
</script>

<style scoped>
.record-view {
  display: flex;
  flex-direction: column;
  gap: 32rpx;
}

.input-card {
  padding: 32rpx;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12rpx;
  margin-bottom: 24rpx;
}

.dot {
  width: 16rpx;
  height: 16rpx;
  background: #C084FC;
  border-radius: 50%;
}

.card-title {
  font-size: 18rpx;
  color: rgba(192, 132, 252, 0.8);
  font-weight: 600;
  letter-spacing: 4rpx;
  text-transform: uppercase;
}

.title-input {
  width: 100%;
  height: 80rpx;
  background: transparent;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  color: rgba(255, 255, 255, 0.9);
  font-size: 28rpx;
  margin-bottom: 16rpx;
}

.title-input::placeholder {
  color: rgba(255, 255, 255, 0.3);
}

.date-picker {
  margin-bottom: 16rpx;
}

.date-value {
  font-size: 22rpx;
  color: rgba(168, 85, 247, 0.6);
}

.content-textarea {
  width: 100%;
  height: 160rpx;
  background: transparent;
  color: rgba(255, 255, 255, 0.8);
  font-size: 26rpx;
  margin-bottom: 24rpx;
}

.content-textarea::placeholder {
  color: rgba(255, 255, 255, 0.3);
}

.save-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, rgba(147, 51, 234, 0.4), rgba(124, 58, 237, 0.4));
  border: 1px solid rgba(168, 85, 247, 0.3);
  border-radius: 32rpx;
  box-shadow: 0 8rpx 32rpx rgba(168, 85, 247, 0.2);
}

.save-btn.disabled {
  opacity: 0.5;
}

.events-list {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.event-card {
  padding: 32rpx;
}

.event-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16rpx;
}

.event-title {
  font-size: 30rpx;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
}

.event-date {
  font-size: 20rpx;
  color: rgba(168, 85, 247, 0.5);
}

.event-content {
  display: block;
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.6;
  margin-bottom: 24rpx;
}

.ai-reply {
  background: rgba(168, 85, 247, 0.08);
  border: 1px solid rgba(168, 85, 247, 0.25);
  border-radius: 20rpx;
  padding: 24rpx;
  box-shadow: 0 0 20px rgba(168, 85, 247, 0.1);
  margin-top: 16rpx;
}

.ai-header {
  display: flex;
  align-items: center;
  gap: 8rpx;
  margin-bottom: 12rpx;
}

.ai-icon {
  font-size: 24rpx;
}

.ai-title {
  font-size: 18rpx;
  color: rgba(192, 132, 252, 0.8);
  font-weight: 600;
  letter-spacing: 4rpx;
  text-transform: uppercase;
}

.ai-content {
  font-size: 22rpx;
  color: rgba(243, 232, 255, 0.8);
  font-style: italic;
  line-height: 1.5;
}

.ai-content.typing {
  animation: typing 2s ease-out;
}

@keyframes typing {
  from { opacity: 0; transform: translateY(10rpx); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
