<script setup>

import {ref, watch} from "vue"
import PriorCheckbox from "@/components/common/PriorCheckbox.vue";
import TCheckboxBinary from "@/components/ui/TCheckboxBinary.vue";
import TInputText from "@/components/ui/TInputText.vue";
import CodeLabel from "@/components/common/CodeLabel.vue";
import SizeCapacityBox from "@/components/common/SizeCapacityBox.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const props = defineProps(["v-request-limit-config", "v-is-group", "v-is-location"])

const {config, maxConns, maxConnsPerIP} = (function () {
	let config = props.vRequestLimitConfig
	if (config == null) {
		config = {
			isPrior: false,
			isOn: false,
			maxConns: 0,
			maxConnsPerIP: 0,
			maxBodySize: {
				count: -1,
				unit: "kb"
			},
			outBandwidthPerConn: {
				count: -1,
				unit: "kb"
			}
		}
	}
	return {
		config: ref(config),
		maxConns: ref(config.maxConns),
		maxConnsPerIP: ref(config.maxConnsPerIP)
	}
})()

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

watch(maxConns, function (v) {
	let conns = parseInt(v, 10)
	if (isNaN(conns)) {
		config.value.maxConns = 0
		return
	}
	if (conns < 0) {
		config.value.maxConns = 0
	} else {
		config.value.maxConns = conns
	}
})

watch(maxConnsPerIP, function (v) {
	let conns = parseInt(v, 10)
	if (isNaN(conns)) {
		config.value.maxConnsPerIP = 0
		return
	}
	if (conns < 0) {
		config.value.maxConnsPerIP = 0
	} else {
		config.value.maxConnsPerIP = conns
	}
})

</script>

<template>
	<div>
		<input type="hidden" name="requestLimitJSON" :value="JSON.stringify(config)"/>
		<DefinitionTable>
			<PriorCheckbox :v-config="config" v-if="vIsLocation || vIsGroup"></PriorCheckbox>
			<tbody v-show="(!vIsLocation && !vIsGroup) || config.isPrior">
			<tr>
				<td class="title">启用请求限制</td>
				<td>
					<TCheckboxBinary v-model="config.isOn"></TCheckboxBinary>
				</td>
			</tr>
			</tbody>
			<tbody v-show="isOn()">
			<tr>
				<td>最大并发连接数</td>
				<td>
					<TInputText type="text" maxlength="6" v-model="maxConns"/>
					<p class="comment">当前网站最大并发连接数，超出此限制则响应用户
						<CodeLabel>429</CodeLabel>
						代码。为0表示不限制。
					</p>
				</td>
			</tr>
			<tr>
				<td>单IP最大并发连接数</td>
				<td>
					<TInputText type="text" maxlength="6" v-model="maxConnsPerIP"/>
					<p class="comment">单IP最大连接数，统计单个IP总连接数时不区分网站，超出此限制则响应用户
						<CodeLabel>429</CodeLabel>
						代码。为0表示不限制。<span v-if="maxConnsPerIP > 0 && maxConnsPerIP <= 3" class="red">当前设置的并发连接数过低，可能会影响正常用户访问，建议不小于3。</span>
					</p>
				</td>
			</tr>
			<tr>
				<td>单连接带宽限制</td>
				<td>
					<SizeCapacityBox :v-value="config.outBandwidthPerConn" :v-supported-units="['byte', 'kb', 'mb']"></SizeCapacityBox>
					<p class="comment">客户端单个请求每秒可以读取的下行流量。</p>
				</td>
			</tr>
			<tr>
				<td>单请求最大尺寸</td>
				<td>
					<SizeCapacityBox :v-value="config.maxBodySize" :v-supported-units="['byte', 'kb', 'mb', 'gb']"></SizeCapacityBox>
					<p class="comment">单个请求能发送的最大内容尺寸。</p>
				</td>
			</tr>
			</tbody>
		</DefinitionTable>
	</div>
</template>

<style scoped>
</style>
