<script setup>
import PopupLayout from "@/layouts/PopupLayout.vue";
import ctxRef from "@/utils/ctx_ref.js";
import Context from "./createIPPopup.js"
import SubmitButton from "@/components/form/SubmitButton.vue";
import TInputText from "@/components/ui/TInputText.vue";
import PostForm from "@/components/form/PostForm.vue";
import TSelect from "@/components/ui/TSelect.vue";
import TOption from "@/components/ui/TOption.vue";
import TRadio from "@/components/ui/TRadio.vue";
import TTextarea from "@/components/ui/TTextarea.vue";
import CodeLabel from "@/components/common/CodeLabel.vue";
import DatetimeInput from "@/components/common/DatetimeInput.vue";
import MoreOptionsIndicator from "@/components/common/MoreOptionsIndicator.vue";
import FirewallEventLevelOptions from "@/components/server/FirewallEventLevelOptions.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const ctx = ctxRef(new Context())

</script>

<template>
	<PopupLayout>
		<header v-if="ctx.list.type == 'white'">添加IP到白名单</header>
		<header v-if="ctx.list.type == 'black'">添加IP到黑名单</header>
		<header v-if="ctx.list.type == 'grey'">添加IP到灰名单</header>

		<PostForm action="$" @success="ctx.success">
			<input type="hidden" name="listId" :value="ctx.listId"/>
			<input type="hidden" name="serverId" :value="ctx.serverId"/>
			<input type="hidden" name="sourceCategory" :value="ctx.sourceCategory"/>

			<DefinitionTable>
				<tbody v-show="!ctx.quickMode">
				<tr>
					<td class="title">类型 *</td>
					<td>
						<TSelect name="type" v-model="ctx.type">
							<TOption value="ip">单IP或IP段</TOption>
							<TOption value="all">所有IP</TOption>
						</TSelect>
						<p class="comment" v-if="ctx.type == 'ip'">单个IP或一个IP范围。</p>
						<p class="comment" v-if="ctx.type == 'all'">允许或禁用所有的IP。</p>
					</td>
				</tr>

				<tr v-if="ctx.type != 'all'">
					<td>添加方式</td>
					<td>
						<TRadio name="method" value="single" v-model="ctx.method">单个添加</TRadio> &nbsp; &nbsp;
						<TRadio name="method" value="batch" v-model="ctx.method">批量添加</TRadio>
					</td>
				</tr>
				</tbody>

				<!-- 单个添加 -->
				<tbody v-if="ctx.method == 'single' && ctx.type == 'ip'">
				<tr>
					<td>IP或IP段 *</td>
					<td>
						<TInputText type="text" name="value" maxlength="64" placeholder="x.x.x.x" autofocus class="w-full" v-model="ctx.value"/>
						<p class="comment">支持IPv4和IPv6；支持三种格式：单个IP（比如192.168.1.100）、IP范围（比如192.168.1.1-192.168.1.255）、CIDR（比如192.168.1.1/24）。</p>
					</td>
				</tr>
				</tbody>

				<!-- 批量添加 -->
				<tbody v-if="ctx.method == 'batch' && ctx.type == 'ip'">
				<tr>
					<td>IP列表</td>
					<td>
						<TTextarea name="ipData" class="w-full" :placeholder="'x.x.x.x' + '\n' + 'x.x.x.x/24' + '\n' + 'x.x.x.x-y.y.y.y'" rows="3"></TTextarea>
						<p class="comment">每行一个IP，支持三种格式：
							<CodeLabel :class="'grey'">192.168.1.100</CodeLabel>
							、
							<CodeLabel :class="'grey'">192.168.1.1-192.168.1.255</CodeLabel>
							、
							<CodeLabel :class="'grey'">192.168.1.1/24</CodeLabel>
							。
						</p>
					</td>
				</tr>
				</tbody>

				<tbody>
				<tr>
					<td>过期时间</td>
					<td>
						<DatetimeInput :v-name="'expiredAt'" :v-timestamp="ctx.expiresAt"></DatetimeInput>
						<p class="comment">在加入名单某一段时间后会失效，留空表示永久有效。</p>
					</td>
				</tr>

				<tr>
					<td>级别</td>
					<td>
						<FirewallEventLevelOptions :v-value="ctx.eventLevel"></FirewallEventLevelOptions>
					</td>
				</tr>

				<tr>
					<td colspan="2">
						<MoreOptionsIndicator></MoreOptionsIndicator>
					</td>
				</tr>
				</tbody>
				<tbody v-show="ctx.moreOptionsVisible">
				<tr>
					<td>备注</td>
					<td>
						<TInputText type="text" name="reason" maxlength="100" class="w-full" v-model="ctx.reason"/>
					</td>
				</tr>
				</tbody>
			</DefinitionTable>
			<SubmitButton></SubmitButton>
		</PostForm>
	</PopupLayout>
</template>

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