<!-- 交易商管理-个性化管理-交易商分组管理-分组交易商-编辑 -->
<template>
    <app-drawer :title="t('investor.custom.group.user.edit.title')" v-model:show="show" :refresh="refresh" :loading="loading">
        <el-transfer v-model="selectedValue" :data="dataList" :titles="[t('investor.custom.group.user.edit.accountname'), t('investor.custom.group.user.edit.userid')]"
            :props="{ label: 'accountname', key: 'userid' }">
            <template #default="{ option }">
                {{ option.userid }} -- {{ option.accountname }}
            </template>
        </el-transfer>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { queryInvestorForPer, addInvestorForPer } from '@/services/api/investor'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

const { global: { t } } = i18n
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)
const selectedValue = shallowRef([])

const { dataList } = useRequest(queryInvestorForPer, {
    params: {
        usergroupid: props.record.autoid
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    loading.value = true
    addInvestorForPer({
        data: {
            adduserids: selectedValue.value.join(','),
            usergroupid: props.record.autoid
        }
    }).then(() => {
        ElMessage.success(t('common.tips3'))
        onCancel(true)
    }).catch((err) => {
        ElMessage.error(t('common.tips4') + err)
    }).finally(() => {
        loading.value = false
    })
}
</script>

<style lang="less">
.el-transfer-panel {
    --el-transfer-panel-width: 260px;
    --el-transfer-panel-body-height: 400px;
}
</style>