<script setup>

import useUtils from "@/utils/utils.js"
import {inject, ref} from "vue"
import OriginListTable from "@/components/server/OriginListTable.vue";
import TipIcon from "@/components/common/TipIcon.vue";

const utils = useUtils()

const Tea = inject("$Tea")

const props = defineProps(["v-primary-origins", "v-backup-origins", "v-server-type", "v-params"])

const {primaryOrigins, backupOrigins} = (function () {
	return {
		primaryOrigins: ref(props.vPrimaryOrigins),
		backupOrigins: ref(props.vBackupOrigins)
	}
})()

const createPrimaryOrigin = function () {
	utils.popup("/servers/server/settings/origins/addPopup?originType=primary&" + props.vParams, {
		width: "45em",
		height: "36em",
		callback: function (resp) {
			utils.success("保存成功", function () {
				window.location.reload()
			})
		}
	})
}

const createBackupOrigin = function () {
	utils.popup("/servers/server/settings/origins/addPopup?originType=backup&" + props.vParams, {
		width: "45em",
		height: "36em",
		callback: function (resp) {
			utils.success("保存成功", function () {
				window.location.reload()
			})
		}
	})
}

const updateOrigin = function (originId, originType) {
	utils.popup("/servers/server/settings/origins/updatePopup?originType=" + originType + "&" + props.vParams + "&originId=" + originId, {
		width: "45em",
		height: "36em",
		callback: function (resp) {
			utils.success("保存成功", function () {
				window.location.reload()
			})
		}
	})
}

const deleteOrigin = function (originId, originAddr, originType) {
	utils.confirm("确定要删除此源站（" + originAddr + "）吗？", function () {
		Tea.action("/servers/server/settings/origins/delete?" + props.vParams + "&originId=" + originId + "&originType=" + originType)
			.post()
			.success(function () {
				utils.success("删除成功", function () {
					window.location.reload()
				})
			})
	})
}

const updateOriginIsOn = function (originId, originAddr, isOn) {
	let message
	let resultMessage
	if (isOn) {
		message = "确定要启用此源站（" + originAddr + "）吗？"
		resultMessage = "启用成功"
	} else {
		message = "确定要停用此源站（" + originAddr + "）吗？"
		resultMessage = "停用成功"
	}
	utils.confirm(message, function () {
		Tea.action("/servers/server/settings/origins/updateIsOn?" + props.vParams + "&originId=" + originId + "&isOn=" + (isOn ? 1 : 0))
			.post()
			.success(function () {
				utils.success(resultMessage, function () {
					window.location.reload()
				})
			})
	})
}


</script>

<template>
	<div>
		<h5 class="!mb-4">主要源站
			<a href="" @click.prevent="createPrimaryOrigin()">[添加主要源站]</a>
			<TipIcon content="主要源站为日常使用的源站，如果添加多个源站，可以根据权重随机调用。"></TipIcon>
		</h5>
		<p class="comment" v-if="primaryOrigins.length == 0">暂时还没有主要源站。</p>
		<OriginListTable v-if="primaryOrigins.length > 0" :v-origins="vPrimaryOrigins" :v-origin-type="'primary'" @delete-origin="deleteOrigin" @update-origin="updateOrigin" @update-origin-is-on="updateOriginIsOn"></OriginListTable>

		<h5 class="group-header">备用源站
			<a href="" @click.prevent="createBackupOrigin()">[添加备用源站]</a>
			<TipIcon content="备用源站为备用的源站，当所有的主要源站都无法访问时，会自动切换到备用源站；如果有多个备用源站，也可以根据权重随机调用；如果主要源站中的任一源站恢复可访问状态时，系统会自动切换回主要源站。"></TipIcon>
		</h5>
		<p class="comment" v-if="backupOrigins.length == 0">暂时还没有备用源站。</p>
		<OriginListTable v-if="backupOrigins.length > 0" :v-origins="backupOrigins" :v-origin-type="'backup'" @delete-origin="deleteOrigin" @update-origin="updateOrigin" @update-origin-is-on="updateOriginIsOn"></OriginListTable>
	</div>
</template>

<style scoped>
</style>
