<!-- 会员机构管理-子机构管理-子机构转移-新增 -->
<template>
    <app-drawer :title="t('account.tradeacct.sign.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('member.subinstitution.transfer.edit.legend') }}</legend>
                <el-form-item :label="t('member.subinstitution.transfer.edit.diversionoutlevel')" prop="diversionoutlevel">
                    <el-radio-group v-model="formData.diversionoutlevel" @change="switchOutType">
                        <template v-for="item in transferouttypeEnum.getEnumOptions()" :key="item.value">
                            <el-radio :label="item.label" :value="item.value" />
                        </template>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.outparentuserid')" prop="outparentuserid" v-if="formData.diversionoutlevel === 1">
                    <select-level v-model="formData.outparentuserid" :params="{ subarealevel: 1 }"
                        @change="selectOutUser" />
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.outparentuserid1')" prop="outparentuserid" v-else>
                    <select-level v-model="formData.outparentuserid" :params="{ subarealevel: 2 }"
                        @change="selectOutUser" />
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.cardtypeid')">
                    {{ outUserInfo ? certificatetypeEnum.getEnumTypeName(outUserInfo.cardtypeid) : handleNoneValue() }}
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.cardnum')">
                    {{ outUserInfo ? decryptAES(outUserInfo.cardnum) : handleNoneValue() }}
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('member.subinstitution.transfer.edit.legend1') }}</legend>
                <el-form-item :label="t('member.subinstitution.transfer.edit.transfermode')" prop="transfermode">
                    <el-select v-model="formData.transfermode">
                        <template v-for="item in transfermodeEnum.getEnumOptions()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.diversioninlevel')" prop="diversioninlevel">
                    <el-select v-model="formData.diversioninlevel" @change="switchInType">
                        <template v-for="item in transferintypeEnum.getEnumOptions()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.inuserid')" prop="inuserid" v-if="formData.diversioninlevel === 1">
                    <app-select-member v-model="formData.inuserid" :params="{ usertype: '2', roles: '7' }"
                        @change="selectInUser" />
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.inuserid1')" prop="inuserid" v-else>
                    <select-level v-model="formData.inuserid" :params="{ subarealevel: 1 }" @change="selectInUser" />
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.cardtypeid')">
                    {{ inUserInfo ? certificatetypeEnum.getEnumTypeName(inUserInfo.cardtypeid) : handleNoneValue() }}
                </el-form-item>
                <el-form-item :label="t('member.subinstitution.transfer.edit.cardnum')">
                    {{ inUserInfo ? decryptAES(inUserInfo.cardnum) : handleNoneValue() }}
                </el-form-item>
                <el-form-item class="el-form-item--row">
                    <span class="g-red">{{ t('member.subinstitution.transfer.edit.tips1') }}</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 { organSonTransferAdd } from '@/services/api/member'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import SelectLevel from './select-level/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

// 证件类型
const certificatetypeEnum = useEnum('certificatetype')
// 转出机构类型
const transferouttypeEnum = useEnum('transferouttype')
// 转入机构类型
const transferintypeEnum = useEnum('transferintype')
// 转移方式
const transfermodeEnum = useEnum('transfermode')

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

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

const formData = reactive<Partial<Member.OrganSonTransferAddReq>>({
    diversionoutlevel: 1,
    diversiontype: 2,
    transfermode: 1,
    diversioninlevel: 1
})

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

// 表单验证规则
const formRules: FormRules = {
    diversioninlevel: [{ required: true }],
    diversionoutlevel: [{ required: true }],
    inuserid: [{ required: true }],
    outparentuserid: [{ required: true }],
    transfermode: [{ required: true }]
}

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

// 切换转入类型
const switchInType = () => {
    formData.inuserid = undefined
    inUserInfo.value = undefined
}

// 选择转出机构
const selectOutUser = (item?: Member.SonOrganPageByLevelRsp) => {
    if (item) {
        getUserInfoByUserId({ userid: item.userid }).then((res) => {
            outUserInfo.value = res.data
        })
    } else {
        outUserInfo.value = undefined
    }
}

// 选择转入机构
const selectInUser = (item?: Member.SonOrganPageByLevelRsp | Model.OrganSelectRsp) => {
    if (item) {
        getUserInfoByUserId({ userid: item.userid }).then((res) => {
            inUserInfo.value = res.data
        })
    } else {
        inUserInfo.value = undefined
    }
}

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
            organSonTransferAdd({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>