<template>
	<!-- 验证码登录 -->
	<view>
		<u-navbar title="" autoBack placeholder></u-navbar>
		<view class="card_login">
			<view class="card_head_logo">
				<image class="mine_image" src="../static/logo.png" mode=""></image>
				<view class="welcome_title">欢迎登录宝智达冷链运输</view>
			</view>
			<u-form labelPosition="left" :model="formList" :rules="rules" ref="uForm">
				<u-form-item label="手机号" prop="phone" labelWidth="80" borderBottom ref="item1">
					<u-input v-model="formList.phone" border="none" placeholder="请填写手机号"></u-input>
				</u-form-item>
				<u-form-item label="验证码" prop="verifyCode" labelWidth="80" borderBottom>
					<u-input v-model="formList.verifyCode" border="none" placeholder="请填写验证码"></u-input>
					<u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini"
						:disabled="disabled1"></u-button>
				</u-form-item>
			</u-form>
			<u-code ref="uCode" @change="codeChange" seconds="90" @start="disabled1 = true"
				@end="disabled1 = false"></u-code>
			<view class="btn_login">
				<view class="title_register" @click="posswordLogin">密码登录</view>
				<view class="title_register" @click="goRegister">去注册</view>
			</view>
			<view class="card_hint">
				<view class="card_checkbox">
					<u-checkbox-group @change="checkboxChange">
						<u-checkbox name="asgas" shape="circle"></u-checkbox>
					</u-checkbox-group>
					<span class="message" :style="{display:hintShow ? 'block' : 'none'}">
						请先阅读并同意协议
					</span>
				</view>
				<view class="agreement_title">我已阅读并同意<span class="protocol_title">《用户协议》</span>、<span
						class="protocol_title">《隐私政策》</span>,
					并授权宝智达气瓶使用该宝智达气瓶账号信息（如昵称、头像、收货地址）进行统一管理
				</view>
			</view>
			<view class="card_btn">
				<u-button type="primary" :disabled="disabled" text="登录" @click="getLogin"></u-button>
			</view>
		</view>
		<!-- <view class="wx_card">
			<view class="card_btn_wx frame">
				<u-icon name="weixin-fill" size="26" color="#07c160"></u-icon>
			</view>
			<span class="title_login_wx">微信登录</span>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formList: {
					phone: '',
					verifyCode: '',
				},
				checked: false,
				hintShow: false,
				userInfo: {},
				tips: '',
				disabled1: false,
				rules: {
					phone: {
						type: 'string',
						required: true,
						message: '请填写手机号',
						trigger: ['blur']
					},
					verifyCode: {
						type: 'string',
						required: true,
						message: '请填写验证码',
						trigger: ['blur']
					},
				},
			}
		},
		computed: {
			disabled() {
				if (this.formList.phone == '' || this.formList.verifyCode == '') {
					return true;
				} else {
					return false;
				}
			}
		},
		// beforeCreate() {
		// 	var token = this.$cache.getToken()
		// 	if (token) {
		// 		uni.redirectTo({
		// 			url: '/pages/indexRouter'
		// 		})
		// 	}
		// },
		methods: {
			checkboxChange() {
				if (this.checked) {
					this.checked = false
				} else {
					this.checked = true
					this.hintShow = false
				}
			},
			getLogin() {
				if (!this.checked) {
					this.hintShow = true
				} else {
					uni.showLoading({
						title: '登录中...'
					});
					this.$api.post('/api/applet/login', {
						phone: this.formList.phone,
						verifyCode: this.formList.verifyCode
					}).then((res) => {
						console.log(res,4443)
						if (res.code == 200) {
							this.$cache.setToken(res.data.token)
							// this.getUserInfo()
							uni.redirectTo({
								url: '/pages/indexRouter'
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
						uni.hideLoading();
					})
				}
			},
			getUserInfo() {
				this.$api.get('/api/user/profile').then(res => {
					if (res.code == 200) {
						this.userInfo = res.data
						this.$cache.setCache('userInfo', this.userInfo)
						uni.redirectTo({
							url: '/pages/indexRouter'
						})
					} else {
						uni.showToast({
							title: '当前用户不存在',
							icon: 'none'
						});
					}
				})
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					if (this.formList.phone) {
						uni.showLoading({
							title: '正在获取验证码'
						})
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						this.$api.get('/api/verify-code', {
							phone: this.formList.phone,
						}).then(res => {
							if (res.code == 200) {
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
							}
							uni.hideLoading();
						})
					} else {
						uni.$u.toast('请先输入手机号');
					}
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			// 密码登录
			posswordLogin() {
				uni.redirectTo({
					url: '/pages/login'
				})
			},
			// 注册
			goRegister() {
				uni.navigateTo({
					url: '/pages/register'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.welcome_title {
		font-size: 46rpx;
		font-weight: 600;
		margin-bottom: 60rpx;
		margin-top: 10rpx;
	}

	.card_head_logo {
		margin-top: 50rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.card_login {
		padding: 20rpx 40rpx;
	}

	.mine_image {
		width: 100rpx;
		height: 100rpx;
	}

	.login_input_box {
		margin-bottom: 30rpx;
	}

	.title_register {
		display: flex;
		justify-content: flex-end;
		font-size: 28rpx;
		color: #494949;
	}

	.card_hint {
		margin-top: 60rpx;
		display: flex;
		align-items: flex-start;
	}

	.agreement_title {
		font-size: 26rpx;
		color: #333;
	}

	.protocol_title {
		color: #1177ff;
	}

	.card_btn {
		margin-top: 20rpx;
	}

	.card_checkbox {
		margin-top: 5rpx;
		position: relative;
		cursor: pointer;
	}

	.message {
		display: none;
		position: absolute;
		padding: 5px 8px;
		border: 1px solid #494949;
		border-radius: 9px;
		top: -36px;
		font-size: 12px;
		width: 110px;
		background: #494949;
		color: #fff;
		left: -6px;
	}

	.message::after {
		content: '';
		position: absolute;
		left: 8px;
		top: 100%;
		border: 8px solid transparent;
		border-top: 6px solid #494949;
		margin-left: -2px;
	}

	.btn_login {
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
	}
</style>