<!-- 会员机构管理-机构管理-风控个性化设置 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['member_institution_riskcfg_add'])"
                    @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small"
                    :data-list="getActionButtons(['member_institution_riskcfg_details', 'member_institution_riskcfg_modify', 'member_institution_riskcfg_delete'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </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 }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { useEnum } from '@/hooks/enum'
import { queryAccountRiskConfig } from '@/services/api/member'
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 { global: { t } } = i18n

// 风险率类型
const marketerTypeEnum = useEnum('marketerType')

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'userid', label: 'member.institution.riskcfg.userid' },
    { field: 'username', label: 'member.institution.riskcfg.username' },
    { field: 'accountid', label: 'member.institution.riskcfg.accountid' },
    { field: 'customertype', label: 'member.institution.riskcfg.customertype', formatValue: (val) => marketerTypeEnum.getEnumTypeName(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Member.AccountRiskConfigReq>({
    filters: [
        {
            field: 'username',
            label: t('member.institution.riskcfg.memberuserid')
        },
        {
            field: 'accountid',
            label: t('member.institution.riskcfg.accountid')
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理请求参数
const onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>