<template>
  <div class="article-card" @click="handleClick">
    <div class="article-image">
      <img v-if="resource?.coverImage" :src="FILE_DOWNLOAD_URL + resource.coverImage" :alt="resource.title" />
      <div v-else class="image-placeholder"></div>
      <div class="article-tag">精选文章</div>
    </div>
    <div class="article-content">
      <h3 class="article-title">{{ resource?.title || '标题' }}</h3>
      <p class="article-desc">
        {{ resource?.summary || '暂无简介' }}
      </p>
      <div class="article-footer">
        <div class="meta-tag">
          <el-icon><Document /></el-icon>
          <span>热门文章</span>
        </div>
        <span class="view-count">{{ formatViewCount(resource?.viewCount || 0) }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { Document } from '@element-plus/icons-vue';
import type { ResourceRecommendVO } from '@/types';
import { FILE_DOWNLOAD_URL } from '@/config';

const props = defineProps<{
  resource?: ResourceRecommendVO;
}>();

const router = useRouter();

// 格式化浏览量
function formatViewCount(count: number): string {
  if (count < 1000) {
    return `${count}次浏览`;
  } else if (count < 10000) {
    return `${(count / 1000).toFixed(1)}k次浏览`;
  } else {
    return `${(count / 10000).toFixed(1)}w次浏览`;
  }
}

// 点击卡片
function handleClick() {
  if (props.resource?.resourceID) {
    router.push(`/article/show?articleId=${props.resource.resourceID}`);
  }
}
</script>

<style lang="scss" scoped>
.article-card {
  background: #FFFFFF;
  border-radius: 0.625em;
  overflow: hidden;
  box-shadow: 0px 0.5em 1.25em 0px rgba(164, 182, 199, 0.2);
  transition: all 0.3s;
  cursor: pointer;
  display: flex;
  flex-direction: column;

  &:hover {
    transform: translateY(-0.25em);
    box-shadow: 0px 0.75em 1.75em 0px rgba(164, 182, 199, 0.3);
  }

  .article-image {
    width: 100%;
    aspect-ratio: 384 / 221;
    position: relative;
    overflow: hidden;
    flex-shrink: 0;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .image-placeholder {
      width: 100%;
      height: 100%;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background: url('data:image/svg+xml,<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"><defs><pattern id="grid" width="40" height="40" patternUnits="userSpaceOnUse"><path d="M 40 0 L 0 0 0 40" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="1"/></pattern></defs><rect width="100%" height="100%" fill="url(%23grid)" /></svg>');
      }
    }

    .article-tag {
      position: absolute;
      top: 0;
      left: 0;
      background: #D1AD79;
      border-radius: 0px 0px 0.625em 0px;
      padding: 0.2em 1.6em;
      min-width: 5.4em;
      min-height: 2.1em;
      display: flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      
      font-family: 'PingFang SC';
      font-weight: 600;
      font-size: 0.875em;
      line-height: 1.57;
      color: #FFFFFF;
    }
  }

  .article-content {
    padding: 5.2% 5.7% 5.7% 5.7%;
    flex: 1;
    display: flex;
    flex-direction: column;

    .article-title {
      font-family: 'PingFang SC';
      font-weight: 600;
      font-size: 1.25em;
      line-height: 1.4;
      color: #141F38;
      margin: 0 0 0.2em 0;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      line-clamp: 1;
      -webkit-box-orient: vertical;
    }

    .article-desc {
      font-family: 'PingFang SC';
      font-weight: 400;
      font-size: 0.875em;
      line-height: 1.57;
      color: rgba(0, 0, 0, 0.3);
      margin: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 4;
      line-clamp: 4;
      -webkit-box-orient: vertical;
    }

    .article-footer {
      margin-top: 1.25em;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .meta-tag {
        display: flex;
        align-items: center;
        gap: 0.25em;

        .el-icon {
          width: 1em;
          height: 1em;
          color: #FFFFFF;
        }

        span {
          font-family: 'PingFang SC';
          font-weight: 500;
          font-size: 0.875em;
          line-height: 1.57;
          color: #C62828;
        }
      }

      .view-count {
        font-family: 'PingFang SC';
        font-weight: 400;
        font-size: 0.875em;
        line-height: 1.57;
        color: rgba(0, 0, 0, 0.3);
      }
    }
  }
}
</style>

