<script setup>

import "@/lib/array.js"
import useUtils from "@/utils/utils.js"
import {ref} from "vue"
import TCheckboxBinary from "@/components/ui/TCheckboxBinary.vue";
import PriorCheckbox from "@/components/common/PriorCheckbox.vue";
import TinyBasicLabel from "@/components/common/TinyBasicLabel.vue";
import LabelOn from "@/components/common/LabelOn.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";
import ListTable from "@/components/common/ListTable.vue";
import TColumn from "@/components/ui/TColumn.vue";
import NavButton from "@/components/common/NavButton.vue";

const utils = useUtils()

const props = defineProps(["v-auth-config", "v-is-location"])

const emits = defineEmits(["change"])

const {authConfig} = (function () {
	let authConfig = props.vAuthConfig
	if (authConfig == null) {
		authConfig = {
			isPrior: false,
			isOn: false
		}
	}
	if (authConfig.policyRefs == null) {
		authConfig.policyRefs = []
	}
	return {
		authConfig: ref(authConfig)
	}
})()

const isOn = function () {
	return (!props.vIsLocation || authConfig.value.isPrior) && authConfig.value.isOn
}

const add = function () {
	utils.popup("/servers/server/settings/access/createPopup", {
		callback: function (resp) {
			authConfig.value.policyRefs.push(resp.data.policyRef)
			change()
		},
		height: utils.POPUP_HEIGHT_LG
	})
}

const update = function (index, policyId) {
	utils.popup("/servers/server/settings/access/updatePopup?policyId=" + policyId, {
		callback: function (resp) {
			utils.success("保存成功", function () {
				utils.reload()
			})
		},
		height: utils.POPUP_HEIGHT_LG
	})
}

const remove = function (index) {
	authConfig.value.policyRefs.$remove(index)
	change()
}

const methodName = function (methodType) {
	switch (methodType) {
		case "basicAuth":
			return "BasicAuth"
		case "subRequest":
			return "子请求"
		case "typeA":
			return "URL鉴权A"
		case "typeB":
			return "URL鉴权B"
		case "typeC":
			return "URL鉴权C"
		case "typeD":
			return "URL鉴权D"
	}
	return ""
}

const change = function () {
	setTimeout(function () {
		// 延时通知，是为了让表单有机会变更数据
		emits("change", authConfig.value)
	}, 100)
}


</script>

<template>
	<div>
		<input type="hidden" name="authJSON" :value="JSON.stringify(authConfig)"/>
		<DefinitionTable>
			<PriorCheckbox :v-config="authConfig" v-if="vIsLocation"></PriorCheckbox>
			<tbody v-show="!vIsLocation || authConfig.isPrior">
			<tr>
				<td class="title">启用鉴权</td>
				<td>
					<TCheckboxBinary v-model="authConfig.isOn"/>
				</td>
			</tr>
			</tbody>
		</DefinitionTable>

		<!-- 鉴权方式 -->
		<div v-show="isOn()">
			<h5 class="group-header">鉴权方式</h5>
			<div v-show="authConfig.policyRefs.length > 0">
				<ListTable :items="authConfig.policyRefs" dataKey="authPolicyId">
					<TColumn header="名称" class="name-column" frozen>
						<template #body="{data: ref}">
							{{ ref.authPolicy.name }}
						</template>
					</TColumn>
					<TColumn header="鉴权方法" class="normal-value-column">
						<template #body="{data: ref}">
							{{ methodName(ref.authPolicy.type) }}
						</template>
					</TColumn>
					<TColumn header="参数" class="description-column">
						<template #body="{data: ref}">
							<span v-if="ref.authPolicy.type == 'basicAuth'">{{ ref.authPolicy.params.users.length }}个用户</span>
							<span v-if="ref.authPolicy.type == 'subRequest'">
								<span v-if="ref.authPolicy.params.method.length > 0" class="grey">[{{ ref.authPolicy.params.method }}]</span>
								{{ ref.authPolicy.params.url }}
							</span>
							<span v-if="ref.authPolicy.type == 'typeA'">{{ ref.authPolicy.params.signParamName }}/有效期{{ ref.authPolicy.params.life }}秒</span>
							<span v-if="ref.authPolicy.type == 'typeB'">有效期{{ ref.authPolicy.params.life }}秒</span>
							<span v-if="ref.authPolicy.type == 'typeC'">有效期{{ ref.authPolicy.params.life }}秒</span>
							<span v-if="ref.authPolicy.type == 'typeD'">{{ ref.authPolicy.params.signParamName }}/{{ ref.authPolicy.params.timestampParamName }}/有效期{{ ref.authPolicy.params.life }}秒</span>

							<div v-if="(ref.authPolicy.params.exts != null && ref.authPolicy.params.exts.length > 0) || (ref.authPolicy.params.domains != null && ref.authPolicy.params.domains.length > 0)" class="label-group mt-1">
								<TinyBasicLabel v-if="ref.authPolicy.params.exts != null" v-for="ext in ref.authPolicy.params.exts">扩展名：{{ ext }}</TinyBasicLabel>
								<TinyBasicLabel v-if="ref.authPolicy.params.domains != null" v-for="domain in ref.authPolicy.params.domains">域名：{{ domain }}</TinyBasicLabel>
							</div>
						</template>
					</TColumn>
					<TColumn header="状态" class="fixed-status-column">
						<template #body="{data: ref}">
							<LabelOn :v-is-on="ref.authPolicy.isOn"></LabelOn>
						</template>
					</TColumn>
					<TColumn header="操作" class="two op" frozen align-frozen="right">
						<template #body="{data: ref, index}">
							<a href="" @click.prevent="update(index, ref.authPolicyId)">修改</a> &nbsp;
							<a href="" @click.prevent="remove(index)">删除</a>
						</template>
					</TColumn>
				</ListTable>
			</div>
			<div class="mt-4">
				<NavButton small type="button" @click.prevent="add" action-create>添加鉴权方式</NavButton>
			</div>
		</div>
	</div>
</template>

<style scoped>
</style>
