<!-- 会员中心-资金流水 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading">
                <template #before>
                    <el-select v-model="isHistory" :disabled="loading" @change="onRefresh">
                        <el-option :value="false" label="当前" />
                        <el-option :value="true" label="历史" />
                    </el-select>
                </template>
                <template #after v-if="isHistory">
                    <el-date-picker type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="YYYYMMDD" v-model="filterDate" />
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <!-- 时间 -->
            <template #createtime="{ value }">
                {{ formatDate(value) }}
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useDataTable } from '@/hooks/datatable'
import { useDataFilter } from '@/hooks/datatable'
import { queryAmountLog, queryHisAmountLog } from '@/services/api/bank'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'

const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.AmountLogRsp | Ermcp.HisAmountLogRsp>()
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.AmountLogReq | Ermcp.HisAmountLogReq>()
const loading = shallowRef(false)
const isHistory = shallowRef(false) // 是否历史流水
const filterDate = shallowRef<string[]>([])

const tableColumns = shallowRef<Ermcp.TableColumn[]>([
    { prop: 'createtime', label: '时间' },
    { prop: 'operatetypename', label: '操作类型' },
    { prop: 'amount', label: '金额' },
])

// 获取数据列表
const getDataList = async (params: Partial<(Ermcp.AmountLogReq | Ermcp.HisAmountLogReq)> = {}) => {
    loading.value = true
    dataList.value = []

    const payload: Ermcp.AmountLogReq | Ermcp.HisAmountLogReq = {
        page: pageIndex.value,
        pagesize: pageSize.value,
        ...params
    }

    if (isHistory.value) {
        const [startDate, endDate] = filterDate.value || []
        filterOptons.buttonList = [
            { lable: '重置', onClick: () => onSearch(true) },
            { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
        ]

        queryHisAmountLog({
            ...payload,
            startDate,
            endDate
        }).then((res) => {
            total.value = res.total
            dataList.value = res.data
        }).catch((err) => {
            ElMessage.error(err)
        }).finally(() => {
            loading.value = false
        })
    } else {
        filterOptons.buttonList = []

        queryAmountLog(payload).then((res) => {
            total.value = res.total
            dataList.value = res.data
        }).catch((err) => {
            ElMessage.error(err)
        }).finally(() => {
            loading.value = false
        })
    }
}

const onSearch = (clear = false) => {
    if (clear) {
        filterDate.value = []
    }
    getQueryParams((qs) => {
        pageIndex.value = 1
        getDataList(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => {
        getDataList(qs).catch((err) => ElMessage.error(err))
    })
}

onRefresh()
</script>