<!-- 会员机构管理-机构管理-营销中心管理-新增/修改 -->
<template>
    <app-drawer :title="t('member.institution.marketing.edit.title')" width="900" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="140px" :model="formData" :rules="formRules" :show-message="false">
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('member.institution.marketing.edit.userid') }}</legend>
                <el-form-item :label="t('member.institution.marketing.edit.userid')" prop="userid">
                    <app-select-member v-model="formData.userid" :placeholder="t('investor.custom.riskcfg.edit.tips1')" :params="{ exists: '3' }"
                      :disabled="!!record" @change="onMemberChange"/>
                </el-form-item>
                <el-form-item :label="t('member.institution.marketing.edit.userid1')" prop="userid" v-if="!record" >
                    <el-button @click="openComponent('Roles')">{{ t('member.institution.marketing.edit.look') }}</el-button>
                    <span>{{ manger }}</span>
                </el-form-item>
            </fieldset>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.save') }}</el-button>
        </template>
        <component ref="componentRef" v-bind="{ record, roleid: 3 }" :is="componentMap.get(componentId)" @closed="closeComponent" v-if="componentId" />
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, ref, defineAsyncComponent, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useComponent } from '@/hooks/component'
import { i18n } from '@/stores'
import { roleadd, initAdd, getOragnDefaultManager } from '@/services/api/member'
import { useRequest } from '@/hooks/request'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    record: {
        type: Object as PropType<Member.OrganRoleRsp>
    }
})

const formData = ref<Partial<Member.RoleAddReq>>({
    roleid: 3,
    type: props.record ? 2 : 1
})

const componentMap = new Map<string, unknown>([
    ['Roles', defineAsyncComponent(() => import('@pc/components/modules/roles/index.vue'))], // 角色权限信息
])
const { componentRef, componentId, openComponent, closeComponent } = useComponent()

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const show = shallowRef(true)
const refresh = shallowRef(false)
const organSelect = ref<Model.OrganSelectRsp>()

const { loading } = useRequest(initAdd, {
    params: {
        role: 'marketing'
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const { data: manger, run: runDefaultManager } = useRequest(getOragnDefaultManager, {
    manual: true,
    params: {
        userid: formData?.value.userid?.toString()
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    userid: [{ required: true }]
}

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

const onMemberChange = (item?: Model.OrganSelectRsp) => {
    organSelect.value = item

    const { userid } = formData.value
    if (userid) {
        runDefaultManager({
            userid: userid.toString()
        })
    }
}

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

onMounted(() => {
    const { userid, } = props.record ?? {}
    if (userid) {
        formData.value.userid = userid
    }
})
</script>