<script setup>
// Copyright 2024 FlexCDN root@flexcdn.cn. All rights reserved. Official site: https://flexcdn.cn .

import ctxRef from "@/utils/ctx_ref.js"
import Context from "./index.js"
import Menu from "./@menu.vue"
import TMessage from "@/components/ui/TMessage.vue";
import WarningMessage from "@/components/common/WarningMessage.vue";
import ServerTrafficLimitStatusViewer from "@/components/server/ServerTrafficLimitStatusViewer.vue";
import Pager from "@/components/common/Pager.vue";
import ListTable from "@/components/common/ListTable.vue";
import Column from "primevue/column";
import TLabel from "@/components/ui/TLabel.vue";
import NotFoundInfo from "@/components/common/NotFoundInfo.vue";

const ctx = ctxRef(new Context())

</script>

<template>
	<Menu active-tab="index"></Menu>

	<TMessage error="" v-if="!ctx.serversIsEnabled">
		当前账号下的网站不可用，请检查账号状态以及是否有逾期未支付的账单。
	</TMessage>

	<WarningMessage v-if="ctx.serversIsEnabled && ctx.countUnpaidBills > 0">
		<a href="/finance/bills?paidFlag=0">有逾期未支付的账单，可能会影响你的CDN服务正常运行，请尽快完成支付。</a>
	</WarningMessage>

	<NotFoundInfo disable-spacing v-if="ctx.servers.length == 0">暂时还没有任何负载均衡服务。</NotFoundInfo>

	<div v-if="ctx.servers.length > 0">
		<ListTable :items="ctx.servers">
			<Column header="网站名称" class="name-column" frozen>
				<template #body="{data:server}">
					<a :href="'/lb/server?serverId=' + server.id">{{server.name}}</a>

					<div v-if="server.userPlan != null && server.userPlan.id > 0">
						<span style="margin-top: 1em" class="small" :class="{grey: !server.userPlan.isExpired, red: server.userPlan.isExpired}">套餐：{{server.userPlan.name}} / {{server.userPlan.dayTo}} <span v-if="server.userPlan.isExpired">已过期</span></span>
					</div>
					<div v-if="server.trafficLimitStatus != null">
						<ServerTrafficLimitStatusViewer v-model="server.trafficLimitStatus"></ServerTrafficLimitStatusViewer>
					</div>
				</template>
			</Column>
			<Column header="CNAME" class="domain-column">
				<template #body="{data:server}">
					{{server.cname}}
				</template>
			</Column>
			<Column header="TCP" v-if="ctx.supportTCP" class="name-column">
				<template #body="{data:server}">
					<div v-if="server.tcpPorts.length > 0" class="label-group">
						<TLabel outlined="" v-for="port in server.tcpPorts">{{port}}</TLabel>
					</div>
					<span v-else class="disabled">-</span>
				</template>
			</Column>
			<Column header="TLS" v-if="ctx.supportTCP" class="name-column">
				<template #body="{data:server}">
					<div v-if="server.tlsPorts.length > 0" class="label-group">
						<TLabel outlined=""  v-for="port in server.tlsPorts">{{port}}</TLabel>
					</div>
					<span v-else class="disabled">-</span>
				</template>
			</Column>
			<Column header="UDP" v-if="ctx.supportUDP" class="name-column">
				<template #body="{data:server}">
					<div v-if="server.udpPorts.length > 0" class="label-group">
						<TLabel outlined="" v-for="port in server.udpPorts">{{port}}</TLabel>
					</div>
					<span v-else class="disabled">-</span>
				</template>
			</Column>
			<Column header="状态" class="status-column">
				<template #body="{data:server}">
					<span v-if="!server.isOn" class="grey">停用中</span>
					<span v-else class="green">正常</span>
				</template>
			</Column>
			<Column header="操作" class="three op" frozen align-frozen="right">
				<template #body="{data:server}">
					<a :href="'/lb/server?serverId=' + server.id">管理</a> &nbsp;
					<a href="" v-if="server.isOn" @click.prevent="ctx.updateServerOff(server.id)">停用</a><a href="" v-if="!server.isOn" @click.prevent="ctx.updateServerOn(server.id)"><span class="red">启用</span></a> &nbsp;
					<a href=""  @click.prevent="ctx.deleteServer(server.id)">删除</a>
				</template>
			</Column>
		</ListTable>

		<Pager :data="ctx.page"></Pager>
	</div>
</template>

<style lang="postcss" scoped>
</style>