<template>
	<xn-form-container
		:title="formData.id ? '编辑帖子' : '增加帖子'"
		:height="550"
		:get-container="false"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
		placement="bottom"
		:mask="false"
		style="width: 960px; left: calc(50% - 960px / 2)"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="8">
					<a-form-item label="标题：" name="postTitle">
						<a-input v-model:value="formData.postTitle" placeholder="请输入标题" max allow-clear show-count :maxlength="100" />
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="分类：" name="typeId">
						<a-select
							v-model:value="formData.typeId"
							placeholder="请选择分类"
							style="width: 100%"
							:options="typeOptions"
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="指向：" name="appointUserArr">
						<a-select
							v-model:value="formData.appointUserArr"
							mode="multiple"
							show-search
							placeholder="请选择"
							style="width: 100%"
							:options="usertypeOptions"
							:filter-option="filterOption"
						></a-select>
						<!-- @popupScroll="popupScroll" -->
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="内容：" name="postContent">
						<xn-editor v-model="formData.postContent" placeholder="请输入内容" :height="300"></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.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}
	const userPagination = ref({
		size: 99999999,
		current: 1
	})
	const userTotal = ref(0)
	//获取用户
	const getUserAllList = (add) => {
		forumApi.allUserList(userPagination.value).then((res) => {
			userTotal.value = res.total
			let userList = res.records.map((r) => {
				return {
					label: `${r.name}-${r.eduIdentityName??''}`,
					value: r.id,
					...r
				}
			})
			usertypeOptions.value = add ? [...usertypeOptions.value, ...userList] : userList
		})
	}
	const popupScroll = (el) => {
		let offset = 0
		if (el.target.scrollHeight - el.target.scrollTop - offset <= el.target.clientHeight) {
			if (userPagination.value.size * userPagination.value.current < userTotal.value) {
				userPagination.value.current += 1
				getUserAllList(true)
			}
		}
	}
	// 打开抽屉
	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
				}
			})
		})
		getUserAllList()
		if (module) {
			if (record?.appointUser) {
				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
				if (formData.value.appointUserArr && formData.value.appointUserArr.length > 1) {
					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>
