<template>
    <app-view class="bank-statement">
        <template #header>
            <app-navbar title="资金流水">
                <template #right>
                    <Button round @click="routerTo('bank-hisstatement')">更多</Button>
                </template>
            </app-navbar>
        </template>
        <app-pull-refresh v-model:error="error" v-model:pageIndex="pageIndex" :page-count="pageCount"
            @refresh="onRefresh">
            <app-list class="bank-statement__table" :columns="columns" :data-list="dataList">
                <template #createtime="{ value }">
                    <span>{{ formatDate(value, 'YYYY-MM-DD') }}</span>
                    <span>{{ formatDate(value, 'HH:mm:ss') }}</span>
                </template>
                <template #businesscode="{ value }">
                    {{ getAccountBusinessCodeName(value) }}
                </template>
            </app-list>
        </app-pull-refresh>
    </app-view>
</template>

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

const { routerTo } = useNavigation()
const { pageIndex, pageCount, getAmountStatementList } = useAmountStatementList()
const dataList = shallowRef<Model.AmountLogRsp[]>([])
const error = shallowRef(false)

const columns: Model.TableColumn[] = [
    { prop: 'createtime', label: '时间' },
    { prop: 'businesscode', label: '操作类型' },
    { prop: 'amount', label: '金额' },
]

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

<style lang="less">
.bank-statement {
    &__table {
        td.app-list__column {
            &:first-child {
                span:last-child {
                    color: #999;
                    font-size: .24rem;
                }
            }

            &:not(:first-child) {
                font-size: .32rem;
            }
        }
    }
}
</style>