<template>
	<el-container>
		<el-main>
			<el-card>
				<pi-section title="邮箱配置"/>
				<el-form :model="eForm" :rules="eRules" ref="eFormRef"
						 label-width="110" label-position="right" size="default">
					<el-form-item label="发件人名称" prop="name">
						<el-input v-model="eForm.name" type="text" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="发件人邮箱" prop="username">
						<el-input v-model="eForm.username" type="text" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="SMTP密码" prop="password">
						<el-input v-model="eForm.password" type="text" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="SMTP服务器" prop="host">
						<el-input v-model="eForm.host" type="text" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="端口" prop="port">
						<el-input v-model="eForm.port" type="text" style="width: 30%"/>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" plain :loading="isSaving" @click="save('eForm')">保存</el-button>
					</el-form-item>
				</el-form>
				<pi-section title="钉钉配置"/>
				<el-form :model="dForm" :rules="dRules" ref="dFormRef"
						 label-width="110" label-position="right" size="default">
					<el-form-item label="名称" prop="name">
						<el-input v-model="dForm.name" type="text" placeholder="机器人名称或备注" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="URL" prop="url">
						<el-input v-model="dForm.url" type="textarea" placeholder="机器人URL" style="width: 30%"/>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" plain :loading="isSaving" @click="save('dForm')">保存</el-button>
					</el-form-item>
				</el-form>
				<pi-section title="飞书配置"/>
				<el-form :model="fForm" :rules="fRules" ref="fFormRef"
						 label-width="110" label-position="right" size="default">
					<el-form-item label="名称" prop="name">
						<el-input v-model="fForm.name" type="text" placeholder="机器人名称或备注" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="URL" prop="url">
						<el-input v-model="fForm.url" type="textarea" placeholder="机器人URL" style="width: 30%"/>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" plain :loading="isSaving" @click="save('fForm')">保存</el-button>
					</el-form-item>
				</el-form>
				<pi-section title="企业微信配置"/>
				<el-form :model="qForm" :rules="qRules" ref="qFormRef"
						 label-width="110" label-position="right" size="default">
					<el-form-item label="名称" prop="name">
						<el-input v-model="qForm.name" type="text" placeholder="机器人名称或备注" style="width: 30%"/>
					</el-form-item>
					<el-form-item label="URL" prop="url">
						<el-input v-model="qForm.url" type="textarea" placeholder="机器人URL" style="width: 30%"/>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" plain :loading="isSaving" @click="save('qForm')">保存</el-button>
					</el-form-item>
				</el-form>
			</el-card>
		</el-main>
	</el-container>
</template>

<script setup>
import piSection from "@/components/piSection"
import {getCurrentInstance, onMounted, ref} from "vue"
import api from "@/api"
import tools from "@/utils/tools"

const eFormRef = ref(null)
const dFormRef = ref(null)
const fFormRef = ref(null)
const qFormRef = ref(null)
const {proxy} = getCurrentInstance()
let isSaving = ref(false)
let eForm = ref({
	name: null,
	username: null,
	password: null,
	host: null,
	port: 465,
})
let dForm = ref({
	name: null,
	url: null
})
let fForm = ref({
	name: null,
	url: null
})
let qForm = ref({
	name: null,
	url: null
})
const eRules = ref({
	name: [
		{required: true, message: '请输入邮箱名称', trigger: 'blur'},
	],
	username: [
		{required: true, message: '请输入邮箱地址', trigger: 'blur'},
	],
	password: [
		{required: true, message: '请输入密码', trigger: 'blur'},
	],
	host: [
		{required: true, message: '请输入服务器地址', trigger: 'blur'},
	],
	port: [
		{required: true, message: '请输入端口号', trigger: 'blur'},
	],
})
const dRules = ref({
	name: [
		{required: true, message: '请输入名称', trigger: 'blur'},
	],
	url: [
		{required: true, message: '请输入机器人URL', trigger: 'blur'},
	],
})
const fRules = ref({
	name: [
		{required: true, message: '请输入名称', trigger: 'blur'},
	],
	url: [
		{required: true, message: '请输入机器人URL', trigger: 'blur'},
	],
})
const qRules = ref({
	name: [
		{required: true, message: '请输入名称', trigger: 'blur'},
	],
	url: [
		{required: true, message: '请输入机器人URL', trigger: 'blur'},
	],
})

onMounted(() => {
	loadData()
})

async function loadData() {
	const res = await api.system.config.multi({keys: ['email.config', 'qiwei.config', 'feishu.config', 'dingding.config']})
	if (res.data['email.config']) {
		eForm.value = JSON.parse(res.data['email.config'])
	}
	if (res.data['qiwei.config']) {
		qForm.value = JSON.parse(res.data['qiwei.config'])
	}
	if (res.data['feishu.config']) {
		fForm.value = JSON.parse(res.data['feishu.config'])
	}
	if (res.data['dingding.config']) {
		dForm.value = JSON.parse(res.data['dingding.config'])
	}
}

async function save(e) {
	let validate, res, err;
	switch (e) {
		case 'eForm':
			validate = await eFormRef.value.validate().catch(() => {
			});
			if (!validate) {
				return false
			}
			isSaving.value = true;
			[res, err] = await tools.go(api.system.config.save({'email.config': JSON.stringify(eForm.value)}))
			if (err) {
				proxy.$message.error(err.msg)
				return;
			}
			isSaving.value = false;
			proxy.$message.success(res.msg)
			break;
		case 'dForm':
			validate = await dFormRef.value.validate().catch(() => {
			});
			if (!validate) {
				return false
			}
			isSaving.value = true;
			[res, err] = await tools.go(api.system.config.save({'dingding.config': JSON.stringify(dForm.value)}))
			if (err) {
				proxy.$message.error(err.msg)
				return;
			}
			isSaving.value = false;
			proxy.$message.success(res.msg)
			break;
		case 'fForm':
			validate = await fFormRef.value.validate().catch(() => {
			});
			if (!validate) {
				return false
			}
			isSaving.value = true;
			[res, err] = await tools.go(api.system.config.save({'feishu.config': JSON.stringify(fForm.value)}))
			if (err) {
				proxy.$message.error(err.msg)
				return;
			}
			isSaving.value = false;
			proxy.$message.success(res.msg)
			break;
		case 'qForm':
			validate = await qFormRef.value.validate().catch(() => {
			});
			if (!validate) {
				return false
			}
			isSaving.value = true;
			[res, err] = await tools.go(api.system.config.save({'qiwei.config': JSON.stringify(qForm.value)}))
			if (err) {
				proxy.$message.error(err.msg)
				return;
			}
			isSaving.value = false;
			proxy.$message.success(res.msg)
			break;
	}
}
</script>

<style scoped>

</style>
