<template>
    <app-view class="credit-statement">
        <template #header>
            <app-navbar title="积分流水">
            </app-navbar>
        </template>
        <app-pull-refresh v-model:error="error" v-model:pageIndex="pageIndex" :page-count="pageCount"
            @refresh="onRefresh">
            <app-list class="credit-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 #scoreconfigtype="{ value }">
                    {{ getScoreConfigTypeName(value) }}
                </template>
            </app-list>
        </app-pull-refresh>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { formatDate } from '@/filters'
import { getScoreConfigTypeName } from '@/constants/credit'
import { useCreditStatementList } from '@/business/credit'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import AppList from '@mobile/components/base/list/index.vue'

const { pageIndex, pageCount, getCreditStatementList } = useCreditStatementList()
const dataList = shallowRef<Model.UserScoreLogRsp[]>([])
const error = shallowRef(false)

const columns: Model.TableColumn[] = [
    { prop: 'createtime', label: '时间' },
    { prop: 'scoreconfigtype', label: '操作类型' },
    { prop: 'score', label: '积分' },
]

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

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

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