<template>
  <div class="page detail">
    <van-nav-bar title="表白详情" left-text="返回" left-arrow @click-left="back" />

    <div v-if="loading" class="detail__tip">加载中…</div>
    <van-empty v-else-if="!conf" description="表白不存在或已被移除" />

    <div v-else class="page__inner">
      <!-- 表白卡片 -->
      <article class="conf" :style="{ background: themeAt(conf.theme).bg }">
        <div v-if="conf.to" class="conf__to" :style="{ color: themeAt(conf.theme).accent }">
          致 · {{ conf.to }}
        </div>
        <p class="conf__content">{{ conf.content }}</p>
        <div class="conf__foot">
          <span class="conf__author">
            <span class="conf__avatar" :style="{ background: themeAt(conf.theme).accent }">{{ initial(conf.author) }}</span>
            <span class="conf__name">{{ conf.author }}</span>
            <span class="conf__time">· {{ formatTime(conf.createdAt) }}</span>
          </span>
          <button
            class="conf__like"
            :class="{ 'is-liked': liked }"
            :style="liked ? { color: themeAt(conf.theme).accent } : null"
            @click="like"
          >
            <AppIcon name="heart" :size="16" />
            <span>{{ conf.likes }}</span>
          </button>
        </div>
      </article>

      <!-- 评论区 -->
      <div class="section">
        <div class="section__head">
          <span class="section__title">评论 {{ conf.comments.length }}</span>
        </div>

        <div class="card comments">
          <van-empty v-if="!conf.comments.length" description="还没有评论，来做第一个" />
          <div
            v-for="(m, i) in conf.comments"
            :key="m.id"
            class="comment"
            :class="{ 'comment--line': i !== conf.comments.length - 1 }"
          >
            <span class="comment__avatar">{{ initial(m.name) }}</span>
            <div class="comment__main">
              <div class="comment__top">
                <span class="comment__name">{{ m.name }}</span>
                <span class="comment__time">{{ formatTime(m.createdAt) }}</span>
              </div>
              <p class="comment__text">{{ m.content }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部评论输入 -->
    <div v-if="conf" class="cbar">
      <input v-model="cName" class="cbar__name" placeholder="昵称" maxlength="12" />
      <input
        v-model="cText"
        class="cbar__text"
        placeholder="写句评论鼓励一下…"
        maxlength="80"
        @keyup.enter="sendComment"
      />
      <button class="cbar__send" :disabled="sending" @click="sendComment">发送</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant'
import {
  fetchConfession,
  likeConfession,
  commentConfession,
  hasLiked,
  formatTime,
  themeAt
} from '../store/wall.js'

const route = useRoute()
const router = useRouter()

const conf = ref(null)
const loading = ref(true)
const liked = ref(false)
const cName = ref('')
const cText = ref('')
const sending = ref(false)

async function load() {
  loading.value = true
  try {
    const data = await fetchConfession(route.params.id)
    conf.value = { ...data, comments: data.comments || [] }
    liked.value = hasLiked(data.id)
  } catch (e) {
    conf.value = null
  } finally {
    loading.value = false
  }
}
load()

function back() {
  if (window.history.length > 1) router.back()
  else router.replace('/wall')
}
function initial(name) {
  return (name || '匿')[0]
}
async function like() {
  if (liked.value) {
    showToast('你已经点过啦')
    return
  }
  try {
    const likes = await likeConfession(conf.value.id)
    conf.value.likes = typeof likes === 'number' ? likes : conf.value.likes + 1
    liked.value = true
  } catch (e) {
    showToast(e.message || '操作失败')
  }
}
async function sendComment() {
  const content = cText.value.trim()
  if (!content) {
    showToast('先写点评论吧～')
    return
  }
  sending.value = true
  try {
    const comment = await commentConfession(conf.value.id, {
      name: cName.value.trim(),
      content
    })
    conf.value.comments.push(comment)
    cText.value = ''
    showToast('评论成功')
  } catch (e) {
    showToast(e.message || '评论失败')
  } finally {
    sending.value = false
  }
}
</script>

<style scoped>
.detail {
  background: var(--brand-bg);
  min-height: 100vh;
  padding-bottom: 68px;
}
.detail__tip {
  text-align: center;
  color: var(--brand-text-tertiary);
  font-size: 13px;
  padding: 40px 0;
}

/* 表白卡片 */
.conf {
  border-radius: var(--brand-radius-lg);
  padding: 18px;
  box-shadow: var(--brand-shadow-sm);
}
.conf__to {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 10px;
}
.conf__content {
  margin: 0;
  color: var(--brand-text);
  font-size: 16px;
  line-height: 1.8;
  white-space: pre-wrap;
  word-break: break-word;
}
.conf__foot {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.conf__author {
  display: flex;
  align-items: center;
  gap: 6px;
}
.conf__avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.conf__name {
  font-size: 13px;
  font-weight: 600;
  color: var(--brand-text-secondary);
}
.conf__time {
  font-size: 12px;
  color: var(--brand-text-tertiary);
}
.conf__like {
  display: flex;
  align-items: center;
  gap: 4px;
  border: none;
  background: rgba(255, 255, 255, 0.6);
  color: var(--brand-text-tertiary);
  font-size: 14px;
  padding: 6px 12px;
  border-radius: 16px;
  cursor: pointer;
}
.conf__like.is-liked {
  font-weight: 700;
}

/* 评论 */
.comments {
  padding: 4px 14px;
}
.comment {
  display: flex;
  gap: 10px;
  padding: 12px 0;
  position: relative;
}
.comment--line::after {
  content: '';
  position: absolute;
  left: 44px;
  right: 0;
  bottom: 0;
  height: 1px;
  background: var(--brand-border);
}
.comment__avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--brand-primary-light);
  color: var(--brand-primary);
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 34px;
}
.comment__main {
  flex: 1;
  min-width: 0;
}
.comment__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.comment__name {
  font-size: 13px;
  font-weight: 600;
  color: var(--brand-text-secondary);
}
.comment__time {
  font-size: 11px;
  color: var(--brand-text-tertiary);
}
.comment__text {
  margin: 4px 0 0;
  font-size: 14px;
  line-height: 1.6;
  color: var(--brand-text);
  word-break: break-word;
}

/* 底部评论输入条 */
.cbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--brand-surface);
  border-top: 1px solid var(--brand-border);
  z-index: 50;
}
.cbar__name {
  width: 66px;
  flex: 0 0 66px;
  border: 1px solid var(--brand-border);
  border-radius: 16px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
}
.cbar__text {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--brand-border);
  border-radius: 16px;
  padding: 8px 12px;
  font-size: 14px;
  outline: none;
}
.cbar__text:focus,
.cbar__name:focus {
  border-color: var(--brand-primary);
}
.cbar__send {
  flex: 0 0 auto;
  border: none;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 16px;
  background: linear-gradient(135deg, #ff6b9d, #ff8f6b);
  cursor: pointer;
}
.cbar__send:disabled {
  opacity: 0.6;
}
</style>
