<script setup>

// CC
import TInputText from "@/components/ui/TInputText.vue";
import MetricKeysConfigBox from "@/components/server/MetricKeysConfigBox.vue";
import TCheckboxBinary from "@/components/ui/TCheckboxBinary.vue";
import {ref, watch} from "vue";
import TInputGroup from "@/components/ui/TInputGroup.vue";
import TInputGroupAddon from "@/components/ui/TInputGroupAddon.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const props = defineProps(["v-checkpoint"])

const {keys, period, threshold, ignoreCommonFiles, enableFingerprint, options, value} = (function () {
	let keys = []
	let period = 60
	let threshold = 1000
	let ignoreCommonFiles = true
	let enableFingerprint = true

	let options = {}
	if (window.UPDATING_RULE != null) {
		options = window.UPDATING_RULE.checkpointOptions
	}

	if (options == null) {
		options = {}
	}
	if (options.keys != null) {
		keys = options.keys
	}
	if (keys.length == 0) {
		keys = ["${remoteAddr}", "${requestPath}"]
	}
	if (options.period != null) {
		period = options.period
	}
	if (options.threshold != null) {
		threshold = options.threshold
	}
	if (options.ignoreCommonFiles != null && typeof (options.ignoreCommonFiles) == "boolean") {
		ignoreCommonFiles = options.ignoreCommonFiles
	}
	if (options.enableFingerprint != null && typeof (options.enableFingerprint) == "boolean") {
		enableFingerprint = options.enableFingerprint
	}

	setTimeout(function () {
		change()
	}, 100)

	return {
		keys: ref(keys),
		period: ref(period),
		threshold: ref(threshold),
		ignoreCommonFiles: ref(ignoreCommonFiles),
		enableFingerprint: ref(enableFingerprint),
		options: ref({}),
		value: ref(threshold)
	}
})()

const changeKeys = function (_keys) {
	keys.value = _keys
	change()
}

const change = function () {
	let _period = parseInt(period.value.toString())
	if (isNaN(_period) || _period <= 0) {
		_period = 60
	}

	let _threshold = parseInt(threshold.value.toString())
	if (isNaN(_threshold) || _threshold <= 0) {
		_threshold = 1000
	}
	value.value = _threshold

	let _ignoreCommonFiles = ignoreCommonFiles.value
	if (typeof _ignoreCommonFiles != "boolean") {
		_ignoreCommonFiles = false
	}

	let _enableFingerprint = enableFingerprint.value
	if (typeof _enableFingerprint != "boolean") {
		_enableFingerprint = true
	}

	props.vCheckpoint.options = [
		{
			code: "keys",
			value: keys.value
		},
		{
			code: "period",
			value: _period,
		},
		{
			code: "threshold",
			value: _threshold
		},
		{
			code: "ignoreCommonFiles",
			value: _ignoreCommonFiles
		},
		{
			code: "enableFingerprint",
			value: _enableFingerprint
		}
	]
}

const thresholdTooLow = function () {
	let _threshold = parseInt(threshold.value.toString())
	if (isNaN(_threshold) || _threshold <= 0) {
		_threshold = 1000
	}
	return _threshold > 0 && _threshold < 5
}

watch(period, function () {
	change()
})

watch(threshold, function () {
	change()
})

watch(ignoreCommonFiles, function () {
	change()
})

watch(enableFingerprint, function () {
	change()
})
</script>

<template>
	<div>
		<input type="hidden" name="operator" value="gt"/>
		<input type="hidden" name="value" :value="value"/>
		<DefinitionTable>
			<tbody>
			<tr>
				<td class="title">统计对象组合 *</td>
				<td class="!font-normal">
					<MetricKeysConfigBox :v-keys="keys" @change="changeKeys"></MetricKeysConfigBox>
				</td>
			</tr>
			<tr>
				<td>统计周期 *</td>
				<td class="!font-normal">
					<TInputGroup>
						<TInputGroupAddon>
							<span>每</span>
						</TInputGroupAddon>
						<TInputText type="text" v-model="period" style="width: 6em" maxlength="8"/>
						<TInputGroupAddon>
							<span>秒</span>
						</TInputGroupAddon>
					</TInputGroup>
				</td>
			</tr>
			<tr>
				<td>阈值 *</td>
				<td class="!font-normal">
					<TInputText type="text" v-model="threshold" style="width: 6em" maxlength="8"/>
					<p class="comment">
						当统计对象在统计周期内请求次数达到此阈值时即认为满足CC条件。
						<span class="red" v-if="thresholdTooLow()">对于网站类应用来说，当前阈值（{{ threshold }}）设置的太低，有可能会影响用户正常访问。</span>
					</p>
				</td>
			</tr>
			<tr>
				<td>检查请求来源指纹</td>
				<td class="!font-normal">
					<TCheckboxBinary v-model="enableFingerprint"></TCheckboxBinary>
					<p class="comment">在接收到HTTPS请求时尝试检查请求来源的指纹，用来检测代理服务和爬虫攻击；如果你在网站前面放置了别的反向代理服务，请取消此选项。</p>
				</td>
			</tr>
			<tr>
				<td>忽略常用文件</td>
				<td class="!font-normal">
					<TCheckboxBinary v-model="ignoreCommonFiles"></TCheckboxBinary>
					<p class="comment">忽略js、css、jpg等常在网页里被引用的文件名，即对这些文件的访问不加入计数，可以减少误判几率。但如果单独访问这些文件，比如在浏览器里直接打开，也会加入计数。</p>
				</td>
			</tr>
			</tbody>
		</DefinitionTable>
	</div>
</template>