<!-- 报表查询-服务费分润报表 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #startDate>
                    <el-form-item :label="t('report.fee.date')" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('report.fee.startdate')" :end-placeholder="t('report.fee.enddate')" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['report_bank_export'])" @click="openComponentOnClick" />
            </template>
            <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="{ record, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref, shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { manageDivReportQuery } from '@/services/api/report'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'

const roleTypeEnum = useEnum('roleType') // 角色

const { global: { t } } = i18n
const dateValue = shallowRef<string[] | null>([])

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.ManageDivReq>({
    onClose: () => onSearch()
})

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradedate', label: 'report.fee.tradedate' },
    { field: 'areaname', label: 'report.fee.areaname' },
    { field: 'areatype', label: 'report.fee.areatype', formatValue: (val) => roleTypeEnum.getEnumTypeName(val) },
    { field: 'bankname', label: 'report.fee.bankname' },
    { field: 'totalcount', label: 'report.fee.totalcount' },
    { field: 'totalfee', label: 'report.fee.totalfee' }
])

// 表单验证规则
const filterRules: FormRules = {
    date: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.ManageDivReq>({
    filters: [
        {
            field: 'cusbankname',
            label: t('report.fee.cusbankname'),
            placeholder: t('report.fee.tips1'),
        },
        {
            field: 'startDate',
            required: true
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    const [startDate, endDate] = dateValue.value || []
    qs.startDate = startDate
    qs.endDate = endDate
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}


const onReset = () => {
    dateValue.value = []
    resetFilters()
}

const openComponentOnClick = (code: string) => {
    openComponent(code)
}
</script>