<template>
	<div class="main">
		<div class="auth-shell">
			<div class="top-actions ">
				<div class="setup-item center-justify">
					<el-dropdown>
						<span class="el-dropdown-link ">
							<span class="setup-text">{{ $t('login.network') }}</span>
						</span>
					</el-dropdown>
				</div>
				<div class="setup-item center-justify">
					<el-dropdown>
						<span class="el-dropdown-link">
							<span class="setup-text">{{ $t('login.Language') }}</span>
						</span>
						<template #dropdown>
							<el-dropdown-menu>
								<el-dropdown-item @click="switchLanguage('zh')">
									中文
								</el-dropdown-item>
								<el-dropdown-item @click="switchLanguage('en')">
									English
								</el-dropdown-item>
							</el-dropdown-menu>
						</template>
					</el-dropdown>
				</div>
			</div>
			<div class="auth-card">
				<div class="auth-left">
					<div class="logo ">
						<img class="logo-image" src="@/assets/logo2.png" alt="">
						<span class="logo-title">TK主播数据助手</span>
					</div>
					<div class="welcome">{{ $t('login.title') }}</div>
					<div class="from">
						<div class="from-input">
							<el-form label-position="left" label-width="100px" :model="formData">
								<div class="field-label">{{ $t('login.tenantName') }}</div>
								<div class="from-input-item1">
									<img src="@/assets/username.png" alt="">
									<el-input style="height: 25px;" v-model="formData.tenantName"
										:placeholder="$t('login.tenantName')" clearable @keyup.enter="onSubmit" />
								</div>
								<div class="field-label">{{ $t('login.account') }}</div>
								<div class="from-input-item1">
									<img src="@/assets/username.png" alt="">
									<el-input style="height: 25px;" v-model="formData.userId"
										:placeholder="$t('login.account')" clearable @keyup.enter="onSubmit" />
								</div>
								<div class="field-label">{{ $t('login.password') }}</div>
								<div class="from-input-item1">
									<img src="@/assets/password.png" alt="">
									<el-input style="height: 25px; " v-model="formData.password" type="password"
										:placeholder="$t('login.password')" show-password @keyup.enter="onSubmit" />
								</div>

								<div class="from-input-item">
									<el-button class="loginButton" style="height: 3vw;" color="#4f6ef7" type="primary"
										:loading="isSubmitting" :disabled="isSubmitting" @click="onSubmit">{{
											$t('login.login') }}</el-button>
								</div>
							</el-form>
						</div>
					</div>
					<div class="version">{{ $t('login.version') }}:{{ version }}</div>
				</div>
				<div class="auth-right">
					<img src="@/assets/logoBg2.png" class="illustration" alt="">
				</div>
			</div>
		</div>
	</div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { login, getIdByName } from '@/api/account';
import { getToken, setToken, setUser, setUserPass, getUserPass } from '@/utils/storage';
import { usePythonBridge } from '@/utils/pythonBridge'
import { useI18n } from 'vue-i18n'


const { locale } = useI18n()
let localeData = ref(locale.value == 'zh' ? '中文' : 'English')
function switchLanguage(lang) {
	console.log(localeData.value)
	locale.value = lang
	localStorage.setItem('lang', lang)
}

const { getVersion, stopScript } = usePythonBridge();
let version = ref('5.9.2');
onMounted(() => {

	stopScript();
	setTimeout(() => {
		getVersion().then((res) => {
			version.value = res;
		})
	}, 500);


})


const router = useRouter();
const isSubmitting = ref(false);

const formData = ref({
	tenantName: getUserPass() == null ? '' : getUserPass().tenantName,
	userId: getUserPass() == null ? '' : getUserPass().userId,
	password: getUserPass() == null ? '' : getUserPass().password,
});



const onSubmit = () => {
	if (isSubmitting.value) {
		return;
	}
	isSubmitting.value = true;
	setUserPass(formData.value);
	getIdByName(formData.value.tenantName).then((tenantId) => {
		console.log(tenantId)
		login({
			tenantId: Number(tenantId),
			username: formData.value.userId,
			password: formData.value.password,
		}).then((res) => {
			isSubmitting.value = false;
			// console.log(res)
			setToken(res.tokenValue);
			setUser(res);
			router.push('/nav');
		}).catch((err) => {
			isSubmitting.value = false;
		});
	}).catch(() => {
		isSubmitting.value = false;
	})


};
</script>

