<template>
	<view class="login-container">
		<view class="login-box-bg">
			<image
				src="https://images.unsplash.com/photo-1519751138087-5bf79df62d5b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wyMDUzMDJ8MHwxfHNlYXJjaHwyMHx8YmFja2dyb3VuZHxlbnwxfHx8fDE3MjExOTc4NjJ8MA&ixlib=rb-4.0.3&q=80&w=1080"
				mode="aspectFill"></image>
		</view>
		<view class="login-box">
			<view class="title">登录</view>
			<view class="sub-title">欢迎使用有极智慧园区管理软件</view>
			<view class="form">
				<view class="form-item">
					<view class="form-item-label">手机号</view>
					<view class="form-input-item">
						<uni-icons class="form-input-icons" type="person" size="24"></uni-icons>
						<view class="form-input-content">
							<input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
								v-model="form.phone" />
						</view>
					</view>
				</view>
				<!-- 获取验证码 -->
				<view class="form-item">
					<view class="form-item-label">验证码</view>
					<view class="form-input-item">
						<uni-icons class="form-input-icons" type="email" size="24"></uni-icons>
						<view class="form-input-content">
							<input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
								v-model="form.phoneCode" />
						</view>
						<text class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
							{{codeName}}
						</text>
					</view>
				</view>
				<view class="form-btn-container">
					<button class="form-btn" @click="submitLogin">登录</button>
				</view>
			</view>
		</view>
		<view class="login-bottom">
			<view class="line-text">or</view>
			<button class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
				<uni-icons type="weixin" size="24" color="#43b156"></uni-icons>
			</button>
		</view>
		<image-code :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>
		</image-code>
	</view>
</template>

<script>
	import {
		login,
		getImgCode,
		sendPhoneCode,
		getUserInfo
	} from '@/request/api/login'
	import {
		getProjectList
	} from '@/request/api/project'
	import {
		isTel
	} from '@/uitls/validate'
	import imageCode from "@/components/login/imageCode.vue";
	export default {
		data() {
			return {
				form: {
					phone: '18888888888',
					phoneCode: '888888'
				},
				captchaShow: false,
				imgCode: '',
				codeImg: '',
				codeName: '获取验证码', //获取验证码text
				isDisabled: false, //获取验证码按钮状态
			}
		},
		components: {
			imageCode
		},
		methods: {
			getImageCode() {
				if (!isTel(this.form.phone)) return this.$toast('请输入正确的手机号');
				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 this.$toast('请输入图片验证码');
				sendPhoneCode(this.form.phone, this.imgCode).then(res => {
					if (res.code === 200) {
						this.codeReset();
						this.$toast('发送成功');
						this.imgCode = '';
						this.captchaShow = false;
					} else {
						this.imgCodeFunc();
					}
				})
			},
			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) { //微信手机号一键登录
				if (!e.detail.code) return;
				this.login({
					pCode: e.detail.code
				})
			},
			submitLogin() { //手机验证码登录
				if (!this.form.phoneCode) return this.$toast('请输入短信验证码');
				this.login(this.form);
			},
			login(postData) {
				uni.showLoading({
					title: '登录中'
				})
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						if (postData.pCode) 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.setStorageSync('token', res.data.token);
					getUserInfo().then(user => {
						if (user.code === 200) {
							let userInfo = user.data;
							this.$store.dispatch('app/changeOrganization', userInfo.organization);
							this.$store.dispatch('app/changeUser', userInfo);
							uni.removeStorageSync('chatToken');
							this.$chat.connect(userInfo.userId);
							this.initProjectData(userInfo.projectId);
						} else {
							uni.hideLoading();
						}
					})
				} else {
					uni.hideLoading();
				}
			},
			initProjectData(projectId) {
				getProjectList().then(res => {
					if (res.code == 200) {
						if (res.data.length === 0) return this.successLogin();
						let project = projectId === -1 ? res.data[0] : res.data.find(node => node.id ===
							projectId);
						this.$store.dispatch('app/changeProject', project);
						this.$store.dispatch('app/changeIdentity', project.projectListIdentity[0]);
						this.successLogin();
					} else {
						uni.hideLoading();
					}
				})
			},
			successLogin() {
				uni.setStorageSync('vuex_state', this.$store.state);
				this.$toast('登录成功');
				setTimeout(() => {
					uni.hideLoading();
					uni.navigateBack();
				}, 400)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-container {
		height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;

		.login-box-bg {
			height: 300rpx;

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

		.login-box {
			padding: 0 30rpx;
			flex: 1;
			min-width: 640rpx;
		}

		.title {
			padding-top: 30rpx;
			font-size: 48rpx;
			font-weight: bold;
		}

		.sub-title {
			font-size: 24rpx;
			color: $uni-extra-color;
			margin-top: 10rpx;
		}

		//弹出输入框
		.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 {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 30rpx;

			.form-item {
				width: 100%;
				margin-bottom: 30rpx;
			}

			.form-item-label {
				font-weight: 500;
				margin-bottom: 20rpx;
			}

			// 登录的input项
			.form-input-item {
				height: 88rpx;
				border-radius: 88rpx;
				border: 1px solid $uni-border-1;
				width: 100%;
				display: flex;
				align-items: center;
				box-sizing: border-box;

				.form-input-icons {
					padding: 0 20rpx 0 30rpx;

					.uni-icons {
						color: $uni-secondary-color !important;
					}
				}

				.form-input-content {
					flex: 1;
					width: 0;
				}

				.code-input-btn {
					color: $uni-primary;
					padding: 0 30rpx;
				}
			}

			// 登录按钮容器
			.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;
				}
			}
		}

		.login-bottom {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding-bottom: 30px;

			.wx-btn {
				margin-top: 40rpx;
				height: 80rpx;
				width: 160rpx;
				text-align: center;
				line-height: 80rpx;
				background: #fff;

				&::after {
					border-radius: 80rpx;
					border-color: $uni-border-1;
				}
			}

			.line-text {
				position: relative;
				text-align: center;
				color: $uni-secondary-color;
				width: 100%;

				&::before {
					content: '';
					position: absolute;
					left: 60rpx;
					right: calc(50% + 20px);
					height: 1px;
					background: $uni-border-1;
					top: 50%;
				}

				&::after {
					content: '';
					position: absolute;
					right: 60rpx;
					left: calc(50% + 20px);
					height: 1px;
					background: $uni-border-1;
					top: 50%;
				}
			}
		}
	}
</style>