<template>
	<xn-form-container title="回复" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="内容：" name="replyContent">
						<xn-editor v-model="formData.replyContent" placeholder="请输入" :height="400"></xn-editor>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import SnowflakeId from 'snowflake-id'
	import tool from '@/utils/tool'
	import forumApi from '@/api/forum/forumApi'
	import XnEditor from '@/components/Editor/index.vue'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const treeData = ref([])
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	// 默认展开的节点(顶级)
	const submitLoading = ref(false)
	// 模块ID
	const moduleId = ref('')
	//回复ID
	const replyid = ref('')
	//类型
	const typeOptions = ref([])
	const handleChange = (value) => {
		searchFormState.typeId = value
		table.value.refresh(true)
	}
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}

	// 打开抽屉
	const onOpen = (record, module, childId, formType) => {
		moduleId.value = module
		replyid.value = childId ?? '-1'
		visible.value = true
		formData.value.formType = formType
		if (formType) {
			formData.value.replyContent = record.replyContent
		} else {
			formData.value.replyContent = ''
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		replyContent: [required('请输入内容')]
	}

	const categoryOptions = tool.dictList('MENU_TYPE')
	const visibleOptions = tool.dictList('MENU_VISIBLE')
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				let params = {}
				if (formData.value.formType) {
					params = {
						...formData.value,
						replyId: replyid.value
					}
				} else {
					params = {
						...formData.value,
						postId: moduleId.value,
						parentId: replyid.value
					}
				}

				forumApi.submitPostreply(params, formData.value.formType).then(() => {
					onClose()
					emit('successful')
				})
			})
			.finally(() => {
				submitLoading.value = false
			})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
