<template>
	<xn-form-container
		:title="formData.recordId ? '编辑敏感词过滤记录' : '增加敏感词过滤记录'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="过滤词语：" name="sensitivityWord">
				<a-input v-model:value="formData.sensitivityWord" placeholder="请输入过滤词语" allow-clear />
			</a-form-item>
			<a-form-item label="帖子：" name="postId">
				<a-select
					v-model:value="formData.typeId"
					show-search
					placeholder="请选择帖子"
					style="width: 100%"
					:options="invitationList"
					:filter-option="filterOption"
				></a-select>
			</a-form-item>
			<a-form-item label="过滤类型：" name="recordType">
				<a-select
					v-model:value="formData.recordType"
					show-search
					placeholder="请选择类型"
					style="width: 100%"
					:options="typeList"
					:filter-option="filterOption"
				></a-select>
			</a-form-item>
			<a-form-item label="用户：" name="userId">
				<a-select
					v-model:value="formData.userId"
					show-search
					placeholder="请选择用户"
					style="width: 100%"
					:options="usertypeOptions"
					:filter-option="filterOption"
				></a-select>
			</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="forumSensitivityRecordForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import forumSensitivityRecordApi from '@/api/forum/forumSensitivityRecordApi'
	import forumApi from '@/api/forum/forumApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	//用户
	const usertypeOptions = ref([])
	//帖子
	const invitationList = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		forumApi.allUserList().then((data) => {
			usertypeOptions.value = data.map((r) => {
				return {
					label: r.name,
					value: r.id,
					...r
				}
			})
		})
		forumApi
			.forumList({
				current: 1,
				size: 999999999,
				sortOrder: 0
			})
			.then((data) => {
				invitationList.value = data.records.map((r) => {
					return {
						label: r.postTitle,
						value: r.postId,
						...r
					}
				})
			})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	//类型
	const typeList = ref([
		{
			label: '发帖',
			value: 0
		},
		{
			label: '回复',
			value: 1
		}
	])
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			forumSensitivityRecordApi
				.forumSensitivityRecordSubmitForm(formDataParam, formDataParam.recordId)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
