<template>
	<view class="login-page" :style="{ paddingTop: statusBarHeight + 'px' }">
		<view class="brand-area">
			<view class="logo-icon">
				<text class="logo-symbol">◆</text>
			</view>
			<text class="app-name">Novalon 教练端</text>
			<text class="app-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" :loading="loading" :disabled="loading || !username || !password" @click="handleLogin">
				{{ loading ? '登录中...' : '登 录' }}
			</button>
		</view>

		<view class="footer-tip">
			<text>Novalon 健身房管理系统</text>
		</view>
	</view>
</template>

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

	export default {
		data() {
			return {
				statusBarHeight: 0,
				username: '',
				password: '',
				showPassword: false,
				loading: false
			}
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync()
			this.statusBarHeight = systemInfo.statusBarHeight || 20

			if (store.isLoggedIn) {
				uni.reLaunch({ url: '/pages/index/index' })
			}
		},
		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: #F5F7FA;
		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-icon {
		width: 80px;
		height: 80px;
		background: rgba(0,230,118,0.15);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 20px;
	}
	.logo-symbol {
		font-size: 36px;
		color: #00C853;
	}
	.app-name {
		font-size: 28px;
		font-weight: 700;
		color: #1E1E1E;
		letter-spacing: 1px;
	}
	.app-slogan {
		font-size: 14px;
		color: #7A7E84;
		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: #FFFFFF;
		border-radius: 40px;
		display: flex;
		align-items: center;
		padding: 0 18px;
		margin-bottom: 14px;
		box-shadow: 0 4px 12px rgba(0,0,0,0.06);
	}
	.input-icon {
		font-size: 18px;
		margin-right: 10px;
		color: #7A7E84;
	}
	.login-input {
		flex: 1;
		height: 100%;
		font-size: 15px;
		color: #1E1E1E;
	}
	.toggle-pwd {
		font-size: 18px;
		color: #7A7E84;
		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: #BDBDBD;
	}
</style>