<style lang="less">
.main {
	width: 1600px;
	height: 900px;
	overflow: hidden;
	box-sizing: border-box;
	background: linear-gradient(180deg, #f3f5f9 0%, #eef1f5 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	font-family: "Source Han Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;

	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

.center-line {
	display: flex;
	flex-direction: column;
	align-items: center;
	// justify-content: center;
}

.center-justify {
	display: flex;
	justify-content: space-around;
	align-items: center;
}

.auth-shell {
	width: 1320px;
	height: 760px;
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
}

.auth-shell::before,
.auth-shell::after {
	content: "";
	position: absolute;
	border-radius: 50%;
	filter: blur(0);
}

.auth-shell::before {
	width: 420px;
	height: 420px;
	background: radial-gradient(circle, rgba(214, 226, 255, 0.7), rgba(214, 226, 255, 0));
	left: -80px;
	top: -40px;
}

.auth-shell::after {
	width: 360px;
	height: 360px;
	background: radial-gradient(circle, rgba(255, 236, 214, 0.6), rgba(255, 236, 214, 0));
	right: -40px;
	bottom: -60px;
}

.top-actions {
	position: absolute;
	top: 50px;
	right: 80px;
	display: flex;
	gap: 16px;
	z-index: 2;
}

.setup-item {
	background: rgba(255, 255, 255, 0.95);
	border: 1px solid #e1e6f2;
	border-radius: 16px;
	padding: 8px 12px 8px 14px;
	box-shadow: 0 8px 18px rgba(40, 57, 108, 0.12);
	cursor: pointer;
	transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
	display: inline-flex;
	align-items: center;
	gap: 6px;
}

.setup-text {
	font-size: 13px;
	font-weight: 600;
	color: #2b3347;
	letter-spacing: 0.2px;
}

.setup-item::after {
	content: "";
	width: 6px;
	height: 6px;
	border-right: 2px solid #9aa1b3;
	border-bottom: 2px solid #9aa1b3;
	transform: rotate(45deg);
	margin-top: -2px;
}

.setup-item:hover {
	transform: translateY(-1px);
	border-color: #c9d2f4;
	box-shadow: 0 12px 24px rgba(40, 57, 108, 0.16);
}

.setup-item:active {
	transform: translateY(0);
}

.auth-card {
	width: 1200px;
	height: 680px;
	display: flex;
	background: #ffffff;
	border-radius: 28px;
	overflow: hidden;
	box-shadow: 0 24px 60px rgba(40, 57, 108, 0.16);
	position: relative;
	z-index: 1;
}

.auth-left {
	width: 42%;
	padding: 48px 56px;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.logo {
	// margin-bottom: 24px;
	display: flex;
	align-items: center;
}

.logo-image {
	height: 200px;
	margin-bottom: -20px;
}

.logo-title {
	font-size: 20px;
	font-weight: 600;
	color: #2b3347;
	letter-spacing: 2px;
	position: relative;
	margin-top: 30px;
	padding: 10px 0px 10px 14px;
}

.logo-title::before {
	content: "";
	position: absolute;
	left: 0;
	top: 4px;
	bottom: 4px;
	width: 4px;
	border-radius: 2px;
	background: linear-gradient(180deg, #4f6ef7, #5b7bff);
}

.welcome {
	font-size: 24px;
	font-weight: 600;
	color: #2b3347;
	margin-bottom: 8px;
}

.from {
	width: 100%;
	color: #2b3347;
	background-color: transparent;
	border-radius: 0;
	border: none;
	padding: 0;
	box-sizing: border-box;
}

.from-input {
	width: 100%;
	padding: 8px 0 0 0;
}

.field-label {
	font-size: 13px;
	color: #6b7280;
	margin: 12px 0 6px;
}

.from-input-item {
	display: flex;
	padding: 16px 0 0 0;
}

.loginButton {
	width: 100%;
	height: 48px;
	background: linear-gradient(135deg, #4f6ef7, #5b7bff);
	border-radius: 14px;
	border: none;
	font-size: 16px;
	font-weight: 600;
	color: #ffffff;
	box-shadow: 0 12px 24px rgba(79, 110, 247, 0.28);
}

.from-input-item1 {
	display: flex;
	align-items: center;
	width: 100%;
	height: 48px;
	background: #f6f8fc;
	border-radius: 12px;
	border: 1px solid #e6eaf2;
	padding: 10px 14px;
	box-sizing: border-box;
	margin-bottom: 4px;
	gap: 10px;
}

.from-input-item1 img {
	width: 18px;
	height: 18px;
	opacity: 0.6;
}

.version {
	margin-top: 24px;
	font-size: 12px;
	color: #9aa1b3;
}

.auth-right {
	width: 58%;
	background: linear-gradient(160deg, #f8f9fc 0%, #f0f3f9 100%);
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}

.illustration {
	width: 90%;
	height: auto;
	max-height: 88%;
	object-fit: contain;
	filter: drop-shadow(0 30px 60px rgba(40, 57, 108, 0.25));
}

.el-input__wrapper {
	--el-input-focus-border-color: rgba(255, 255, 0, 0);
	--el-menu-hover-bg-color: rgba(255, 255, 0, 0);
}
</style>

<style scoped lang="less">
::v-deep(.el-input__wrapper) {
	background-color: transparent;
	box-shadow: none;
}

::v-deep(.el-input__inner) {
	color: #2b3347;
}

::v-deep(.el-input__inner::placeholder) {
	color: #9aa1b3;
}
</style>
