<template>
	<view class="login-container">
		<view class="logo">
			<image :src="logoImage" mode="aspectFill"></image>
		</view>
		<view class="form">
			<view class="form-item">
				<view class="form-input-item">
					<view class="form-input-left">
						<uni-icons class="form-input-icons" type="person" size="24"></uni-icons>
					</view>
					<view class="form-input-content">
						<input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
							v-model="loginForm.phone" />
					</view>
				</view>
			</view>
			<!-- 获取验证码 -->
			<view class="form-item">
				<view class="form-input-item">
					<view class="form-input-left">
						<uni-icons class="form-input-icons" type="email" size="24"></uni-icons>
					</view>
					<view class="form-input-content code-input-wrapper">
						<input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
							v-model="loginForm.phoneCode" />
						<button class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
							{{codeName}}
						</button>
					</view>
				</view>
			</view>
			<view class="form-btn-container">
				<button class="form-btn" @click="submitLogin">登录</button>
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="form-btn-container">
				<button type="primary" class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
					微信手机号一键登录
				</button>
			</view>
			<!-- #endif -->
		</view>
		<model :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
			<view class="code-input-container">
				<input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
				<view class="code-canvas-wrapper">
					<img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
				</view>
			</view>
		</model>
	</view>
</template>

<script>
	import {
		login,
		getImgCode,
		sendPhoneCode
	} from '@/request/api/login'
	import {
		isTel
	} from '@/uitls/validate'
	import model from "@/components/login/model.vue";
	export default {
		data() {
			return {
				logoImage: require('@/static/image/login/logo.png'),
				loginForm: {
					phone: '18888888888',
					phoneCode: '888888'
				},
				captchaShow: false,
				imgCode: '',
				codeImg: '',
				codeName: '获取验证码', //获取验证码text
				isDisabled: false, //获取验证码按钮状态
			}
		},
		components: {
			model
		},
		onLoad() {},
		methods: {
			getImageCode() {
				if (!isTel(this.loginForm.phone)) return uni.showToast({
					title: '请输入正确的手机号',
					icon: "none"
				})
				if (this.isDisabled) return;
				this.imgCodeFunc();
				this.captchaShow = true;
			},
			imgCodeFunc() {
				getImgCode().then(res => {
					if (res.code === 200) {
						this.codeImg = res.data.pngBase64;
					}
				})
			},
			confirmHandle() {
				if (!this.imgCode) return uni.showToast({
					title: '请输入图片验证码',
					icon: "none"
				})
				sendPhoneCode(this.loginForm.phone, this.imgCode).then(res => {
					if (res.code === 200) {
						this.codeReset();
						uni.showToast({
							title: '发送成功',
							icon: "none"
						})
					}
				})
			},
			codeReset() {
				//重置获取验证码倒计时
				let codeNumber = 60;
				codeNumber--;
				this.handleCode(codeNumber);
				let codeRestFn = setInterval(() => {
					codeNumber--;
					this.handleCode(codeNumber);
					if (codeNumber == 0) clearInterval(codeRestFn); //停止
				}, 1000);
			},
			handleCode(codeNumber) {
				//code操作
				this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
				this.isDisabled = codeNumber == 0 ? false : true;
			},
			wxPhoneLogin(e) { //微信手机号一键登录
				this.login({
					pCode: e.detail.code
				})
			},
			submitLogin() { //手机验证码登录
				if (!this.loginForm.phoneCode) return uni.showToast({
					title: '请输入短信验证码',
					icon: "none"
				})
				this.login(this.loginForm);
			},
			login(postData) {
				uni.showLoading({
					title: '登录中'
				})
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						if (postData.phone != '18888888888') postData['code'] = loginRes.code;
						login(postData).then(this.successFunc)
					}
				})
				// #endif
				// #ifndef MP-WEIXIN
				login(postData).then(this.successFunc);
				// #endif
			},
			successFunc(res) {
				if (res.code === 200) {
					uni.showToast({
						title: '登录成功',
						icon: "none"
					})
					console.log(res.data);
					setTimeout(() => {
						uni.hideLoading();
						// uni.navigateBack();
					}, 400)
				} else {
					uni.hideLoading();
				}

			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.logo {
			margin-top: 80rpx;
			width: 150rpx;
			height: 150rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		//弹出输入框
		.code-input-container {
			display: flex;
			align-items: center;
			margin-top: 20rpx;

			.code-input {
				background: #F7F8FA;
				width: 306rpx;
				height: 84rpx;
				line-height: 84rpx;
				padding: 0 20rpx;
				border-radius: 20rpx;
				box-sizing: border-box;

				&::placeholder {
					line-height: 84rpx;
				}
			}

			.code-canvas-wrapper {
				flex: 1;

				.code-image {
					width: 100%;
					height: 84rpx;
				}
			}
		}

		.form {
			margin-top: 70rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 686rpx;
			margin: 70rpx auto;

			// 登录表单项
			.form-item {
				width: 100%;
				border-radius: 200rpx;
				height: 100rpx;
				background-color: $uni-white;
				display: flex;
				margin-bottom: 50rpx;


				&:last-of-type {
					margin-bottom: 0;
				}
			}

			// 登录的input项
			.form-input-item {
				height: 100%;
				width: 100%;
				display: flex;
				align-items: center;
				box-sizing: border-box;

				// input左边区域
				.form-input-left {
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					.form-input-icons {
						/* #ifdef H5 */
						color: $uni-secondary-color !important;

						/* #endif */
						/* #ifdef MP-WEIXIN */
						text {
							color: $uni-secondary-color !important;
						}

						/* #endif */
					}
				}

				// 手机号信息
				.phone-info {
					margin-left: 20rpx;

					// 手机号信息文字
					.phone-info-txt {
						margin-right: 8rpx;
					}

					// 下拉按钮图片大小
					.phone-info-img {
						width: 24rpx;
						height: 24rpx;
					}
				}

				// input内容区域
				.form-input-content {
					flex: 1 1 auto;
					padding-right: 20rpx;
					position: relative;

					// 具体的input样式
					.form-input {
						width: 100%;
						padding-left: 20rpx;

						// 覆盖uni的默认的提示
						::v-deep .uni-input-placeholder {
							color: #C8C9CC;
						}
					}

				}
			}

			// 验证码
			.code-input-wrapper {
				display: flex;
				align-items: center;

				.code-input-btn {
					width: auto;
					width: 200rpx;
					height: 64rpx;
					font-size: 24rpx;
					padding: 12rpx 40rpx;
					white-space: nowrap;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 200rpx;
					background: $uni-primary;
					color: #fff;

					&.disabled {
						opacity: 0.5;
					}

					&::after {
						display: none;
					}
				}
			}

			// 登录按钮容器
			.form-btn-container {
				margin-top: 20rpx;
				width: 100%;
			}

			// 登录按钮
			.form-btn {
				height: 88rpx;
				border-radius: 200rpx;
				padding: 24rpx 32rpx;
				line-height: 40rpx;
				color: $uni-white;
				background-color: $uni-primary;
				font-size: 32rpx;

				&.disabled {
					opacity: 0.5;
				}

				&::after {
					display: none;
				}
			}

			.wx-btn {
				height: 88rpx;
				padding: 24rpx 32rpx;
				border-radius: 200rpx;
				line-height: 40rpx;
				font-size: 32rpx;
				color: $uni-white;
				background-color: $uni-success;

				&::after {
					display: none;
				}
			}

		}
	}
</style>