<template>
  <div class="store-page">
    <Breadcrumb />

    <userCard type="goods" class="user-card-wrap" />

    <a-alert v-if="notice" type="info" closable class="notice">{{ notice }}</a-alert>

    <header class="page-header">
      <div>
        <h1 class="store-section-title">云商城</h1>
        <p class="store-section-desc">选购乐跑次数套餐，支付后自动到账</p>
      </div>
      <div class="store-search-wrap">
        <a-input-search
          v-model="keyword"
          placeholder="搜索商品名称"
          allow-clear
          class="store-search"
          @search="getGoods"
          @clear="getGoods"
        />
      </div>
    </header>

    <a-skeleton animation :loading="loading" class="store-spin goods-spin">
      <StoreGridSkeleton :count="6" />
      <template #content>
        <a-empty v-if="filteredData.length === 0" description="暂无商品" />
        <div v-else class="goods-grid">
        <article
          v-for="item in filteredData"
          :key="item.id"
          class="goods-card"
          @click="goDetail(item.id)"
        >
          <div class="goods-card__visual">
            <GoodsEmoji :icon="item.icon" size="lg" :aria-label="item.name" />
            <a-tag v-if="item.isHot" color="orangered" size="small" class="goods-card__hot">热销</a-tag>
          </div>
          <div class="goods-card__body">
            <h3 class="goods-card__name">{{ item.name }}</h3>
            <p v-if="item.description" class="goods-card__desc">{{ item.description }}</p>
            <ul v-if="parseFeatures(item.features).length" class="goods-card__features">
              <li v-for="(f, i) in parseFeatures(item.features).slice(0, 3)" :key="i">{{ f }}</li>
            </ul>
            <div class="goods-card__footer">
              <span class="goods-card__price">¥{{ item.price }}</span>
              <a-tag size="small" :color="item.num > 0 ? 'green' : 'red'">
                库存 {{ stockLabel(item.num) }}
              </a-tag>
            </div>
          </div>
          <a-button type="primary" class="goods-card__btn" @click.stop="goDetail(item.id)">
            立即选购
          </a-button>
        </article>
        </div>
      </template>
    </a-skeleton>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getGoodsList } from '@/api/goods'
import { Notification } from '@arco-design/web-vue'
import userCard from '@/components/userCard/userCard.vue'
import GoodsEmoji from '@/components/store/GoodsEmoji.vue'
import StoreGridSkeleton from '@/components/skeleton/StoreGridSkeleton.vue'
import { getNotice } from '@/utils/util'
import { stockLabel, parseFeatures } from '@/utils/storeFormat'

const router = useRouter()
const notice = ref('')
const data = ref([])
const loading = ref(false)
const keyword = ref('')

const filteredData = computed(() => {
  const k = keyword.value.trim().toLowerCase()
  if (!k) return data.value
  return data.value.filter((item) => (item.name ?? '').toLowerCase().includes(k))
})

const goDetail = (id) => router.push(`/store/goodsDetail/${id}`)

const getGoods = async () => {
  try {
    loading.value = true
    const res = await getGoodsList()
    if (!res || res.code !== 0) {
      return Notification.error({
        title: '获取商品列表失败',
        content: res?.msg ?? '请稍后再试'
      })
    }
    data.value = res.data ?? []
  } catch (error) {
    Notification.error({
      title: '获取商品列表失败',
      content: error.message || '请稍后再试'
    })
  } finally {
    loading.value = false
  }
}

const GetNotice = async () => {
  const { path } = useRoute()
  notice.value = await getNotice(path)
}

getGoods()
GetNotice()
</script>

<style lang="less" scoped>
@import '@/styles/store-theme.less';

.user-card-wrap {
  margin-bottom: 20px;
}

.notice {
  margin-top: 16px;
  border-radius: @store-radius-sm;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-top: 16px;
  margin-bottom: 24px;
}

.store-search-wrap {
  width: min(300px, 100%);
  flex-shrink: 0;
}

.goods-spin {
  min-height: 200px;
}

.goods-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.goods-card {
  background: @store-card-bg;
  border: 1px solid @store-card-border;
  border-radius: @store-radius;
  box-shadow: @store-shadow;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  display: flex;
  flex-direction: column;

  &:hover {
    transform: translateY(-4px);
    box-shadow: @store-shadow-hover;
  }

  &__visual {
    position: relative;
    height: 140px;
    background: @store-cover-bg;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &__hot {
    position: absolute;
    top: 12px;
    right: 12px;
  }

  &__body {
    padding: 16px 16px 8px;
    flex: 1;
  }

  &__name {
    margin: 0 0 8px;
    font-size: 1.1rem;
    font-weight: 600;
    color: @store-primary;
    line-height: 1.4;
  }

  &__desc {
    margin: 0 0 8px;
    font-size: 0.85rem;
    color: @store-text-muted;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  &__features {
    margin: 0 0 12px;
    padding-left: 18px;
    font-size: 0.8rem;
    color: @store-text-muted;

    li {
      margin-bottom: 2px;
    }
  }

  &__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  &__price {
    font-size: 1.35rem;
    font-weight: 700;
    color: @store-price;
  }

  &__btn {
    margin: 0 16px 16px;
    border-radius: 999px;
    background: @store-primary !important;
    border-color: @store-primary !important;

    &:hover {
      background: @store-primary-light !important;
    }
  }
}
</style>
