<template>
	<view class="app-container">
		<view class="banner">
			<image src="@/static/mine/apply.png" mode="widthFix"></image>
		</view>
		<view class="main">
			<view class="title">请输入您登录企业微信的手机号码</view>
			
			<view class="form">
				<view class="row">
					<view class="row-c">
						<view class="left">+86</view>
						<view class="input">
							<input type="text" placeholder="请输入手机号码" v-model="phone">
						</view>
					</view>
				</view>
				<view class="row">
					<view class="row-c">
						<view class="left">验证码</view>
						<view class="input">
							<input type="text" placeholder="请输入短信验证码" v-model="code">
						</view>
					</view>
					<view class="btn" :class="!phone || countDown != 60 ? 'disabled': ''" @tap="getCode">{{countDown == 60 ? getCodeText : countDown+'s'}}</view>
				</view>
			</view>
			<view class="tips" >{{countDown != 60 ? '验证码已发送，请注意查收短信':''}}</view>
			
			<view class="button" :class="!phone || !code ? 'disabled': ''" @tap="submitForm">确定</view>
			
		</view>
		
		<view class="code-container" v-show="isShowCode">
			<view class="title">请完成安全验证</view>
			<pt-images-verification ref="verification" :top="codeObj.yHeight" :bgImg="codeObj.bigImage" :maskImg="codeObj.smallImage" :isSuccess="codeObj.isSuccess" :isFail="codeObj.isFail" @refresh="refresh" @finish="finish"></pt-images-verification>
			<view class="button" @tap="isShowCode = false">关闭</view>
		</view>
		<div class="global-mask" v-show="isShowCode"></div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				code: '',
				getCodeText: '获取验证码',
				countDown: 60,
				timer: null,
				
				isShowCode: false,
				codeObj: {
					bigImage: '',
					smallImage: '',
					key: '',
					yHeight: '',
					isSuccess: false,
					isFail: false,
				}
			}
		},
		onLoad() {
			
		},
		methods: {
			// 获取验证码
			getCode() {
				if(!this.phone) {
					this.$toast('请先填写手机号码');
					return false;
				}
				if(this.countDown != 60) {
					this.$toast('请勿频繁获取验证码');
					return false;
				}
				if(!(/^1[3456789]\d{9}$/.test(this.phone))) {
					this.$toast('请填写正确的手机号码');
					return false;
				}
				
				// this.init();
				this.isShowCode = true;
				this.$refs.verification.refresh();
				
			},
			
			// 提交表单
			submitForm() {
				if(!this.phone) {
					this.$toast('请先填写手机号码');
					return false;
				}
				if(!(/^1[3456789]\d{9}$/.test(this.phone))) {
					this.$toast('请填写正确的手机号码');
					return false;
				}
				if(!this.code) {
					this.$toast('请先填写验证码');
					return false;
				}
				
				this.$axios({
					url: '/worker/bind',
					params: {
						phone: this.phone,
						code: this.code
					},
					isLoading: 1
				}).then(res => {
					this.$successToast('申请成功');
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1500)
				})
				
			},
			
			// 获取图片验证码
			init(){
				this.$axios({
					url: '/common/getVerifi',
					method: 'get',
					params: {},
					isLoading: 1
				}).then(res => {
					this.codeObj = res.data;
				})
			},
			// 刷新验证码
			refresh(){
				this.init()
			},
			// 验证结束
			finish(value){
				console.log(value);
				this.$axios({
					url: '/worker/sms/send',
					params: {
						phone: this.phone,
						key: this.codeObj.key,
						vrifyCode: Math.round(value)
					},
					isLoading: 1
				}).then(res => {
					this.codeObj.isSuccess = true;
					this.codeObj.isFail = false;
					setTimeout(() => {
						this.isShowCode = false;
						this.$toast('短信发送成功');
					}, 1500)
					this.countDown--;
					this.timer = setInterval(() => {
						this.countDown--;
						if (this.countDown == 0) {
							this.countDown = 60;
							this.getCodeText = '重新获取';
							clearInterval(this.timer)
						}
					}, 1000)
				}).catch(res => {
					this.codeObj.isSuccess = false;
					this.codeObj.isFail = true;
					setTimeout(() => {
						this.$refs.verification.refresh();
					}, 1500)
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
	}
	.banner {
		image {
			width: 100%;
		}
	}
	.code-container {
		position: fixed;
		top: calc(50vh - 150px);
		left: calc(50vw - 170px);
		z-index: 999;
		background: #FFFFFF;
		padding: 20px;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		.title {
			font-size: 28rpx;
			color: #333333;
			width: 100%;
			text-align: left;
			margin-bottom: 10px;
		}
		.button {
			margin-top: 20px;
			color: #666666;
			width: 200rpx;
			line-height: 60rpx;
			border: 1px solid #eaeaea;
			border-radius: 10rpx;
			text-align: center;
		}
	}
	.main {
		padding: 50rpx;
		.title {
			font-size: 32rpx;
			color: #333333;
			text-align: center;
		}
		.button {
			margin-top: 120rpx;
			width: 100%;
			text-align: center;
			line-height: 88rpx;
			border-radius: 88rpx;
			font-size: 32rpx;
			color: #FFFFFF;
			background: linear-gradient(-90deg,#ff3f42 0%, #fe781f 100%);
			&.disabled {
				background: #C1C1C1;
			}
		}
		.tips {
			font-size: 24rpx;
			color: #FE781F;
			line-height: 24rpx;
			margin-top: 20rpx;
			height: 24rpx;
			margin-left: 10rpx;
		}
		.form {
			margin-top: 60rpx;
			.row {
				&:last-child {
					margin-top: 40rpx;
					display: flex;
					align-items: flex-end;
					.btn {
						width: 180rpx;
						height: 50rpx;
						border-radius: 50rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 24rpx;
						color: #FFFFFF;
						background: linear-gradient(-90deg,#ff3f42 0%, #fe781f 100%);
						margin-left: 30rpx;
						&.disabled {
							background: #C1C1C1;
						}
					}
				}
				.row-c {
					height: 60rpx;
					display: flex;
					align-items: center;
					border-bottom: 1px solid #D1D1D1;
					.left {
						width: 140rpx;
						height: 28rpx;
						border-right: 1px solid #D1D1D1;
						font-size: 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						flex-shrink: 0;
					}
					.input {
						width: 100%;
						input {
							font-size: 30rpx;
							width: 100%;
							padding: 0 25rpx;
							box-sizing: border-box;
						}
					}
				}
			}
		}
	}
</style>
