<template>
	<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
		<a-form-item label="发送方式：" name="type">
			<a-radio-group v-model:value="formData.type">
				<a-radio :value="1">纯文本</a-radio>
				<a-radio :value="2">HTML</a-radio>
				<a-radio :value="3">模板邮件</a-radio>
			</a-radio-group>
		</a-form-item>
		<a-form-item label="接收人邮箱：" name="receiveAccounts">
			<a-textarea
				v-model:value="formData.receiveAccounts"
				:placeholder="type !== 3 ? '请输入接收人邮箱，多个逗号拼接' : '预先创建且上传了收件人的收件人列表名称'"
				:auto-size="{ minRows: 2, maxRows: 5 }"
			/>
		</a-form-item>
		<a-form-item label="邮件主题：" name="subject" v-if="type !== 3">
			<a-input v-model:value="formData.subject" placeholder="请输入邮件主题" allow-clear/>
		</a-form-item>
		<a-form-item label="邮件正文" name="content" v-if="type === 1">
			<a-textarea
				v-model:value="formData.content"
				placeholder="请输入邮件正文"
				:auto-size="{ minRows: 6, maxRows: 6 }"
			/>
		</a-form-item>
		<a-form-item label="邮件正文" name="content" v-if="type === 2">
			<xn-editor v-model="formData.content" placeholder="请输入邮件正文" :height="200"></xn-editor>
		</a-form-item>
		<a-form-item label="模板名：" name="templateName" v-if="type === 3">
			<a-input v-model:value="formData.templateName" placeholder="请输入预先创建且通过审核的模板名称"
					 allow-clear/>
		</a-form-item>
		<a-form-item label="标签名：" name="tagName" v-if="type === 3">
			<a-input v-model:value="formData.tagName" placeholder="请输入标签名" allow-clear/>
		</a-form-item>
		<a-form-item label="发件人昵称：" name="sendUser" v-if="type !== 3">
			<a-input v-model:value="formData.sendUser" placeholder="设置发件人昵称" allow-clear/>
		</a-form-item>
	</a-form>
</template>

<script setup name="aliyunEmailSend">
import {message} from 'ant-design-vue'
import XnEditor from '@/components/Editor/index.vue'
import {required, rules} from '@/utils/formRules'
import emailApi from '@/api/sys/dev/emailApi'
import fileApi from '@/api/sys/dev/fileApi'

const uploadRef = ref()
// 发送文本方式
const type = ref(1)
// 定义emit事件
const emit = defineEmits({loadingStart: null, loadingEnd: null})
const formRef = ref()
// 表单数据
const formData = ref({engine: 2,type:1})

// 默认要校验的
const formRules = {
	receiveAccounts: [required('请输入接收人邮箱，多个逗号拼接')],
	subject: [required('请输入邮件主题')],
	templateName: [required('请输入预先创建且通过审核的模板名称')],
	content: [required('请输入邮件正文')]
}

// 发送
const send = () => {
	formRef.value.validate().then(() => {
		emit('loadingStart')
		emailApi
			.emailSend(formData.value)
			.then(() => {
				message.success('发送成功')
			})
			.finally(() => {
				emit('loadingEnd')
			})
	})
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
	send
})
</script>
