<template>
	<view class="login-page">
		<view class="brand-stage">
			<view class="brand-stage__glow brand-stage__glow--one"></view>
			<view class="brand-stage__glow brand-stage__glow--two"></view>
			<view class="status-bar"></view>
			<view class="brand-stage__card">
				<view class="brand-mark">
					<view class="brand-mark__leaf brand-mark__leaf--left"></view>
					<view class="brand-mark__leaf brand-mark__leaf--right"></view>
					<view class="brand-mark__stem"></view>
				</view>
				<text class="brand-stage__name">绿纤</text>
				<text class="brand-stage__slogan">专属您的美丽管家</text>
			</view>
		</view>

		<view class="login-card">
			<text class="login-card__title">欢迎回来</text>
			<text class="login-card__subtitle">登录以享受专属美丽服务</text>

			<button class="wx-btn" open-type="getPhoneNumber" @getphonenumber="onWxLogin">
				<view class="wx-btn__icon"></view>
				<text class="wx-btn__text">微信一键登录</text>
			</button>

			<view class="divider-row">
				<view class="divider-row__line"></view>
				<text class="divider-row__text">或</text>
				<view class="divider-row__line"></view>
			</view>

			<view class="phone-form">
				<view class="form-field" :class="{ 'form-field--focus': phoneFocus }">
					<text class="form-field__prefix">+86</text>
					<view class="form-field__line"></view>
					<input
						class="form-field__input"
						v-model="phone"
						type="number"
						maxlength="11"
						placeholder="请输入手机号"
						placeholder-class="form-field__placeholder"
						@focus="phoneFocus = true"
						@blur="phoneFocus = false"
					/>
					<view v-if="phone.length > 0" class="form-field__clear" @click="phone = ''">
						<view class="ico-close"></view>
					</view>
				</view>

				<view class="form-field" :class="{ 'form-field--focus': codeFocus }">
					<input
						class="form-field__input"
						v-model="smsCode"
						type="number"
						maxlength="6"
						placeholder="请输入验证码"
						placeholder-class="form-field__placeholder"
						@focus="codeFocus = true"
						@blur="codeFocus = false"
					/>
					<view class="code-btn" :class="{ 'code-btn--disabled': countdown > 0 }" @click="sendCode">
						<text>{{ countdown > 0 ? countdown + 's' : '获取验证码' }}</text>
					</view>
				</view>

				<button class="submit-btn" :class="{ 'submit-btn--active': canSubmit }" @click="onPhoneLogin">
					登录
				</button>
			</view>
		</view>

		<view class="agreement">
			<view class="agreement__check" @click="agreed = !agreed">
				<view class="agreement__box" :class="{ 'agreement__box--checked': agreed }">
					<view v-if="agreed" class="agreement__tick"></view>
				</view>
			</view>
			<view class="agreement__content">
				<text class="agreement__text">已阅读并同意</text>
				<text class="agreement__link" @click.stop="goProtocol('user')">《用户协议》</text>
				<text class="agreement__text">和</text>
				<text class="agreement__link" @click.stop="goProtocol('privacy')">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'login',
	data() {
		return {
			phone: '',
			smsCode: '',
			countdown: 0,
			agreed: false,
			timer: null,
			phoneFocus: false,
			codeFocus: false
		}
	},
	computed: {
		canSubmit() {
			return this.phone.length === 11 && this.smsCode.length >= 4
		}
	},
	beforeDestroy() {
		if (this.timer) clearInterval(this.timer)
	},
	methods: {
		onWxLogin(e) {
			if (!this.agreed) {
				uni.showToast({ title: '请先同意用户协议', icon: 'none' })
				return
			}
			uni.showLoading({ title: '登录中...' })
			setTimeout(() => {
				uni.setStorageSync('member_token', 'mock_wx_token_' + Date.now())
				uni.setStorageSync('memberInfo', {
					id: '10001',
					nickname: '微信用户',
					avatar: '',
					phone: '138****8888'
				})
				uni.hideLoading()
				uni.showToast({ title: '登录成功', icon: 'success' })
				setTimeout(() => {
					uni.switchTab({ url: '/pages/home/home' })
				}, 800)
			}, 1200)
		},

		sendCode() {
			if (this.countdown > 0) return
			if (this.phone.length !== 11) {
				uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
				return
			}
			uni.showToast({ title: '验证码已发送', icon: 'none' })
			this.countdown = 60
			this.timer = setInterval(() => {
				this.countdown--
				if (this.countdown <= 0) clearInterval(this.timer)
			}, 1000)
		},

		onPhoneLogin() {
			if (!this.agreed) {
				uni.showToast({ title: '请先同意用户协议', icon: 'none' })
				return
			}
			if (!this.canSubmit) {
				uni.showToast({ title: '请填写完整信息', icon: 'none' })
				return
			}
			uni.showLoading({ title: '登录中...' })
			setTimeout(() => {
				uni.setStorageSync('member_token', 'mock_phone_token_' + Date.now())
				uni.setStorageSync('memberInfo', {
					id: '10002',
					nickname: '手机用户',
					avatar: '',
					phone: this.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
				})
				uni.hideLoading()
				uni.showToast({ title: '登录成功', icon: 'success' })
				setTimeout(() => {
					uni.switchTab({ url: '/pages/home/home' })
				}, 800)
			}, 1200)
		},

		goProtocol(type) {
			uni.showToast({ title: '协议页待开发', icon: 'none' })
		}
	}
}
</script>

