<template>
  <app-view class="news-list" flex>
    <template #header>
      <app-navbar title="市场资讯" />
    </template>
    <Tabs v-model:active="currentColumnId" @change="onClickTab">
      <template v-for="(item, index) in siteColumns" :key="index">
        <Tab :name="item.id" :title="item.columnname" />
      </template>
    </Tabs>
    <app-pull-refresh ref="pullRefreshRef" class="news-list__container" v-model:error="error"
      v-model:pageIndex="pageIndex" :page-count="pageCount" @refresh="onRefresh">
      <Empty v-if="!dataList.length" />
      <template v-for="(item, index) in dataList" :key="index">
        <Cell class="article-item" title-class="article-item__title" value-class="article-item__time"
          :title="item.title" :value="formatDate(item.creaedate, 'MM/DD')"
          :to="{ name: 'news-details', params: { details: JSON.stringify(item) } }" />
      </template>
    </app-pull-refresh>
  </app-view>
</template>
  
<script lang="ts" setup>
import { shallowRef } from 'vue'
import { Cell, Tab, Tabs, Empty } from 'vant'
import { formatDate } from '@/filters'
import { useSiteColumnDetail } from '@/business/news'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

const { siteColumns, currentColumnId, pageIndex, pageCount, getSiteColumnDetail } = useSiteColumnDetail()
const pullRefreshRef = shallowRef()
const dataList = shallowRef<Model.SiteColumnDetailRsp[]>([])
const error = shallowRef(false)

// Tab触发
const onClickTab = () => {
  pullRefreshRef.value?.refresh()
}

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