<template>
  <app-view class="supply-demand">
    <template #header>
      <app-navbar class="supply-demand__header" title="供求" :show-back-button="false">
        <template #footer>
          <Search shape="round" placeholder="商品搜索" disabled />
        </template>
      </app-navbar>
    </template>
    <app-pull-refresh class="supply-demand__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: 'SupplyDemandDetail', params: { item: JSON.stringify(item) } })">
          <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, Search } from 'vant'
import { getImageUrl } from '@/filters'
import { useOrderQuoteList } from '@/business/goods'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

const { pageIndex, pageCount, getOrderQuoteList } = useOrderQuoteList()
const dataList = shallowRef<Model.OrderQuoteRsp[]>([])
const error = shallowRef(false)

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

const onRefresh = (finish: () => void) => {
  getOrderQuoteList().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>