<!-- 交易商管理-个性化管理-交易商分组管理-分组交易商 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <template #header>
                <app-filter :options="filterOptons" />
            </template>
            <app-table :data="dataList" :columns="tableColumns">
                <template #headerLeft>
                    <span>{{ t('investor.custom.group.user.groupname') }} {{ record.groupname }}</span>
                </template>
                <template #headerRight>
                    <div style="display: flex;">
                        <el-button type="primary" @click="showComponent('Edit')">{{ t('operation.add') }}</el-button>
                        <el-button @click="emit('closed')">{{ t('operation.close') }}</el-button>
                    </div>
                </template>
                <!-- 操作 -->
                <template #operate="{ row }">
                    <el-button size="small" icon="Delete" @click="showComponent('Delete', row)" circle plain />
                </template>
                <template #footer>
                    <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                        @change="onSearch" />
                </template>
            </app-table>
            <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ record, selectedItem }"
                @closed="closeComponent" v-if="componentId" />
        </app-view>
    </teleport>
</template>

<script lang="ts" setup>
import { ref, PropType, defineAsyncComponent } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useDataFilter } from '@/hooks/datatable'
import { useComponent } from '@/hooks/component'
import { useRequest } from '@/hooks/request'
import { queryPersonalizedDetail } from '@/services/api/investor'
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'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const props = defineProps({
    record: {
        type: Object as PropType<Investor.PersonalizedRsp>,
        required: true
    }
})

const componentMap = new Map<string, unknown>([
    ['Edit', defineAsyncComponent(() => import('./edit/index.vue'))], // 编辑
    ['Delete', defineAsyncComponent(() => import('./delete/index.vue'))], // 删除
])

const emit = defineEmits(['closed'])
const selectedItem = ref<Investor.PersonalizedDetailRsp>()

const { filterOptons, getQueryParams } = useDataFilter<Investor.PersonalizedDetailReq>()

const { componentRef, componentId, openComponent, closeComponent } = useComponent(() => run())

const { dataList, total, pageSize, pageIndex, run } = useRequest(queryPersonalizedDetail, {
    params: {
        pageNum: 1,
        pageSize: 20,
        usergroupid: props.record.autoid
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'userid', label: 'investor.custom.group.user.userid' },
    { field: 'accountname', label: 'investor.custom.group.user.accountname' },
    { field: 'createtime', label: 'investor.custom.group.user.createtime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

filterOptons.inputList = [
    { label: t('investor.custom.group.user.accountname'), keys: ['accountname'] },
    { label: t('investor.custom.group.user.mobilephone'), keys: ['mobilephone'] },
    { label: t('investor.custom.group.user.cardnum'), keys: ['cardnum'] }
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

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

const showComponent = (code: string, row?: Investor.PersonalizedDetailRsp) => {
    selectedItem.value = row
    openComponent(code)
}
</script>