<!-- 会员机构管理-机构管理-运营机构管理-修改 -->
<template>
    <app-drawer 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">机构</legend>
                <el-form-item label="机构" prop="userid">
                    <app-select-member v-model="formData.userid" :placeholder="t('investor.custom.riskcfg.edit.tips1')" :params="{ exists: '2' }" :disabled="!!record"/>
                </el-form-item>
                <el-form-item label="角色权限信息:" prop="userid" v-if="!record" >
                    <el-button @click="openComponent('Roles')">查看</el-button>
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--vertical">
                <legend class="g-fieldset__legend">商品查询权限</legend>
                <el-form-item v-for="item in data?.markets" v-bind:key="item.marketid" :label="item.marketname" prop="marketid" >
                    <el-checkbox-group v-model="formData.goodsids">
                        <el-checkbox v-for="goods in data?.goodsinfo.filter(e => e.marketid === item.marketid)" v-bind:key="goods.goodsid" 
                            :label="goods.goodsname" :value="goods.goodsid"/>
                    </el-checkbox-group>
                </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 }" :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, queryRoleDetail } from '@/services/api/member'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { useRequest } from '@/hooks/request'

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

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 formData = ref<Partial<Member.RoleAddReq>>({
    roleid: 2,
    type: props.record ? 2 : 1
})

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

useRequest(queryRoleDetail, {
    params: {
        rolename: 'operate',
        userid: props.record?.userid
    },
    onSuccess: (res) => {
        formData.value.goodsids = res.data.operategoodsinfo.map(e => e.goodsid)
        formData.value.marketids = res.data.markets.map(e => e.marketid)
    },
    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 onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

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