<template>
  <app-view class="product">
    <template #header>
      <app-navbar title="产品介绍" />
    </template>
    <app-pull-refresh class="product__container" v-model:error="error" v-model:pageIndex="pageIndex"
      :page-count="pageCount" @refresh="onRefresh">
      <template v-for="(item, index) in dataList" :key="index">
        <Cell
          @click="$router.push({ name: 'product-detail', query: { favorite: active, wrstandardid: item.wrstandardid } })">
          <template #title>
            <img :src="getFirstImage(item.thumurls)" />
            <span>{{ item.wrstandardname }}</span>
          </template>
        </Cell>
      </template>
    </app-pull-refresh>
  </app-view>
</template>
  
<script lang="ts" setup>
import { shallowRef } from 'vue'
import { Cell } from 'vant'
import { getImageUrl } from '@/filters'
import { useQueryTHJProductLists } from '@/business/goods'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

const { pageIndex, pageCount, getQueryTHJProductLists, active } = useQueryTHJProductLists()
const dataList = shallowRef<Model.THJProductRsp[]>([])
const error = shallowRef(false)

// 获取产品首图
const getFirstImage = (url: string) => {
  const images = url.split(',').map((path) => getImageUrl(path))
  return images[0] ?? ''
}

const onRefresh = (finish: () => void) => {
  /// 获取产品列表
  getQueryTHJProductLists().then((res) => {
    if (pageIndex.value === 1) {
      dataList.value = []
    }
    dataList.value.push(...res)
  }).catch(() => {
    error.value = true
  }).finally(() => {
    finish()
  })
}

</script>

<style lang="less">
@import './index.less';
</style>