<!-- 交易商管理-交易商管理-交易商批量转移-新增 -->
<template>
    <app-drawer :title="t('investor.manage.transfer.edit.title')" width="960" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--horizontal" label-width="140px" :model="formData" :rules="formRules"
            :show-message="false">
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('investor.manage.transfer.edit.legend') }}</legend>
                <el-form-item :label="t('investor.manage.transfer.edit.diversiontype')" prop="diversiontype">
                    <el-radio-group v-model="formData.diversiontype" @change="switchOutType">
                        <template v-for="item in investortransfertypeEnum.getEnumOptions()" :key="item.value">
                            <el-radio :label="item.label" :value="item.value" />
                        </template>
                    </el-radio-group>
                </el-form-item>
                <template v-if="formData.diversiontype === 1">
                    <el-form-item :label="t('investor.manage.transfer.edit.outinvestor')" prop="outinvestor">
                        <app-select-member v-model="formData.outinvestor" :params="{ usertype: '5' }"
                            @change="selectOutUser" />
                    </el-form-item>
                    <el-form-item :label="t('investor.manage.transfer.edit.memberUserName')">
                        {{ handleNoneValue(accountInfo?.memberUserName) }}
                    </el-form-item>
                    <el-form-item :label="t('investor.manage.transfer.edit.parentUserName')">
                        {{ handleNoneValue(accountInfo?.parentUserName) }}
                    </el-form-item>
                    <el-form-item :label="t('investor.manage.transfer.edit.cardtypeid')">
                        {{ outUserInfo ? certificatetypeEnum.getEnumTypeName(outUserInfo.cardtypeid) :
                            handleNoneValue() }}
                    </el-form-item>
                    <el-form-item :label="t('investor.manage.transfer.edit.cardnum')">
                        {{ outUserInfo ? decryptAES(outUserInfo.cardnum) : handleNoneValue() }}
                    </el-form-item>
                </template>
                <template v-else>
                    <el-form-item :label="t('investor.manage.transfer.edit.outmemberuserid')" prop="outmemberuserid">
                        <app-select-member v-model="formData.outmemberuserid" :params="{ usertype: '2', roles: '7' }"
                            @change="outParentUserRef.onReset()" />
                    </el-form-item>
                    <el-form-item :label="t('investor.manage.transfer.edit.outparentuserid')" prop="outparentuserid">
                        <app-select-member ref="outParentUserRef" v-model="formData.outparentuserid"
                            :params="{ usertype: '3', parentuserid: formData.outmemberuserid }"
                            :disabled="!formData.outmemberuserid" />
                    </el-form-item>
                </template>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('investor.manage.transfer.edit.legend1') }}</legend>
                <el-form-item :label="t('investor.manage.transfer.edit.inmemberuserid')" prop="inmemberuserid">
                    <app-select-member v-model="formData.inmemberuserid" :params="{ usertype: '2', roles: '7' }"
                        @change="selectInUser" />
                </el-form-item>
                <el-form-item :label="t('investor.manage.transfer.edit.inuserid')" prop="inuserid">
                    <app-select-member ref="inUserRef" v-model="formData.inuserid"
                        :params="{ usertype: '3', parentuserid: formData.inmemberuserid }"
                        :disabled="!formData.inmemberuserid" @change="selectInUser" />
                </el-form-item>
                <el-form-item :label="t('investor.manage.transfer.edit.cardtypeid1')">
                    {{ inUserInfo ? certificatetypeEnum.getEnumTypeName(inUserInfo.cardtypeid) : handleNoneValue() }}
                </el-form-item>
                <el-form-item :label="t('investor.manage.transfer.edit.cardnum1')">
                    {{ inUserInfo ? decryptAES(inUserInfo.cardnum) : handleNoneValue() }}
                </el-form-item>
                <el-form-item class="el-form-item--row">
                    <span class="g-red">{{ t('investor.manage.transfer.edit.tips') }}</span>
                </el-form-item>
            </fieldset>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.save') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, reactive, toRaw } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { decryptAES } from '@/services/crypto'
import { handleNoneValue } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryUserinfoByUserId } from '@/services/api/common'
import { investorTransferAdd, getUserAccountDetail } from '@/services/api/investor'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

// 证件类型
const certificatetypeEnum = useEnum('certificatetype')
// 转移类型
const investortransfertypeEnum = useEnum('investortransfertype')

const { global: { t } } = i18n
const formRef = shallowRef<FormInstance>()
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)

const outParentUserRef = shallowRef() // 转出所属会员组件实例
const inUserRef = shallowRef() // 转入所属机构组件实例

const accountInfo = shallowRef<Investor.UserAccountDetailRsp>()
const outUserInfo = shallowRef<Model.UserinfoByUserIdRsp>()
const inUserInfo = shallowRef<Model.UserinfoByUserIdRsp>()

const formData = reactive<Partial<Investor.InvestorTransferAddReq>>({
    diversiontype: 1,
})

const { runAsync: getUserInfoByUserId } = useRequest(queryUserinfoByUserId, {
    manual: true
})

const { runAsync: getAccountInfoByUserId } = useRequest(getUserAccountDetail, {
    manual: true
})

// 表单验证规则
const formRules: FormRules = {
    outinvestor: [{ required: true }],
    inmemberuserid: [{ required: true }],
    diversiontype: [{ required: true }],
    outmemberuserid: [{ required: true }]
}

// 切换转移类型
const switchOutType = () => {
    formData.outinvestor = undefined
    formData.outmemberuserid = undefined
    formData.outparentuserid = undefined
    accountInfo.value = undefined
    outUserInfo.value = undefined
}

// 选择转出交易商
const selectOutUser = (item?: Model.OrganSelectRsp) => {
    if (item) {
        const userid = item.userid
        getAccountInfoByUserId({ userid }).then((res) => {
            accountInfo.value = res.data
        })
        getUserInfoByUserId({ userid }).then((res) => {
            outUserInfo.value = res.data
        })
    } else {
        accountInfo.value = undefined
        outUserInfo.value = undefined
    }
}

// 选择转入机构
const selectInUser = (item?: Model.OrganSelectRsp) => {
    const userid = item?.userid || formData.inmemberuserid
    if (userid) {
        getUserInfoByUserId({ userid }).then((res) => {
            inUserInfo.value = res.data
        })
    } else {
        inUserInfo.value = undefined
    }
    if (item?.usertype === '2' || !formData.inmemberuserid) {
        inUserRef.value.onReset()
    }
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = toRaw(formData)
            loading.value = true
            investorTransferAdd({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>