<template>
  <div>
    <div class="ainews-header">🤖 AI 资讯</div>
    <div class="ainews-item" v-for="(n, i) in state.aiNews.slice(0, 5)" :key="i">
      <img v-if="safeImageURL(n.picUrl)"
        class="ainews-img" :src="n.picUrl" loading="lazy"
        @error="($event.target as HTMLImageElement).style.display='none'" />
      <div class="ainews-body">
        <div class="ainews-title-row">
          <span class="ainews-title">{{ n.title }}</span>
          <span class="ainews-source">{{ n.source }} · {{ formatTime(n.ctime) }}</span>
        </div>
        <div class="ainews-desc" v-if="n.description">{{ n.description }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { state } from '../composables/useOverlayState'
import { safeImageURL } from '@shared/utils'

function formatTime(t: string) {
  return t?.length > 10 ? t.substring(5, 10) : t
}
</script>

<style scoped>
.ainews-header {
  font-size: 11px; font-weight: 700;
  color: var(--text-faint);
  letter-spacing: 0; margin-bottom: 14px;
}
.ainews-item {
  display: flex; gap: 14px;
  margin-bottom: 12px; padding-bottom: 12px;
  border-bottom: 1px solid var(--card-line-soft);
  min-height: 58px;
}
.ainews-item:last-child { margin-bottom: 0; padding-bottom: 0; border-bottom: none; }
.ainews-img {
  width: 92px; height: 58px;
  border-radius: 6px; object-fit: cover;
  flex-shrink: 0; opacity: 0.92;
  box-shadow: 0 8px 22px rgba(0,0,0,0.22);
}
.ainews-body { flex: 1; min-width: 0; }
.ainews-title-row {
  display: flex; align-items: baseline; gap: 10px;
}
.ainews-title {
  font-size: 14px; font-weight: 650;
  color: var(--text-main); line-height: 1.4;
  white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; flex: 1; min-width: 0;
}
.ainews-source { font-size: 10px; color: var(--text-faint); flex-shrink: 0; }
.ainews-desc {
  font-size: 12px; color: var(--text-soft);
  line-height: 1.5; margin-top: 5px;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
</style>
