<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 "./create.js"
import Menu from "./@menu.vue"
import TInputText from "@/components/ui/TInputText.vue";
import PostForm from "@/components/form/PostForm.vue";
import TMessage from "@/components/ui/TMessage.vue";
import TTextarea from "@/components/ui/TTextarea.vue";
import NSDomainGroupSelector from "@/components/ns/NSDomainGroupSelector.vue";
import SubmitButton from "@/components/form/SubmitButton.vue";
import MoreOptionsIndicator from "@/components/common/MoreOptionsIndicator.vue";
import {ref} from "vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const nameInput = ref()
const namesInput = ref()
const groupSelector = ref()
const ctx = ctxRef(new Context(), {
	refs: {nameInput, namesInput, groupSelector}
})

</script>

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

	<div v-if="!ctx.configIsValid">
		<TMessage error="">管理员尚未完成系统配置，暂时无法添加域名，请联系客服。</TMessage>
	</div>

	<PostForm action="$" @success="ctx.success" v-show="ctx.configIsValid">
		<input type="hidden" name="addingType" :value="ctx.addingType"/>
		<DefinitionTable>
			<tbody>
			<tr>
				<td class="title">域名 *</td>
				<td>
					<!-- 单个添加 -->
					<div v-show="ctx.addingType == 'one'">
						<TInputText name="name" maxlength="255" ref="nameInput" placeholder="单个域名" class="w-full"/>
						<p class="comment">
							<a href="" @click.prevent="ctx.setAddingType('batch')">[批量添加]</a>
						</p>
					</div>

					<!-- 批量添加 -->
					<div v-show="ctx.addingType == 'batch'">
						<TTextarea rows="10" name="names" placeholder="每行一个域名" ref="namesInput" class="w-full"></TTextarea>
						<p class="comment">
							<a href="" @click.prevent="ctx.setAddingType('one')">[单个添加]</a>
						</p>
					</div>
				</td>
			</tr>
			<tr>
				<td colspan="2"><MoreOptionsIndicator></MoreOptionsIndicator></td>
			</tr>
			</tbody>
			<tbody v-show="ctx.moreOptionsVisible">
			<tr v-if="ctx.hasGroups">
				<td>所属分组</td>
				<td>
					<NSDomainGroupSelector ref="groupSelector"></NSDomainGroupSelector>
				</td>
			</tr>
			</tbody>
		</DefinitionTable>
		<SubmitButton v-if="ctx.quotaCanCreate"></SubmitButton>
		<TMessage error="" v-if="!ctx.quotaCanCreate">已添加域名个数已超出最大限额（{{ctx.quotaMaxDomains}}个域名），请升级套餐后重试。</TMessage>
	</PostForm>
</template>

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