<!-- 结算管理-分润付款确认 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" showIndex v-model:columns="tableColumns" :loading="loading">
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ selectedRow, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { sharedayquery } from '@/services/api/settlement'
import { useOperation } from '@/hooks/operation'
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 queryParams = shallowRef<Model.ShareDayQueryReq>()
const { filterOptons, getQueryParams } = useDataFilter<Model.ShareDayQueryReq>()

const { componentMap, componentId, selectedRow, closeComponent } = useOperation<Model.ShareDayQueryReq>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(sharedayquery, {
    params: {
        pageNum: 1,
        pageSize: 20,
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'tradedate', label: '交易日' },
    { field: 'totalcount', label: '总笔数	' },
    { field: 'divideamount', label: '总分润金额	' },
    { field: 'totalpay', label: '总已付	' },
    { field: 'operate', label: '操作' }
])

filterOptons.buttonList = [
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() },
    { lable: '重置', className: 'el-button--primary', onClick: () => onSearch(true) }
]

// 处理请求参数
const processRequiredParams = (callback: (params: Model.ShareDayQueryReq) => void, clear = false) => {
    const qs = getQueryParams(clear)
    queryParams.value = qs
    callback(qs)
}

const onSearch = (clear = false) => {
    processRequiredParams((qs) => run(qs), clear)
}

</script>