<template>
	<xn-form-container
		:title="formData.id ? '编辑帮助中心' : '增加帮助中心'"
		:width="1200"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="标题：" name="helpTitle">
				<a-input v-model:value="formData.helpTitle" placeholder="请输入标题" allow-clear />
			</a-form-item>
			<a-form-item label="内容：" name="helpContext">
				<div id="editor"></div>
<!--				<tinymce-editor v-model:value="formData.helpContent" :init="config" />-->
			</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="helpCenterForm">
	//import { ref, reactive, watch } from 'vue';
	//import { Form, message } from 'ant-design-vue';
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import helpCenterApi from '@/api/biz/helpCenterApi'

	import E from "wangeditor"

	//import TinymceEditor from '@/components/TinymceEditor/index.vue';
	//import { uploadFile } from '@/api/system/file';



	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const editor = ref(null)

	/*const config = ref({
		height: 525,
		images_upload_handler: (blobInfo, success, error) => {
			const file = blobInfo.blob();
			uploadFile(file, '2').then((res) => {
				success(API_FILE_PATH_URL + res);
			}).catch((e) => {
				error(e.message);
			});
		}
	});*/

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		nextTick(() => {
			editor.value = new E('#editor')
			// 设置图片上传接口地址
			editor.value.config.uploadImgServer = 'http://127.0.0.1:82/dev/file/uploadLocalReturnUrl'
			editor.value.config.customUploadImg = function (files, insertImgFn) {
				const formData = new FormData()
				formData.append('file', files[0])
				//其他参数根据需要自行添加 formData形式与JSON形式自行使用
				editor.value.config.uploadImgParams.file = files[0]
				const xhr = new XMLHttpRequest()
				xhr.open('POST', editor.value.config.uploadImgServer, false)
				xhr.onload = function (e) {
					if (xhr.status === 200) {
						const response = JSON.parse(xhr.responseText)
						insertImgFn(response.data) // 插入图片
					}
				}
				xhr.send(formData)
			}
			editor.value.create()
		})
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		helpTitle: [required('请输入标题')],
		helpContext: [required('请输入内容')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				helpCenterApi
					.helpCenterSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
