<template>
    <xn-form-container
        :title="formData.id ? '编辑t_question' : '增加t_question'"
        :width="700"
        :visible="visible"
        :destroy-on-close="true"
        @close="onClose"
    >
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="QUESTION_TYPE：" name="questionType">
                <a-input v-model:value="formData.questionType" placeholder="请输入QUESTION_TYPE" allow-clear />
            </a-form-item>
            <a-form-item label="SUBJECT_ID：" name="subjectId">
                <a-input v-model:value="formData.subjectId" placeholder="请输入SUBJECT_ID" allow-clear />
            </a-form-item>
            <a-form-item label="SCORE：" name="score">
                <a-input v-model:value="formData.score" placeholder="请输入SCORE" allow-clear />
            </a-form-item>
            <a-form-item label="GRADE_LEVEL：" name="gradeLevel">
                <a-input v-model:value="formData.gradeLevel" placeholder="请输入GRADE_LEVEL" allow-clear />
            </a-form-item>
            <a-form-item label="DIFFICULT：" name="difficult">
                <a-input v-model:value="formData.difficult" placeholder="请输入DIFFICULT" allow-clear />
            </a-form-item>
            <a-form-item label="CORRECT：" name="correct">
                <a-input v-model:value="formData.correct" placeholder="请输入CORRECT" allow-clear />
            </a-form-item>
            <a-form-item label="INFO_TEXT_CONTENT_ID：" name="infoTextContentId">
                <a-input v-model:value="formData.infoTextContentId" placeholder="请输入INFO_TEXT_CONTENT_ID" allow-clear />
            </a-form-item>
            <a-form-item label="STATUS：" name="status">
                <a-input v-model:value="formData.status" placeholder="请输入STATUS" allow-clear />
            </a-form-item>
            <a-form-item label="DELETED：" name="deleted">
                <a-input v-model:value="formData.deleted" placeholder="请输入DELETED" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
        </template>
    </xn-form-container>
</template>

<script setup name="tQuestionForm">
    import { cloneDeep } from 'lodash-es'
    import { required } from '@/utils/formRules'
    import tQuestionApi from '@/api/question/tQuestionApi'
    // 抽屉状态
    const visible = ref(false)
    const emit = defineEmits({ successful: null })
    const formRef = ref()
    // 表单数据
    const formData = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (record) => {
        visible.value = true
        if (record) {
            let recordData = cloneDeep(record)
            formData.value = Object.assign({}, recordData)
        }
    }
    // 关闭抽屉
    const onClose = () => {
        formRef.value.resetFields()
        formData.value = {}
        visible.value = false
    }
    // 默认要校验的
    const formRules = {
    }
    // 验证并提交数据
    const onSubmit = () => {
        formRef.value.validate().then(() => {
            submitLoading.value = true
            const formDataParam = cloneDeep(formData.value)
            tQuestionApi
                .tQuestionSubmitForm(formDataParam, formDataParam.id)
                .then(() => {
                    onClose()
                    emit('successful')
                })
                .finally(() => {
                    submitLoading.value = false
                })
        })
    }
    // 抛出函数
    defineExpose({
        onOpen
    })
</script>
