<!-- 会员机构管理-子机构管理-子机构变更审核-审核 -->
<template>
    <app-drawer title="修改" width="480" v-model:show="show" :refresh="refresh" :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules"
            :show-message="false">
            <el-form-item label="审核状态" prop="auditflag">
                <el-radio-group v-model="formData.auditflag">
                    <el-radio label="通过" :value="1" />
                    <el-radio label="不通过" :value="0" />
                </el-radio-group>
            </el-form-item>
            <el-form-item label="不通过原因" prop="msg">
                <el-input type="textarea" v-model="formData.msg" maxlength="200" :rows="3" placeholder="请输入" />
            </el-form-item>
        </el-form>
        <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, reactive, PropType, toRaw, computed } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { auditOrganSon, modifyAuditOrganSon } from '@/services/api/member'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'

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

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

const formData = reactive<Member.AuditOrganSonReq>({
    auditflag: 1,
    userid: props.record.userid
})

// 表单验证规则
const formRules = computed<FormRules>(() => ({
    auditflag: [{ required: true }],
    msg: [{ required: !formData.auditflag }]
}))

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

            if (props.record.modifystatus === 4) {
                modifyAuditOrganSon({
                    data: rawData
                }).then(() => {
                    ElMessage.success('审核成功')
                    onCancel(true)
                }).catch((err) => {
                    ElMessage.error('审核失败：' + err)
                    onCancel()
                }).finally(() => {
                    loading.value = false
                })
            } else {
                auditOrganSon({
                    data: rawData
                }).then(() => {
                    ElMessage.success('审核成功')
                    onCancel(true)
                }).catch((err) => {
                    ElMessage.error('审核失败：' + err)
                    onCancel()
                }).finally(() => {
                    loading.value = false
                })
            }
        }
    })
}
</script>