<template>
	<view class="login-page" :style="{ paddingTop: statusBarHeight + 'px', backgroundColor: brandConfig.secondaryColor }">
		<view class="brand-area">
			<view class="logo-wrap" :style="{ backgroundColor: brandConfig.logoUrl ? 'transparent' : 'rgba(' + brandConfig.primaryColorRgb + ',0.15)' }">
				<image v-if="brandConfig.logoUrl" class="logo-img" :src="brandConfig.logoUrl" mode="aspectFit" />
				<text v-else class="logo-symbol" :style="{ color: brandConfig.primaryColor }">◆</text>
			</view>
			<text class="app-name">{{ brandConfig.brandName }}</text>
			<text class="app-slogan" v-if="brandConfig.slogan">{{ brandConfig.slogan }}</text>
		</view>

		<view class="login-form">
			<view class="input-group">
				<text class="input-icon">&#128100;</text>
				<input
					class="login-input"
					type="text"
					v-model="username"
					placeholder="请输入教练账号"
					placeholder-style="color: #BDBDBD"
				/>
			</view>
			<view class="input-group">
				<text class="input-icon">&#128274;</text>
				<input
					class="login-input"
					:type="showPassword ? 'text' : 'password'"
					v-model="password"
					placeholder="请输入密码"
					placeholder-style="color: #BDBDBD"
				/>
				<text class="toggle-pwd" @click="showPassword = !showPassword">
					{{ showPassword ? '&#128065;' : '&#128064;' }}
				</text>
			</view>

			<button class="login-btn" :style="loginBtnStyle" :loading="loading" :disabled="loading || !username || !password" @click="handleLogin">
				{{ loading ? '登录中...' : '登 录' }}
			</button>
		</view>

		<view class="footer-tip">
			<text>{{ brandConfig.brandName }}</text>
		</view>
	</view>
</template>

<script>
	const coachApi = require('../../api/coach')
	const store = require('../../store/index')
	const brandStore = require('../../store/brand')

	export default {
		data() {
			return {
				statusBarHeight: 0,
				username: '',
				password: '',
				showPassword: false,
				loading: false,
				brandConfig: brandStore.config
			}
		},
		computed: {
		loginBtnStyle() { return 'background-color:' + this.brandConfig.primaryColor + ';color:' + this.brandConfig.secondaryColor }
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync()
			this.statusBarHeight = systemInfo.statusBarHeight || 20

			if (store.isLoggedIn) {
				uni.reLaunch({ url: '/pages/index/index' })
			}

			uni.$on('brand:updated', (config) => { this.brandConfig = config })
		},
		onUnload() {
			uni.$off('brand:updated')
		},
		methods: {
			async handleLogin() {
				if (!this.username.trim()) {
					uni.showToast({ title: '请输入教练账号', icon: 'none' })
					return
				}
				if (!this.password) {
					uni.showToast({ title: '请输入密码', icon: 'none' })
					return
				}
				if (this.loading) return

				this.loading = true
				try {
					const res = await coachApi.login(this.username.trim(), this.password)
					if (res && res.token) {
						store.setLogin(res.token, {
							coachId: res.userId,
							username: res.username
						})
						uni.showToast({ title: '登录成功', icon: 'success' })
						setTimeout(() => {
							uni.reLaunch({ url: '/pages/index/index' })
						}, 500)
					} else {
						uni.showToast({ title: '登录失败：账号或密码错误', icon: 'none' })
					}
				} catch (err) {
					console.error('登录失败:', err)
					let msg = '登录失败，请重试'
					if (err && err.data && err.data.message) {
						msg = err.data.message
					} else if (err && err.message) {
						msg = err.message
					}
					uni.showToast({ title: msg, icon: 'none' })
				} finally {
					this.loading = false
				}
			}
		}
	}
</script>

<style scoped>
	.login-page {
		min-height: 100vh;
		background: #1A1A1A;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 0 32px;
		overflow-x: hidden;
	}

	.brand-area {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 80px;
		margin-bottom: 60px;
	}
	.logo-wrap { width: 120px; height: 120px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-bottom: 24px; overflow: hidden; }
	.logo-img { width: 100%; height: 100%; }
	.logo-symbol { font-size: 52px; color: #00C853; }
	.app-name {
		font-size: 28px;
		font-weight: 700;
		color: #FFFFFF;
		letter-spacing: 1px;
	}
	.app-slogan {
		font-size: 14px;
		color: rgba(255,255,255,0.6);
		margin-top: 8px;
		letter-spacing: 2px;
	}

	.login-form {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.input-group {
		width: 100%;
		height: 52px;
		background: rgba(255,255,255,0.1);
		border-radius: 40px;
		display: flex;
		align-items: center;
		padding: 0 18px;
		margin-bottom: 14px;
	}
	.input-icon {
		font-size: 18px;
		margin-right: 10px;
		color: rgba(255,255,255,0.5);
	}
	.login-input {
		flex: 1;
		height: 100%;
		font-size: 15px;
		color: #FFFFFF;
	}
	.toggle-pwd {
		font-size: 18px;
		color: rgba(255,255,255,0.5);
		padding-left: 8px;
	}

	.login-btn {
		width: 100%;
		height: 52px;
		background: #00E676;
		border-radius: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
		border: none;
		padding: 0;
		margin-top: 10px;
		font-size: 17px;
		font-weight: 700;
		color: #1A1A1A;
		box-shadow: 0 4px 16px rgba(0,230,118,0.35);
	}
	.login-btn::after { border: none; }
	.login-btn[disabled] { opacity: 0.5; }

	.footer-tip {
		position: absolute;
		bottom: 60px;
		font-size: 12px;
		color: rgba(255,255,255,0.3);
	}
</style>
