<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 "./renew.js"
import TButton from "@/components/ui/TButton.vue";
import PostForm from "@/components/form/PostForm.vue";
import TTag from "@/components/ui/TTag.vue";
import TStepView from "@/components/ui/TStepView.vue";
import TSteps from "@/components/ui/TSteps.vue";
import TStep from "@/components/ui/TStep.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const ctx = ctxRef(new Context())

</script>

<template>
	<!-- 步骤 -->
	<TStepView step="pay">
		<TSteps>
			<TStep>
				<a href="/anti-ddos/instances">选择要续费的实例</a>
			</TStep>
			<TStep code="pay">
				支付
			</TStep>
		</TSteps>
	</TStepView>

	<PostForm action="$" @success="ctx.success" class="mt-2">
		<input type="hidden" name="userInstanceId" :value="ctx.userInstance.id"/>
		<input type="hidden" name="periodId" :value="ctx.selectedPeriodId"/>
		<DefinitionTable>
			<tbody>
			<tr>
				<td>高防产品</td>
				<td>
					<div v-if="ctx.userInstance.package.id > 0">{{ctx.userInstance.package.summary}}</div>
					<span v-else class="red">已删除</span>
				</td>
			</tr>
			<tr>
				<td>高防IP</td>
				<td>
					<div v-if="ctx.userInstance.instance.id > 0">
						<div v-for="ip in ctx.userInstance.instance.ipAddresses">{{ip}}</div>
					</div>
					<span v-else class="red">已删除</span>
				</td>
			</tr>
			<tr>
				<td>当前有效期</td>
				<td>{{ctx.userInstance.dayTo}}
					<p class="comment" v-if="ctx.userInstance.isExpired">
						<span class="red">已过期，续费后新的有效期将会从今天（{{ctx.userInstance.today}}）开始。</span>
					</p>
					<p class="comment" v-else>续费后，将会在此基础上增加新的有效期。</p>
				</td>
			</tr>
			<tr>
				<td>新续有效期 *</td>
				<td>
					<div v-if="ctx.allPeriods.length > 0" class="label-group">
						<a v-for="period in ctx.allPeriods" @click.prevent="ctx.selectPeriod(period.id)"><TTag :secondary="period.id != ctx.selectedPeriodId">{{period.count}}{{period.unitName}}</TTag></a>
					</div>
					<span v-else class="red">没有可用的有效期</span>
				</td>
			</tr>
			<tr>
				<td>价格</td>
				<td>
					<span v-if="ctx.amount == 0" class="disabled">没有找到对应价格</span>
					<span v-if="ctx.amount > 0">{{ctx.amount}}元</span>
				</td>
			</tr>
			</tbody>
		</DefinitionTable>
		<TButton type="button" @click.prevent="ctx.goNext()" v-if="ctx.amount > 0">下一步</TButton>
	</PostForm>

</template>

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