<template>
	<xn-form-container
		:title="formData.postId ? '编辑帖子信息表' : '增加帖子信息表'"
		: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="typeId">
				<a-select
					v-model:value="formData.typeId"
					show-search
					placeholder="请选择分类"
					style="width: 100%"
					:options="typeOptions"
					: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-item label="帖子标题：" name="postTitle">
				<a-input v-model:value="formData.postTitle" placeholder="请输入帖子标题" allow-clear />
			</a-form-item>
			<a-form-item label="帖子内容：" name="postContent">
				<xn-editor v-model="formData.postContent" placeholder="请输入内容" :height="400"></xn-editor>
			</a-form-item>
			<!-- <a-form-item label="是否置顶：" name="isTop">
				<a-select
					v-model:value="formData.isTop"
					placeholder="请选择是否置顶"
					style="width: 100%"
					:options="topOptions"
					:filter-option="filterOption"
				></a-select>
			</a-form-item>
			<a-form-item label="浏览次数：" name="viewCount">
				<a-input v-model:value="formData.viewCount" placeholder="请输入浏览次数" allow-clear />
			</a-form-item>
			<a-form-item label="回复次数：" name="replyCount">
				<a-input v-model:value="formData.replyCount" placeholder="请输入回复次数" allow-clear />
			</a-form-item>
			<a-form-item label="点赞次数：" name="likeCount">
				<a-input v-model:value="formData.likeCount" placeholder="请输入点赞次数" allow-clear />
			</a-form-item> -->

			<a-form-item label="帖子类型：" name="postType">
				<a-select
					v-model:value="formData.postType"
					placeholder="请选择类型"
					style="width: 100%"
					:options="invitationOptions"
					@change="selectType"
				></a-select>
			</a-form-item>
			<a-form-item label="纠错类型：" name="contentCorrectionParam.resourceType" v-if="formData.postType==2">
				<a-select
					v-model:value="formData.contentCorrectionParam.resourceType"
					placeholder="请选择类型"
					style="width: 100%"
					:options="errorType"
					@change="selectErrorType"
				></a-select>
			</a-form-item>
			<a-form-item label="选择目标：" name="contentCorrectionParam.resourceId" v-if="formData.postType==2">
				<a-select
					v-model:value="formData.contentCorrectionParam.resourceId"
					placeholder="请选择类型"
					style="width: 100%"
					:options="targetList"
				></a-select>
			</a-form-item>

			<a-form-item label="定向用户：" name="appointUserArr">
				<a-select
					v-model:value="formData.appointUserArr"
					show-search
					mode="multiple"
					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="forumPostInfoForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import forumPostInfoApi from '@/api/forum/forumPostInfoApi'
	import forumApi from '@/api/forum/forumApi'
	import XnEditor from '@/components/Editor/index.vue'
	import Bowser from 'bowser'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		contentCorrectionParam:{}
	})
	const submitLoading = ref(false)
	//分类
	const typeOptions = ref([])
	//用户
	const usertypeOptions = ref([])
	//纠错
	const errorType = ref([
		{
			label: '课程章节',
			value: 0
		},
		{
			label: '作业',
			value: 1
		},
		{
			label: '考试',
			value: 2
		}
	])
	//置顶
	const topOptions = ref([
		{
			label: '普通',
			value: 0
		},
		{
			label: '置顶',
			value: 1
		}
	])
	//帖子类型
	const invitationOptions = ref([
		{
			label: '普通帖子',
			value: 0
		},
		{
			label: '技术支持',
			value: 1
		},
		// {
		// 	label: '内容纠错',
		// 	value: 2
		// }
	])
	// 打开抽屉
	const onOpen = (record) => {
		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 (record) {
			if (record.appointUser) {
				record.appointUserArr = record.appointUser.split(',')
			}
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			if (formData.value.postType == 1) {
				formData.value = Object.assign(formData.value, browserObj.value)
			}
			const formDataParam = cloneDeep(formData.value)
			forumPostInfoApi
				.forumPostInfoSubmitForm(formDataParam, formDataParam.postId)
				.then(() => {
					if (formData.value.appointUserArr && formData.value.appointUserArr.length > 1) {
						formData.value.appointUser = formData.value.appointUserArr.join(',')
					}
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	//选择类型
	const selectType = (event) => {
		if (event == 0) {
			browserObj.value = {}
			errorVal.value = {}
		}
		if (event == 1) {
			getBower()
		}
	}
	const targetList = ref([])
	const selectErrorType = (event) => {
		if (event == 0) {
			
		}
		if (event == 1) {
			
		}
		if (event == 2) {
			
		}
	}
	//获取操作系统/浏览器等信息
	const browserObj = ref({})
	function getBower() {
		const browser = Bowser.getParser(window.navigator.userAgent)
		let obj = browser.parsedResult
		browserObj.value = {
			forumSupportEnvParam: {
				browserType: obj.browser.name,
				browserVersion: obj.browser.version,
				osType: obj.os.name,
				osVersion: obj.os.version,
				deviceType: 0
			}
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
