<template>
    <app-pull-refresh ref="pullRefreshRef" v-model:error="error" v-model:pageIndex="pageIndex" :page-count="pageCount"
        @refresh="onRefresh" disabled style="height:auto">
        <app-list :columns="columns" :data-list="dataList" v-if="dataList.length">
            <!-- 日期 -->
            <template #reckondate="{ value }">
                {{ formatDate(value, 'YYYY-MM-DD') }}
            </template>
            <!-- 市场 -->
            <template #marketid="{ value }">
                {{ getMarketName(value) }}
            </template>
            <!-- 吨数 -->
            <template #tradeqty="{ row }">
                {{ `${row.buytradeqty}/${row.selltradeqty}` }}
            </template>
            <!-- 笔数 -->
            <template #tradelot="{ row }">
                {{ `${row.buytradelot}/${row.selltradelot}` }}
            </template>
        </app-list>
        <Empty v-if="!dataList.length"></Empty>
    </app-pull-refresh>
</template>

<script lang="ts" setup>
import { shallowRef, watch } from 'vue'
import { formatDate } from '@/filters'
import { useQueryTHJTradeDataList } from '@/business/goods'
import { getMarketName } from '@/constants/market'
import { Empty } from 'vant'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import AppList from '@mobile/components/base/list/index.vue'

const props = defineProps({
    marketid: {
        type: Number,
        required: true
    }
})

const { pageIndex, pageCount, getQueryTHJTradeDataList } = useQueryTHJTradeDataList()
const dataList = shallowRef<Model.THJTradeDataRsp[]>([])
const error = shallowRef(false)

const columns: Model.TableColumn[] = [
    { prop: 'reckondate', label: '日期' },
    { prop: 'marketid', label: '市场' },
    { prop: 'wrstandardname', label: '商品' },
    { prop: 'accountname', label: '好友' },
    { prop: 'tradeqty', label: '吨数' },
    { prop: 'tradelot', label: '笔数' },
]

/// 下拉刷新
const pullRefreshRef = shallowRef()

const onRefresh = (finish: () => void) => {
    /// 查询数据
    getQueryTHJTradeDataList(props.marketid).then((res) => {
        if (pageIndex.value === 1) {
            dataList.value = []
        }
        dataList.value.push(...res)
    }).catch(() => {
        error.value = true
    }).finally(() => {
        finish()
    })
}

watch(() => props.marketid, () => {
    pullRefreshRef.value?.refresh()
})
</script>