<style lang="scss" scoped>
.login-page {
	min-height: 100vh;
	display: flex;
	flex-direction: column;
	background:
		radial-gradient(circle at top right, rgba(45,180,141,0.26), transparent 28%),
		linear-gradient(180deg, #f8fbfa 0%, #f2f6f7 48%, #edf2f5 100%);
}

.brand-stage {
	position: relative;
	padding: 0 24rpx 110rpx;
	background: linear-gradient(160deg, #1d8b6d 0%, #27b08a 48%, #79dcc1 100%);
	overflow: hidden;
}

.status-bar {
	height: var(--status-bar-height, 0px);
}

.brand-stage__glow {
	position: absolute;
	border-radius: 50%;
	background: rgba(255,255,255,0.12);
}

.brand-stage__glow--one {
	width: 320rpx;
	height: 320rpx;
	top: -80rpx;
	right: -80rpx;
}

.brand-stage__glow--two {
	width: 220rpx;
	height: 220rpx;
	left: -60rpx;
	bottom: 120rpx;
}

.brand-stage__card {
	margin-top: 24rpx;
	padding: 44rpx 32rpx;
	border-radius: 40rpx;
	background: rgba(255,255,255,0.16);
	backdrop-filter: blur(18px);
	-webkit-backdrop-filter: blur(18px);
	border: 1rpx solid rgba(255,255,255,0.2);
	box-shadow: 0 32rpx 64rpx rgba(8,68,54,0.2);
	text-align: center;
}

.brand-mark {
	position: relative;
	width: 110rpx;
	height: 110rpx;
	margin: 0 auto 24rpx;
	border-radius: 32rpx;
	background: rgba(255,255,255,0.18);
	display: flex;
	align-items: center;
	justify-content: center;
}

.brand-mark__leaf {
	position: absolute;
	width: 20rpx;
	height: 34rpx;
	background: #fff;
	border-radius: 999rpx 999rpx 999rpx 6rpx;
}

.brand-mark__leaf--left {
	left: 32rpx;
	top: 24rpx;
	transform: rotate(-34deg);
}

.brand-mark__leaf--right {
	right: 32rpx;
	top: 22rpx;
	transform: scaleX(-1) rotate(-34deg);
}

.brand-mark__stem {
	position: absolute;
	left: 50%;
	bottom: 24rpx;
	width: 6rpx;
	height: 24rpx;
	background: #fff;
	border-radius: 6rpx;
	transform: translateX(-50%);
}

.brand-stage__name {
	display: block;
	font-size: 56rpx;
	font-weight: 800;
	letter-spacing: 8rpx;
	color: #fff;
}

.brand-stage__slogan {
	display: block;
	margin-top: 16rpx;
	font-size: 26rpx;
	letter-spacing: 3rpx;
	color: rgba(255,255,255,0.84);
}

.login-card {
	margin: -72rpx 24rpx 0;
	padding: 36rpx 28rpx 34rpx;
	border-radius: 36rpx;
	background: rgba(255,255,255,0.96);
	box-shadow: 0 28rpx 60rpx rgba(15,23,42,0.12);
}

.login-card__title {
	display: block;
	font-size: 40rpx;
	font-weight: 700;
	color: $text-primary;
}

.login-card__subtitle {
	display: block;
	margin-top: 12rpx;
	font-size: 25rpx;
	color: $text-secondary;
}

.wx-btn {
	margin-top: 32rpx;
	height: 96rpx;
	border-radius: 26rpx;
	background: linear-gradient(135deg, #22b18a 0%, #6fdab9 100%);
	box-shadow: 0 18rpx 36rpx rgba(45,180,141,0.22);
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	font-size: 30rpx;
	font-weight: 700;
}

.wx-btn::after {
	border: none;
}

.wx-btn__icon {
	width: 28rpx;
	height: 22rpx;
	border: 3rpx solid #fff;
	border-radius: 14rpx;
	position: relative;
	margin-right: 12rpx;
}

.wx-btn__icon::after {
	content: '';
	position: absolute;
	left: 6rpx;
	bottom: -7rpx;
	width: 10rpx;
	height: 10rpx;
	border-left: 3rpx solid #fff;
	border-bottom: 3rpx solid #fff;
	background: transparent;
	transform: rotate(-12deg);
}

.divider-row {
	margin: 28rpx 0;
	display: flex;
	align-items: center;
}

.divider-row__line {
	flex: 1;
	height: 1rpx;
	background: rgba(134,144,156,0.16);
}

.divider-row__text {
	margin: 0 18rpx;
	font-size: 24rpx;
	color: $text-tertiary;
}

.phone-form {
	display: flex;
	flex-direction: column;
	gap: 16rpx;
}

.form-field {
	height: 96rpx;
	padding: 0 24rpx;
	border-radius: 24rpx;
	background: rgba(247,249,251,0.96);
	display: flex;
	align-items: center;
	border: 2rpx solid transparent;
}

.form-field--focus {
	border-color: rgba(45,180,141,0.3);
	box-shadow: 0 0 0 8rpx rgba(45,180,141,0.08);
}

.form-field__prefix {
	font-size: 28rpx;
	font-weight: 600;
	color: $text-primary;
}

.form-field__line {
	width: 1rpx;
	height: 28rpx;
	background: rgba(134,144,156,0.2);
	margin: 0 16rpx;
}

.form-field__input {
	flex: 1;
	font-size: 28rpx;
	color: $text-primary;
}

.form-field__placeholder {
	color: $text-tertiary;
}

.form-field__clear {
	width: 40rpx;
	height: 40rpx;
	border-radius: 50%;
	background: rgba(134,144,156,0.12);
	display: flex;
	align-items: center;
	justify-content: center;
}

.code-btn {
	padding: 14rpx 18rpx;
	border-radius: 18rpx;
	background: rgba(45,180,141,0.1);
	font-size: 24rpx;
	font-weight: 600;
	color: $brand-primary;
}

.code-btn--disabled {
	opacity: 0.6;
}

.submit-btn {
	height: 96rpx;
	border-radius: 26rpx;
	background: linear-gradient(135deg, #cfd7de 0%, #bec6cf 100%);
	color: #fff;
	font-size: 30rpx;
	font-weight: 700;
}

.submit-btn::after {
	border: none;
}

.submit-btn--active {
	background: linear-gradient(135deg, #22b18a 0%, #6fdab9 100%);
	box-shadow: 0 18rpx 36rpx rgba(45,180,141,0.24);
}

.agreement {
	padding: 28rpx 40rpx 44rpx;
	display: flex;
	align-items: flex-start;
	justify-content: center;
}

.agreement__box {
	width: 28rpx;
	height: 28rpx;
	border-radius: 50%;
	border: 2rpx solid rgba(134,144,156,0.36);
	background: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 4rpx;
}

.agreement__box--checked {
	background: $brand-primary;
	border-color: $brand-primary;
}

.agreement__tick {
	width: 12rpx;
	height: 6rpx;
	border-left: 2rpx solid #fff;
	border-bottom: 2rpx solid #fff;
	transform: rotate(-45deg);
}

.agreement__content {
	margin-left: 12rpx;
	font-size: 22rpx;
	line-height: 1.8;
}

.agreement__text {
	color: $text-tertiary;
}

.agreement__link {
	color: $brand-primary;
	font-weight: 500;
}

.ico-close {
	width: 16rpx;
	height: 16rpx;
	position: relative;
}

.ico-close::before,
.ico-close::after {
	content: '';
	position: absolute;
	top: 50%;
	left: 0;
	width: 100%;
	height: 3rpx;
	background: $text-tertiary;
	border-radius: 3rpx;
}

.ico-close::before { transform: rotate(45deg); }
.ico-close::after { transform: rotate(-45deg); }
</style>
