<template>
    <app-view class="promotion-detail" flex>
        <template #header>
            <app-navbar title="推广收益明细" />
        </template>
        <app-pull-refresh class="promotion-detail__container" v-model:error="error" v-model:pageIndex="pageIndex"
            :page-count="pageCount" @refresh="onRefresh">
            <app-list :columns="columns" :data-list="dataList" v-if="dataList.length">
                <!-- 时间 -->
                <template #tradetime="{ value }">
                    <span>{{ formatDate(value, 'YYYY-MM-DD') }}</span>
                    <span>{{ formatDate(value, 'HH:mm:ss') }}</span>
                </template>
            </app-list>
            <Empty v-if="!dataList.length"></Empty>
        </app-pull-refresh>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { formatDate } from '@/filters'
import { useNavigation } from '@/hooks/navigation'
import { useQueryTHJPromotionIncomeDetail } from '@/business/order'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import AppList from '@mobile/components/base/list/index.vue'
import { Empty } from 'vant'

const { getQueryString } = useNavigation()
const { pageIndex, pageCount, getTHJPromotionIncomeDetail } = useQueryTHJPromotionIncomeDetail(getQueryString('month'))
const dataList = shallowRef<Model.THJPromotionIncomeDetailRsp[]>([])
const error = shallowRef(false)

const columns: Model.TableColumn[] = [
    { prop: 'tradetime', label: '时间' },
    { prop: 'wrstandardname', label: '商品' },
    { prop: 'market', label: '市场' },
    { prop: 'buyorsell', label: '方向' },
    { prop: 'profitroletype', label: '级别' },
    { prop: 'profitamount', label: '金额' },
]

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