<!-- 报表查询-银行资金报表 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption"  :rules="filterRules">
                <template #startDate>
                    <el-form-item :label="t('report.bank.date')" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('report.bank.startdate')" :end-placeholder="t('report.bank.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 { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { cusbankReportQuery } 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 dateValue = shallowRef<string[] | null>([])
const { global: { t } } = i18n

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradedate', label: 'report.bank.tradedate' },
    { field: 'cusbankname', label: 'report.bank.cusbankname' },
    { field: 'balance', label: 'report.bank.balance' },
    { field: 'totalinamout', label: 'report.bank.totalinamout' },
    { field: 'inamoutcout', label: 'report.bank.inamoutcout' },
    { field: 'totalincharge', label: 'report.bank.totalincharge' },
    { field: 'totaloutamount', label: 'report.bank.totaloutamount' },
    { field: 'outamountcout', label: 'report.bank.outamountcout' },
    { field: 'totaloutcharge', label: 'report.bank.totaloutcharge' },
    { field: 'currentbalance', label: 'report.bank.currentbalance' },
    { field: 'totalpl', label: 'report.bank.totalpl' },
    { field: 'totalcharge', label: 'report.bank.totalcharge' },
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.CusbankReportReq>({
    filters: [
        {
            field: 'cusbankname',
            label: t('report.bank.cusbankname'),
            placeholder: t('report.bank.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) => {
    queryParams.value = processRequiredParams()
    openComponent(code)
}
</script>