<script setup>
import {inject, onMounted, ref} from "vue";

const Tea = inject("$Tea")

const token = ref("")
const tokenInputRe = ref()
const emits = defineEmits(["update:modelValue"])

const refreshToken = function () {
	Tea.action("/csrf/token")
		.get()
		.success(function (resp) {
			token.value = resp.data.token
			emits("update:modelValue", token.value)
		})
}

onMounted(() => {
	refreshToken()

	tokenInputRe.value.form.addEventListener("submit", function () {
		refreshToken()
	})

	// 自动刷新
	setInterval(function () {
		refreshToken()
	}, 10 * 60 * 1000)
})

</script>

<template>
	<input type="hidden" name="csrfToken" :value="token" ref="tokenInputRe"/>
</template>

<style scoped>

</style>