<!-- 交易商管理-交易商管理-交易商管理-审核-操作 -->
<template>
    <app-drawer :title="t('investor.manage.modification.aduit.operate.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">
            <el-form-item :label="t('investor.manage.modification.aduit.operate.auditflag')" prop="auditflag">
                <el-radio-group v-model="formData.auditflag">
                    <el-radio :value="1">{{ t('investor.manage.modification.aduit.operate.auditflag1') }}</el-radio>
                    <el-radio :value="0">{{ t('investor.manage.modification.aduit.operate.auditflag2') }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('investor.manage.modification.aduit.operate.msg')" prop="msg">
                <el-input type="textarea" v-model="formData.msg" :rows="3" :disabled="!!formData.auditflag"
                    maxlength="200" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, toRaw } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { modifyAudit } from '@/services/api/investor'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

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

const formData = ref<Investor.ModifyAuditReq>({
    auditflag: 1,
    userid: props.record.userid,
})

// 表单验证规则
const formRules: FormRules = {
    msg: [{
        required: true,
        validator: (rule, value, callback) => {
            if (formData.value.auditflag || formData.value.msg) {
                callback()
            } else {
                callback(new Error(t('investor.manage.modification.aduit.operate.tips1')))
            }
        },
    }],
}

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

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