<template>
  <div
    class="market-card"
    @mouseenter="isHovered = true"
    @mouseleave="isHovered = false"
  >
    <!-- 封面图 -->
    <div class="market-card-cover" @click.stop.prevent="goWorkflowDetail">
      <img :src="info.coverImage" :alt="info.workflowTitle" class="market-cover-img" />
      <div class="market-cover-overlay"></div>
      <div class="market-platform-chip">{{ info.categoryName1 }}</div>
      <!-- <div class="market-views">▶ {{ info.downCount || 0 }}</div> -->
    </div>

    <!-- 卡片内容 -->
    <div class="market-card-body">
      <div class="market-card-top">
        <h4 class="market-title" @click.stop.prevent="goWorkflowDetail">{{ info.workflowTitle }}</h4>
      </div>
      <p class="market-desc">{{ info.description }}</p>

      <div class="market-footer">
        <div class="market-author">
          <el-avatar :size="22" :src="info.userAvatar || appStore.avatarDefault" />
          <span>{{ info.nickName }}</span>
        </div>
        <button class="market-use-btn" @click.stop.prevent="startUsing">{{ $t('marketplace.cta.use') }} →</button>
      </div>
    </div>

    <!-- 悬停遮罩 -->
    <div class="market-hover-overlay" :class="{ visible: isHovered }">
      <button class="overlay-btn primary" @click.stop.prevent="startUsing">{{ $t('marketplace.cta.use2') }}</button>
      <button class="overlay-btn secondary" @click.stop.prevent="goWorkflowDetail">{{ $t('marketplace.cta.details') }}</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useAppStore } from '@/pinia/appStore'
import { inject } from 'vue'
import { isLogin, openNewTab } from '@/utils/util.js'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'

const { t } = useI18n()
const appStore = useAppStore()
const router = useRouter()
const openLoginDialog = inject('openLoginDialog')

const props = defineProps({
  info: {
    type: Object,
    default: () => ({})
  },
  fromPage: {
    type: String,
    default: () => ''
  }
})

const isHovered = ref(false)

const goWorkflowDetail = () => {
  if (!isLogin({ callback: openLoginDialog, t })) {
    return
  }

  let path = `/search-platform/workflow-detail`
  if (props.fromPage === 'home') {
    path = `/workflow-detail`
  }

  router.push({
    path: path,
    query: {
      publishId: props.info.publishId,
      metaTitle: 'route.WorkflowDetail'
    }
  })
}

const startUsing = () => {
  if (!isLogin({ callback: openLoginDialog, t })) {
    return
  }

  openNewTab(props.info.categoryUrl)
}
</script>

<style scoped lang="scss">
.market-card {
  background: white;
  border-radius: 16px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
  position: relative;
  cursor: pointer;

  &:hover {
    transform: translateY(-6px);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.12);
  }

  &.hidden {
    opacity: 0.3;
    transform: scale(0.95);
  }
}

/* 封面图 */
.market-card-cover {
  position: relative;
  height: 160px;
  overflow: hidden;
}

.market-cover-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.4s;
}

.market-card:hover .market-cover-img {
  transform: scale(1.05);
}

.market-cover-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 40%, rgba(0, 0, 0, 0.4));
}

.market-platform-chip {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 3px 10px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 700;
  color: white;
  background: linear-gradient(135deg, #6366F1, #8B5CF6);
}

.market-views {
  position: absolute;
  bottom: 10px;
  right: 10px;
  color: white;
  font-size: 12px;
  font-weight: 600;
}

/* 卡片内容 */
.market-card-body {
  padding: 16px;
}

.market-card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.market-title {
  font-size: 14px;
  font-weight: 700;
  color: #111827;
  flex: 1;
  margin: 0;
  line-height: 1.3;

  &:hover {
    text-decoration: underline;
  }
}

.market-rating {
  font-size: 12px;
  color: #F59E0B;
  font-weight: 600;
  white-space: nowrap;
}

.market-desc {
  font-size: 12px;
  color: #6B7280;
  line-height: 1.5;
  margin: 0 0 10px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 36px;
}

.market-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 12px;
}

.market-tag {
  padding: 2px 8px;
  background: #F3F4F6;
  border-radius: 100px;
  font-size: 10px;
  color: #6B7280;
}

.market-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.market-author {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #6B7280;
  overflow: hidden;
  flex-shrink: 0;
  max-width: 50%;

  span {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

.market-avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.market-use-btn {
  padding: 5px 12px;
  background: #F5F3FF;
  color: #6366F1;
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;

  &:hover {
    background: #6366F1;
    color: white;
  }
}

/* 悬停遮罩 */
.market-hover-overlay {
  position: absolute;
  inset: 0;
  background: rgba(99, 102, 241, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 10;

  &.visible {
    opacity: 1;
  }
}

.overlay-btn {
  width: 160px;
  padding: 10px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all 0.2s;

  &.primary {
    background: white;
    color: #6366F1;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(255, 255, 255, 0.3);
    }
  }

  &.secondary {
    background: rgba(255, 255, 255, 0.15);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);

    &:hover {
      background: rgba(255, 255, 255, 0.25);
      transform: translateY(-2px);
    }
  }
}
</style>
