<script setup lang="ts">
import apis from "@a"
import router from "@r"
import appStore from "@s"

const tenantEnabled = ref(true)
const tenantId = ref("")
const tenantOptions = ref<any>([])
const show = computed(() => {
	return appStore.authStore.canChangeTenant()
})

const initTenant = async () => {
	apis.loginApi.getTenantList().then((res: any) => {
		tenantEnabled.value = res.data.tenantEnabled === undefined ? true : res.data.tenantEnabled
		if (tenantEnabled.value) {
			tenantOptions.value = res.data.voList
		}
	})
}

function onDynamicTenant() {
	if (tenantId.value != null && tenantId.value !== "") {
		apis.system.tenantApi.dynamicTenant(tenantId.value).then((res: any) => {
			appStore.authStore.setDynamic(true)
			appStore.tagViewStore.delAllViews()
			router.push({ path: "/" })
		})
	}
}
function onDynamicClear() {
	apis.system.tenantApi.dynamicClear().then((res: any) => {
		appStore.authStore.setDynamic(false)
		appStore.tagViewStore.delAllViews()
		router.push({ path: "/" })
	})
}
function init() {
	initTenant()
}

onMounted(init)
</script>
<template>
	<div class="app-navbar-item ms-1 ms-md-3" v-if="show">
		<el-select
			v-model="tenantId"
			clearable
			filterable
			reserve-keyword
			placeholder="请选择租户"
			@change="onDynamicTenant"
			@clear="onDynamicClear">
			<el-option
				v-for="item in tenantOptions"
				:key="item.tenantId"
				:label="item.companyName"
				:value="item.tenantId"></el-option>
			<template #prefix>
				<svg-icon icon-class="company" class="el-input__icon input-icon" />
			</template>
		</el-select>
	</div>
</template>
