<template>
	<view class="content" style="position: relative;">
		<u-navbar title="登录" title-color='#111' title-size="36" back-icon-color="#111" :custom-back="pageBack">
		</u-navbar>
		<view class="avatorWrapper">
			<view class="avator">
				<image class="img" style="margin-top: 80px;" src="../../static/login_icon.png" mode="widthFix"></image>
			</view>
		</view>
		<u-form :model="form" ref="uForm" class="form">
			<u-form-item label="" label-width="130" border-bottom="false">
				<view class="lia inputWrapper">
					<img style="width: 20px;margin: 0 5px;" src="../../static/login_phone_icon.png" />
					<u-input class="margin-left" type="number" placeholder="请输入您的手机号" v-model="form.phone"
						maxlength="11" />
				</view>

			</u-form-item>
			<view class="lia inputWrapper ">
				<u-form-item label="" label-width="130">
					<img style="width: 20px;margin: 0 5px;" src="../../static/login_code_icon.png" />
					<u-input class="margin-left" type="number" placeholder="请输入您的验证码" v-model="form.code"
						maxlength="6" />
					<u-verification-code class="huoquzhong" start-text="获取验证码" end-text="获取验证码" change-text="XS"
						:seconds="seconds" ref="uCode" @change="codeChange">

					</u-verification-code>
					<view class="huoquzhong" slot="right" :disabled="submissionFlag" type="primary" size="mini"
						@click="getCode">{{tips}}
					</view>
				</u-form-item>
			</view>

		</u-form>
		<view class="loginBtn" @click="submit">
			<text class="btnValue">登录</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				form: {
					phone: '',
					code: '',
				},
				seconds: 60,
				submissionFlag: false,
				tips: '',
			}
		},
		onLoad() {

		},
		methods: {
			pageBack() {
				uni.switchTab({
					url: '../index/index'
				})
			},
			submit() {
				// console.log(this.form);
				if (!this.$u.test.mobile(String(this.form.phone))) {
					this.$api.msg("手机号格式不正确！")
				} else if (!this.$u.test.code(this.form.code, 6)) {
					this.$api.msg("验证码格式不正确！")
				} else {
					this.$u.api.smsLogin(this.form).then(res => {
						// console.log(res)
						if (res.code === 200) {
							//登录成功，跳转首页
							uni.setStorageSync("userInfo", res.data)
							uni.setStorageSync("token", res.data.token)
							// uni.setStorageSync("secret", res.result.security.secret)
							// uni.switchTab({
							//   url: '../index/index'
							// });
							// uni.reLaunch({
							// 	url: '../index/index'
							// });
							uni.navigateBack({
								delta: 1
							})
						} else {
							this.$api.msg(res.msg);
						}
					})
				}

			},

			getCode() {
				if (!this.$u.test.mobile(String(this.form.phone))) {
					this.$api.msg("手机格式有误")
					return;
				}
				// console.log(this.form)
				this.$u.api.sendCode(this.form.phone).then(res => {
					uni.showLoading({
						title: '正在获取验证码'
					})
					if (res.code === 200) {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$api.msg('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						// console.log(res)
					} else {
						this.$api.msg(res.msg);
					}
				})
			},
			end() {
				this.submissionFlag = false;
			},
			start() {
				this.submissionFlag = true;
			},
			codeChange(text) {
				this.tips = text;
			},

		}
	}
</script>

<style>
	.lia {
		width: 690rpx;
		height: 105rpx;
		margin: 0 auto;
		border-bottom: 1rpx solid #f1f1f1;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: space-between;
	}

	.shuruqian {
		height: 50rpx;
		width: 60%;
		font-size: 32rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
	}

	.shuruhou {
		height: 50rpx;
		width: 60%;
		font-size: 36rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
	}

	.huoqu {
		font-size: 32rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #2d8bff;
	}

	.huoquzhong {
		/*width: 110px;*/
		font-size: 13px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FC6035;
	}

	.icon_close {
		float: right;
		display: block;
		margin-top: -45px;
		margin-right: 0px;
	}

	.content {
		background: white;
		width: 100vw;
		height: 100vh;
	}

	.avatorWrapper {
		height: 30vh;
		width: 100vw;
		display: flex;
		justify-content: center;
		align-items: flex-end;
	}

	.avator {
		width: 150px;
		height: 200px;
		overflow: hidden;
	}

	.avator .img {
		width: 100%
	}

	.form {
		padding: 0 100upx;
		margin-top: 50px;
	}

	.inputWrapper {
		width: 100%;
		height: 45px;
		background: #EEEEEE;
		border-radius: 25px;
		box-sizing: border-box;
		padding: 0 20px;
		margin-top: 25px;
	}

	.inputWrapper .input {
		width: 100%;
		height: 100%;
		text-align: left;
		font-size: 15px;
	}

	.loginBtn {
		position: absolute;
		width: 75%;
		height: 43px;
		background: #FC6035;
		border-radius: 27px;
		margin-top: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 200px 54px 0px 54px;
		bottom: 20px;
	}

	.loginBtn .btnValue {
		color: white;
	}

	.forgotBtn {
		text-align: center;
		color: #EAF6F9;
		font-size: 15px;
		margin-top: 20px;
	}
</style>
