<template>
	<xn-form-container
		:title="formData.id ? '编辑主题' : '增加主题'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-alert
			class="mb-3"
			message="欢迎来到论坛 — 衷心感谢你参与讨论！
			标题是否清晰明了地描述了主题？看
			起来有意思么？
			谁会感兴趣？
			它为什么重要？
			你期望从社区中获得什么样的回应？
			选择常用的词句以便别人能找到你的主题。若想要给主题和类似主题分组，选择一个分类。"
			type="warning"
		/>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="标题：" name="postTitle">
						<a-input v-model:value="formData.postTitle" placeholder="请输入标题" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="分类：" name="typeId">
						<a-select
							v-model:value="formData.typeId"
							show-search
							placeholder="请选择分类"
							style="width: 200px"
							:options="typeOptions"
							:filter-option="filterOption"
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="指向：" name="appointUserArr">
						<a-select
							v-model:value="formData.appointUserArr"
							mode="multiple"
							show-search
							placeholder="请选择"
							style="width: 200px"
							:options="usertypeOptions"
							:filter-option="filterOption"
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="内容：" name="postContent">
						<xn-editor v-model="formData.postContent" 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 userInfo = tool.data.get('USER_INFO')
	const typeOptions = ref([])
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const treeData = ref([])
	// 表单数据，也就是默认给一些数据
	const formData = ref({
		postType: 0
	})
	// 默认展开的节点(顶级)
	const defaultExpandedKeys = ref([0])
	const submitLoading = ref(false)
	// 模块ID
	const moduleId = ref('')
	const usertypeOptions = ref([])
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}

	// 打开抽屉
	const onOpen = (record, module) => {
		moduleId.value = module
		visible.value = true
		forumApi.forumTypeList().then((data) => {
			typeOptions.value = data.map((r) => {
				return {
					label: r.typeName,
					value: r.typeId,
					...r
				}
			})
		})
		forumApi.allUserList().then((data)=>{
			usertypeOptions.value = data.map(r=>{
				return{
					label:r.name,
					value:r.id,
					...r
				}
			})
		})
		if (module) {
			record.appointUserArr = record.appointUser.split(',')
			formData.value = Object.assign(formData.value, record)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		postTitle: [required('请输入标题')],
		typeId: [required('请选择分类')],
		postContent: [required('请输入内容')]
	}

	const categoryOptions = tool.dictList('MENU_TYPE')
	const visibleOptions = tool.dictList('MENU_VISIBLE')
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				formData.value.appointUser = formData.value.appointUserArr.join(',')
				forumApi.submitForm(formData.value, formData.value.postId).then(() => {
					onClose()
					emit('successful')
				})
			})
			.finally(() => {
				submitLoading.value = false
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
