<template>
  <app-view class="product">
    <template #header>
      <app-navbar title="产品介绍" />
    </template>
    <app-pull-refresh class="product__container" v-model:loading="loading" v-model:error="error"
      v-model:pageIndex="pageIndex" :page-count="pageCount" @refresh="run">
      <ul class="list">
        <li class="list-item g-block--bg" v-for="(item, index) in dataList" :key="index">
          <div class="block">
            <img :src="getFirstImage(item.thumurls)" />
            <span>{{ item.wrstandardname }}</span>
          </div>
          <div class="block">
            <Button type="primary" size="small" round
              @click="$router.push({ name: 'product-detail', query: { favorite: 0, wrstandardid: item.wrstandardid } })">查看详情</Button>
          </div>
        </li>
      </ul>
    </app-pull-refresh>
  </app-view>
</template>
  
<script lang="ts" setup>
import { shallowRef } from 'vue'
import { Button } from 'vant'
import { getFileUrl } from '@/filters'
import { useRequest } from '@/hooks/request'
import { queryTHJProduct } from '@/services/api/goods'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

const dataList = shallowRef<Model.THJProductRsp[]>([])
const error = shallowRef(false)

const { loading, pageIndex, pageCount, run } = useRequest(queryTHJProduct, {
  manual: true,
  params: {
    pagesize: 20,
    favoriteflag: false,
  },
  onSuccess: (res) => {
    if (pageIndex.value === 1) {
      dataList.value = []
    }
    dataList.value.push(...res.data)
  },
  onError: () => {
    error.value = true
  }
})

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

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