<template>
    <app-view class="wrtradedetail">
        <app-pull-refresh v-model:pageIndex="pageIndex" :page-count="pageCount" v-model:error="error"
            @refresh="onRefresh">
            <app-list :columns="columns" :data-list="dataList" @click="onClick">
                <!-- 方向 -->
                <template #buyorsell="{ value }">
                    {{ getBuyOrSellName(value) }}
                </template>
                <!-- 成交时间 -->
                <template #tradetime="{ value }">
                    <span>{{ formatDate(value, 'YYYY-MM-DD') }}</span>
                    <span>{{ formatDate(value, 'HH:mm:ss') }}</span>
                </template>
            </app-list>
            <!-- <Button @click="routerTo('')">更多</Button> -->
        </app-pull-refresh>
    </app-view>
</template>

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

const { router } = useNavigation()
const { pageIndex, pageCount, getWrTradeDetail } = useQueryWrTradeDetail()
const dataList = shallowRef<Model.WrTradeDetailRsp[]>([])
const error = shallowRef(false)

const onClick = (item: Model.THJPurchaseTradeDetailRsp) => {
    router.push({ name: 'wrtrade-detail', params: { item: JSON.stringify(item) } })
}

const columns: Model.TableColumn[] = [
    { prop: 'wrstandardname', label: '商品' },
    { prop: 'buyorsell', label: '方向' },
    { prop: 'tradeprice', label: '成交价格' },
    { prop: 'orderqty', label: '成交数量' },
    { prop: 'tradeqty', label: '成交金额' },
    { prop: 'tradetime', label: '成交时间' },
]

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