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

import PopupLayout from "@/layouts/PopupLayout.vue";
import ctxRef from "@/utils/ctx_ref.js"
import Context from "./index.js"
import TSelect from "@/components/ui/TSelect.vue";
import TOption from "@/components/ui/TOption.vue";
import TCheckboxBinary from "@/components/ui/TCheckboxBinary.vue";
import SubmitButton from "@/components/form/SubmitButton.vue";
import TButton from "@/components/ui/TButton.vue";
import PostForm from "@/components/form/PostForm.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const ctx = ctxRef(new Context())

</script>

<template>
	<PopupLayout>

		<header>批量复制配置</header>

		<PostForm action="$" @success="ctx.success" @done="ctx.done" @before="ctx.before">
			<input type="hidden" name="serverId" :value="ctx.serverId"/>
			<input type="hidden" name="configCode" :value="ctx.configCode"/>
			<input type="hidden" name="targetsJSON" :value="JSON.stringify(ctx.targets)"/>

			<DefinitionTable>
				<tbody>
				<tr>
					<td class="title">将当前配置复制到</td>
					<td>
						<TSelect v-model="ctx.targetType" @change="ctx.changeTargetType">
							<TOption value="">[选择目标]</TOption>
							<TOption v-for="option in ctx.targetOptions" :value="option.code">{{option.name}}</TOption>
						</TSelect>
						<p class="comment" v-if="ctx.configCode == 'waf'">目前只能同步WAF设置的启用和停用，不能复制规则、IP名单等。</p>
					</td>
				</tr>
				</tbody>

				<!-- WAF特殊选项 -->
				<tbody v-show="ctx.configCode == 'waf'">
				<tr>
					<td>同时拷贝国家/地区/省份封禁</td>
					<td>
						<TCheckboxBinary name="wafCopyRegions"></TCheckboxBinary>
						<p class="comment">WAF设置专有选项。</p>
					</td>
				</tr>
				</tbody>

				<!-- 选择分组 -->
				<tbody v-show="ctx.targetType == 'group'">
				<tr v-show="ctx.userId > 0">
					<td>分组</td>
					<td>
						<div class="grid grid-cols-3 gap-2">
							<TCheckboxBinary v-for="userGroup in ctx.userGroups" :v-value="userGroup.id" @input="ctx.changeGroupId(userGroup.id)">{{userGroup.name}}</TCheckboxBinary>
						</div>
					</td>
				</tr>
				</tbody>

				<!-- 选择网站 -->
				<tbody v-show="ctx.targetType == 'server'">
				<tr>
					<td>网站</td>
					<td></td>
				</tr>
				</tbody>

				<!-- 网站数量 -->
				<tbody>
				<tr>
					<td>目标网站数量</td>
					<td>
						<span v-if="ctx.countServers < 0">-</span>
						<span v-else><strong>{{ctx.countServers}}个</strong></span>
					</td>
				</tr>
				<tr v-if="ctx.countServers > 0">
					<td>提醒</td>
					<td><span class="red">此操作无法撤销，请谨慎确认！</span></td>
				</tr>
				</tbody>
			</DefinitionTable>

			<SubmitButton v-show="!ctx.isRequesting">确认复制</SubmitButton>
			<TButton secondary="" type="button" v-show="ctx.isRequesting">正在复制...</TButton>
		</PostForm>
	</PopupLayout>
</template>

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