<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;
  min-height: 100%;
}

.input-card {
  padding: 32rpx;
  flex-shrink: 0;
}

.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.05);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(168, 85, 247, 0.15);
  border-left: 4rpx solid #C084FC;
  box-shadow: 0 0 20px rgba(168, 85, 247, 0.1),
              inset 0 0 15rpx rgba(168, 85, 247, 0.03);
  border-radius: 20rpx;
  padding: 28rpx;
  margin-top: 24rpx;
  transition: all 0.3s ease;
}

.ai-reply:active {
  transform: scale(0.98);
  box-shadow: 0 0 12px rgba(168, 85, 247, 0.08),
              inset 0 0 10rpx rgba(168, 85, 247, 0.02);
}

.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;
  overflow: hidden;
}

.ai-content.typing {
  animation: typing-reveal 2.5s cubic-bezier(0.4, 0, 0.2, 1),
             glow-pulse 2s ease-in-out infinite 1s;
}

@keyframes typing-reveal {
  from {
    opacity: 0;
    transform: translateY(8rpx);
    filter: blur(4rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
    filter: blur(0);
  }
}

@keyframes glow-pulse {
  0%, 100% {
    text-shadow: 0 0 4rpx rgba(192, 132, 252, 0.3);
  }
  50% {
    text-shadow: 0 0 8rpx rgba(192, 132, 252, 0.6);
  }
}
</style>
