<template>
	<Card
		v-if="$team?.pg?.user === $session?.user"
		title="邮件通知"
		class="mx-auto max-w-3xl"
	>
		<template #actions>
			<Button icon-left="edit" @click="showEmailsEditDialog = true">
				编辑
			</Button>
		</template>
		<ListItem
			v-if="emailData"
			v-for="email in emailData"
			:title="fieldLabelMap[email.type] || email.type"
			:description="email.value"
			:key="email.type"
		>
		</ListItem>
		<Dialog
			:options="{
				title: '编辑邮件',
				actions: [
					{
						label: '保存更改',
						variant: 'solid',
						onClick: () => $resources.changeEmail.submit()
					}
				]
			}"
			v-model="showEmailsEditDialog"
		>
			<template v-slot:body-content>
				<div class="mt-3" v-for="email in emailData" :key="email.type">
					<FormControl
						:label="fieldLabelMap[email.type] || email.type"
						v-model="email.value"
					/>
				</div>
				<ErrorMessage class="mt-2" :message="$resources.changeEmail.error" />
			</template>
		</Dialog>
	</Card>
</template>

<script>
import { toast } from 'vue-sonner';
export default {
	name: 'AccountEmails',
	data() {
		return {
			showEmailsEditDialog: false,
			emailData: []
		};
	},
	resources: {
		getEmails() {
			return {
				url: 'jcloud.api.account.get_emails',
				auto: true,
				transform(data) {
					this.emailData = data.map(d => ({
						type: d.type,
						value: d.value
					}));
				}
			};
		},
		changeEmail() {
			return {
				url: 'jcloud.api.account.update_emails',
				params: {
					data: JSON.stringify(this.emailData)
				},
				onSuccess(res) {
					this.showEmailsEditDialog = false;
					toast.success('邮件更新成功');
				}
			};
		}
	},
	computed: {
		fieldLabelMap() {
			return {
				billing_email: '接收账单邮件地址',
				notify_email: '接收通知邮件地址'
			};
		}
	}
};
</script>