<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" @click="onClick">
            <!-- 日期 -->
            <template #profitmonth="{ value }">
                {{ formatDate(value, 'YYYY-MM') }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <Button type="primary" size="mini" @click.stop="submit(row)">申请支取</Button>
            </template>
        </app-list>
        <Empty v-if="!dataList.length"></Empty>
    </app-pull-refresh>
</template>

<script lang="ts" setup>
import { shallowRef, watch, computed } from 'vue'
import { formatDate } from '@/filters'
import { useQueryTHJPromotionIncome } from '@/business/order'
import { useTHJProfitDrawApplyReq } from '@/business/trade'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import AppList from '@mobile/components/base/list/index.vue'
import { Empty, Toast, Button } from 'vant'
import { useNavigation } from '@/hooks/navigation'
import { fullloading, dialog } from '@/utils/vant'

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

const { router } = useNavigation()
const { pageIndex, pageCount, getTHJPromotionIncome } = useQueryTHJPromotionIncome()
const { onSubmit } = useTHJProfitDrawApplyReq()
const dataList = shallowRef<Model.THJPromotionIncomeRsp[]>([])
const error = shallowRef(false)

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

const columns = computed<Model.TableColumn[]>(() => {
    const result = [
        { prop: 'profitmonth', label: '时间' },
        { prop: 'sumamount', label: '金额' },
    ]

    if (props.status === 1) {
        result.push({ prop: 'operate', label: '操作' })
    }

    return result
})

const onClick = (item: Model.THJPromotionIncomeRsp) => {
    router.push({ name: 'promotion-detail', query: { month: formatDate(item.profitmonth, 'YYYYMM') } })
}

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

const submit = (item: Model.THJPromotionIncomeRsp) => {
    dialog('确定要申请支取吗？', {
        showCancelButton: true
    }).then(() => {
        fullloading((hideLoading) => {
            onSubmit(item.profitmonth, item.sumamount).then(() => {
                hideLoading()
                dialog('请求成功，请耐心等待审核。').then(() => {
                    pullRefreshRef.value?.refresh()
                })
            }).catch((err) => {
                Toast.fail(err)
            })
        })
    })
}

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