<template>
    <app-pull-refresh ref="pullRefreshRef" v-model:loading="loading" v-model:error="error" v-model:pageIndex="pageIndex"
        :page-count="pageCount" @refresh="run" 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 { useRequest } from '@/hooks/request'
import { queryTHJPromotionIncome } from '@/services/api/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, showFailToast, 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 { onSubmit } = useTHJProfitDrawApplyReq()
const dataList = shallowRef<Model.THJPromotionIncomeRsp[]>([])
const pullRefreshRef = shallowRef()
const error = shallowRef(false)

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 { loading, pageIndex, pageCount, run } = useRequest(queryTHJPromotionIncome, {
    manual: true,
    params: {
        pagesize: 20,
        profitstatus: props.status,
    },
    onSuccess: (res) => {
        if (pageIndex.value === 1) {
            dataList.value = []
        }
        const data = res.data.map((e) => ({
            ...e,
            profitstatus: props.status ?? e.profitstatus
        }))
        dataList.value.push(...data)
    },
    onError: () => {
        error.value = true
    }
})

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

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

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