<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="save-btn"
        @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>
        
        <view class="event-body">
          <Markdown class="event-content" :content="event.content" />
        </view>

        <view class="ai-reply">
          <view class="ai-header">
            <text class="ai-icon">✨</text>
            <text class="ai-title">Life Harmony AI</text>
          </view>
          <Markdown class="ai-content" :content="event.aiFeedback" />
        </view>
      </view>
    </view>
  </view>
</template>

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

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%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ==================== 输入卡片 - 金色玻璃态 ==================== */
.input-card {
  padding: 40rpx 32rpx;
  flex-shrink: 0;
  /* 金色玻璃态背景 */
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.15), rgba(232, 121, 249, 0.1));
  border: 1px solid rgba(168, 85, 247, 0.3);
  border-radius: 48rpx;
  box-shadow: inset 0 0 30rpx rgba(168, 85, 247, 0.08);
  transition: all 0.3s ease;
}

.input-card:active {
  transform: scale(0.98);
  box-shadow: inset 0 0 20rpx rgba(168, 85, 247, 0.05);
}

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

/* 紫色圆点指示器 */
.dot {
  width: 16rpx;
  height: 16rpx;
  background: #C084FC;
  border-radius: 50%;
  box-shadow: 0 0 12rpx rgba(192, 132, 252, 0.6);
}

/* 卡片标题 - 原型标准 */
.card-title {
  font-size: 20rpx;
  color: rgba(192, 132, 252, 0.8);
  font-weight: 700;
  letter-spacing: 6rpx;
  text-transform: uppercase;
}

/* ==================== 输入框样式 - 玻璃态 ==================== */
.title-input {
  width: 100%;
  height: 80rpx;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 32rpx;
  padding: 0 24rpx;
  color: rgba(243, 232, 255, 0.9);
  font-size: 28rpx;
  margin-bottom: 16rpx;
  box-sizing: border-box;
  transition: all 0.2s ease;
}

.title-input:focus {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(168, 85, 247, 0.4);
  box-shadow: 0 0 12rpx rgba(168, 85, 247, 0.2);
}

.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);
  padding: 8rpx 0;
}

.content-textarea {
  width: 100%;
  height: 120rpx;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 32rpx;
  padding: 20rpx 24rpx;
  color: rgba(255, 255, 255, 0.8);
  font-size: 26rpx;
  line-height: 1.6;
  margin-bottom: 24rpx;
  box-sizing: border-box;
  transition: all 0.2s ease;
}

.content-textarea:focus {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(168, 85, 247, 0.4);
}

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

/* ==================== 保存按钮 - 原型标准 ==================== */
.save-btn {
  width: 100%;
  height: 56rpx;
  background: linear-gradient(to right, rgba(147, 51, 234, 0.4), rgba(192, 132, 252, 0.4));
  border: 1px solid rgba(168, 85, 247, 0.3);
  border-radius: 32rpx;
  color: #ffffff !important;
  font-size: 24rpx;
  font-weight: 600;
  transition: all 0.3s ease;
}

.save-btn:active {
  transform: scale(0.98);
  background: linear-gradient(to right, rgba(147, 51, 234, 0.6), rgba(192, 132, 252, 0.6));
}

/* ==================== 事件列表 ==================== */
.events-list {
  display: flex;
  flex-direction: column;
  gap: 32rpx;
}

/* ==================== 事件卡片 ==================== */
.event-card {
  padding: 32rpx;
  background: rgba(168, 85, 247, 0.05);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 48rpx;
  transition: all 0.3s ease;
}

.event-card:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 20rpx rgba(168, 85, 247, 0.05);
}

.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(243, 232, 255, 0.9);
  max-width: 70%;
}

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

.event-body {
  margin-bottom: 16rpx;
}

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

/* ==================== AI 回复区域 - 金色玻璃态核心特征 ==================== */
.ai-reply {
  margin-top: 24rpx;
  padding: 32rpx;
  border-radius: 40rpx;
  /* 金色玻璃态背景 */
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.08), rgba(232, 121, 249, 0.05));
  border: 1px solid rgba(168, 85, 247, 0.25);
  border-top: 2rpx solid rgba(255, 255, 255, 0.1);
  border-left: 4rpx solid #C084FC;
  box-shadow: 0 0 20px rgba(168, 85, 247, 0.1),
              inset 0 0 20rpx rgba(168, 85, 247, 0.05);
  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.03);
}

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

.ai-icon {
  font-size: 28rpx;
  filter: drop-shadow(0 0 8rpx rgba(192, 132, 252, 0.6));
}

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

.ai-content {
  font-size: 24rpx;
  line-height: 1.5;
}
</style>
