<!-- 查询管理-其它查询-资金流水查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #logType="{ item }">
                    <el-form-item :label="t('query.other.bank_statement.logType')">
                        <el-select v-model="item.value">
                            <el-option v-for="(option, i) in item.options?.()" :key="i" :label="option.label"
                                :value="option.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('query.other.bank_statement.date')" prop="date" v-if="item.value === 2">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" />
                    </el-form-item>
                </template>
                <template #accountId="{ item }">
                        <el-form-item :label="item.label" prop="accountId">
                            <app-select-account v-model="item.value" @change="onAccountChange" />
                        </el-form-item>
                    </template>
            </app-filter>
            <div class="report-account__info" v-if="selectedAccount">
                <span>{{ t('report.account.accountid1') }}{{ selectedAccount.relatedname }}</span>
                <span>{{ t('report.account.accountid') }}{{ selectedAccount.accountid }}</span>
                <span>{{ t('report.account.invloginids') }}{{ selectedAccount.invloginids }}</span>
            </div>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_other_bankstatement_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 { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryAmountStream } from '@/services/api/account'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppSelectAccount from '@pc/components/modules/select-account/index.vue'

const { global: { t } } = i18n
const selectedAccount = shallowRef<Model.TAAccountChildrenSelectRsp>()
const { getEnumOptions } = useEnum('operatetypesearch') // 业务类型

const dateValue = shallowRef<string[] | null>([])

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountName', label: 'query.other.bank_statement.accountName' },
    { field: 'accountId', label: 'query.other.bank_statement.accountId' },
    { field: 'memberName', label: 'query.other.bank_statement.memberName' },
    { field: 'amount', label: 'query.other.bank_statement.amount' },
    { field: 'businessCodeName', label: 'query.other.bank_statement.businessCodeName' },
    { field: 'operateType', label: 'query.other.bank_statement.operateType' },
    { field: 'marketName', label: 'query.other.bank_statement.marketName' },
    { field: 'relationOrderId', label: 'query.other.bank_statement.relationOrderId' },
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.AmountStreamReq>({
    filters: [
        {
            field: 'logType',
            label: t('query.other.bank_statement.logType'),
            value: 1,
            options: () => [
                { label: t('common.current'), value: 1 },
                { label: t('common.history'), value: 2 },
            ]
        },
        {
            field: 'operatetype',
            label: t('query.other.bank_statement.operatetype'),
            options: () => getEnumOptions()
        },
        {
            label: t('query.other.bank_statement.accountId'),
            field: 'accountId',
            placeholder: t('query.other.bank_statement.tips1'),
            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 onAccountChange = (item?: Model.TAAccountChildrenSelectRsp) => {
    selectedAccount.value = item
}

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

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

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

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