<script setup>

import useUtils from "@/utils/utils.js"
import {inject, ref} from "vue"

const utils = useUtils()

const Tea = inject("$Tea")

const props = defineProps(["v-message"])

const {message, params} = (function () {
	let paramsJSON = props.vMessage.params
	let params = null
	if (paramsJSON != null && paramsJSON.length > 0) {
		params = JSON.parse(paramsJSON)
	}

	return {
		message: ref(props.vMessage),
		params: ref(params)
	}
})()

const viewCert = function (certId) {
	utils.popup("/servers/certs/certPopup?certId=" + certId, {
		width: "48em"
	})
}

const readMessage = function (messageId) {
	Tea.action("/messages/readPage")
		.params({"messageIds": [messageId]})
		.post()
		.success(function () {
			utils.reload()
		})
}

</script>

<template>
	<div>
		<table class="ui table selectable">
			<tr :class="{error: message.level == 'error', positive: message.level == 'success', warning: message.level == 'warning'}">
				<td style="position: relative">
					<strong>{{ message.datetime }}</strong>
					<span v-if="message.cluster != null && message.cluster.id != null">
				<span> | </span>
				<a :href="'/clusters/cluster?clusterId=' + message.cluster.id">集群：{{ message.cluster.name }}</a>
			</span>
					<span v-if="message.node != null && message.node.id != null">
				<span> | </span>
				<a :href="'/clusters/cluster/node?clusterId=' + message.cluster.id + '&nodeId=' + message.node.id">节点：{{ message.node.name }}</a>
			</span>
					<a href="" style="position: absolute; right: 1em" @click.prevent="readMessage(message.id)" title="标为已读"><i class="icon check"></i></a>
				</td>
			</tr>
			<tr :class="{error: message.level == 'error', positive: message.level == 'success', warning: message.level == 'warning'}">
				<td>
					<pre style="padding: 0; margin:0; word-break: break-all;">{{ message.body }}</pre>

					<!-- 健康检查 -->
					<div v-if="message.type == 'HealthCheckFailed'" class="mt-2">
						<a :href="'/clusters/cluster/node?clusterId=' + message.cluster.id + '&nodeId=' + param.node.id" v-for="param in params">{{ param.node.name }}: {{ param.error }}</a>
					</div>

					<!-- 集群DNS设置 -->
					<div v-if="message.type == 'ClusterDNSSyncFailed'" class="mt-2">
						<a :href="'/dns/clusters/cluster?clusterId=' + message.cluster.id">查看问题 &raquo;</a>
					</div>

					<!-- 证书即将过期 -->
					<div v-if="message.type == 'SSLCertExpiring'" class="mt-2">
						<a href="" @click.prevent="viewCert(params.certId)">查看证书</a><span v-if="params != null && params.acmeTaskId > 0"> &nbsp;|&nbsp; <a :href="'/servers/certs/acme'">查看任务&raquo;</a></span>
					</div>

					<!-- 证书续期成功 -->
					<div v-if="message.type == 'SSLCertACMETaskSuccess'" class="mt-2">
						<a href="" @click.prevent="viewCert(params.certId)">查看证书</a> &nbsp;|&nbsp;
						<a :href="'/servers/certs/acme'" v-if="params != null && params.acmeTaskId > 0">查看任务&raquo;</a>
					</div>
					<div v-if="message.type == 'SSLCertACMETaskFailed'" class="mt-2">
						<a href="" @click.prevent="viewCert(params.certId)">查看证书</a> &nbsp;|&nbsp;
						<a :href="'/servers/certs/acme'" v-if="params != null && params.acmeTaskId > 0">查看任务&raquo;</a>
					</div>

					<!-- 网站审核通过 -->
					<div v-if="message.type == 'ServerNamesAuditingSuccess'" class="mt-2">
						<a :href="'/servers/server?serverId=' + params.serverId">去查看&raquo;</a>
					</div>

					<!-- 网站审核失败 -->
					<div v-if="message.type == 'ServerNamesAuditingFailed'" class="mt-2">
						<a :href="'/servers/server?serverId=' + params.serverId">去查看&raquo;</a>
					</div>
				</td>
			</tr>
		</table>
		<div class="margin"></div>
	</div>
</template>

<style scoped>
</style>
