<template>
	<a-drawer
		:title="formData.id ? '编辑推送订阅' : '增加推送订阅'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="通知类型：" name="logType">
				<a-select v-model:value="formData.logType" placeholder="请选择" :options="logTypeOptions"/>
			</a-form-item>
			<a-form-item label="通知角色：" name="logRoles">
				<a-checkbox-group v-model:value="formData.logRoles" :options="logRolesOptions"/>
			</a-form-item>
			<a-form-item label="订阅事件：" name="subscribesFlag">
				<a-radio-group v-model:value="formData.subscribesFlag" placeholder="请选择" :options="subscribesFlagOptions"/>
			</a-form-item>
		</a-form>
		<br>
		<p>※ 1. 根据通知类型和通知角色发送通知到公众号推送</p>
		<p>※ 2. 接收通知的人员需要先登陆小程序并点击'绑定微信'，然后关注严格粮业公众号</p>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="sysUserWxOpenSendSetForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import sysUserWxOpenSendSetApi from '@/api/biz/sysUserWxOpenSendSetApi'
import tool from "@/utils/tool";
import dictOptionsApi from "@/api/biz/dictOptionsApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
const logRolesOptions = ref([])
// 表单数据
const formData = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		recordData.logRoles = JSON.parse(recordData.logRoles)
		formData.value = Object.assign({}, recordData)
	} else {
		formData.value.subscribesFlag = 'subscribe'
	}
	dictOptionsApi.options(Object.assign('', {'dictValue': '业务角色'})).then((data) => {
		logRolesOptions.value = data
	})
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.logRoles = JSON.stringify(formDataParam.logRoles)
			sysUserWxOpenSendSetApi
				.sysUserWxOpenSendSetSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
const logTypeOptions = tool.dictList('异常分类')
const subscribesFlagOptions = tool.dictList('订阅类型')
// 抛出函数
defineExpose({
	onOpen
})
</script>
