<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="12">
					<a-form-item label="举报类型：" name="reportReasonType">
						<a-select
							v-model:value="formData.reportReasonType"
							show-search
							placeholder="所有分类"
							style="width: 200px"
							:options="typeOptions"
							:filter-option="filterOption"
							@change="handleChange"
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="举报原因：" name="reportDetail">
						<a-textarea v-model:value="formData.reportDetail" placeholder="请输入举报原因" :rows="4" />
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="证据图片：" name="evidenceScreenshot">
						<UpLoadImg ref="upLoadImgRef" @handlerUpImage="handlerUpImage"></UpLoadImg>
					</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([
		{
			label: '垃圾广告',
			value: 0
		},
		{
			label: '色情内容',
			value: 1
		},
		{
			label: '人身攻击',
			value: 2
		},
		{
			label: '整治敏感',
			value: 3
		},
		{
			label: '其他',
			value: 4
		}
	])
	const handleChange = (value) => {
		formData.value.reportReasonType = value
	}
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}
	const handlerUpImage = (id) => {
		formData.value.evidenceScreenshot = id
	}
	// 打开抽屉
	const onOpen = (record, module) => {
		moduleId.value = module
		visible.value = true
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		reportReasonType: [required('请选择举报类型')],
		reportDetail: [required('请输入举报原因')],
		evidenceScreenshot: [required('请上传证据图片')],
	}

	const categoryOptions = tool.dictList('MENU_TYPE')
	const visibleOptions = tool.dictList('MENU_VISIBLE')
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				forumApi
					.reportinfoAdd({
						...formData.value,
						postId: moduleId.value
					})
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.finally(() => {
				submitLoading.value = false
			})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
