<template>
	<view class="u-page content">
		<u-icon class="back-btn" name="arrow-left" color="black" size="20" @click="navBack"></u-icon>
		<view class="wrapper">
			<image style='width:38px;height:38px;margin-bottom:10px;' src='@/static/logo.png'></image>
			<h2 class="title">欢迎使用网络货运</h2>
			<u--form labelPosition="left" :model="dataDetails" ref="loginForm" labelWidth="80">
				<u-form-item label="电话" prop="phone" borderBottom>
					<u--input v-model="dataDetails.phone" border="none" placeholder="请输入手机号码"></u--input>
				</u-form-item>

				<u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
					<u--input v-model="dataDetails.code" border="none" placeholder="请填写验证码"></u--input>
					<u-button slot="right" @tap="getCode" :text="tips" class="code-style" size="mini"
						:disabled="disabled1">
					</u-button>
				</u-form-item>
				<u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true"
					@end="disabled1 = false">
				</u-code>
			</u--form>
			<u-button  @tap="login"class="code-style submit">登录</u-button>
			<view @click='gonumber' class="use-password" style='text-align:center;color:#6A6A6A;'>使用账号密码登录</view>
			<!-- <u-button type="primary" text="登录" customStyle="margin-top: 50px" @click="login()"></u-button>
			<u-button type="primary" @click='goregister'>手机号一键注册</u-button> -->
			<!-- <u-loading-page :loading="isLoading" bg-color="#e8e8e8"></u-loading-page> -->
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var _this
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				isLoading: false,
				disabled1: false,
				tips: '',
				showCalendar: false,
				showBirthday: false,
				dataDetails: {
					phone: '',
					code: ''
				},
				rules: {
					phone: [
						{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: [ 'blur'],
						}
					],
					code: {
						type: 'string',
						required: true,
						len: 6,
						message: '请填写6位验证码',
						trigger: ['blur'],
					},
				}
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.loginForm.setRules(this.rules)
		},
		onLoad() {
			_this = this
		},
		methods: {
			gonumber(){
				uni.navigateTo({
					url:'/pages/public/lginOther'
				})
			},
			navBack() {
				uni.navigateBack();
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.dataDetails.phone.length != 11) {
					this.$refs.uToast.show({
						type: 'error',
						message: "手机号输入错误",
					})
				} else {
					if (this.$refs.uCode.canGetCode) {
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						_this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
								phone: this.dataDetails.phone
							}).then(res => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();

							})
							.catch(res => {
								uni.$u.toast(res.message);
							});
					} else {
						uni.$u.toast('倒计时结束后再发送');
					}
				}

			},

			login() {
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.loginForm.validate().then(res => {
					uni.$u.toast('校验通过')
					this.isLoading = true
					_this.$request.baseRequest('get', '/commonUser/loginVerifyCode', {
							phone: _this.dataDetails.phone,
							verifyCode: _this.dataDetails.code,
							 loginFlag :2
						}).then(res => {
							_this.$request.baseRequest('', '/driverInfo/firstAuthentication', {
									driverPhone: _this.dataDetails.phone,
								}).then(res1 => {debugger
									uni.setStorageSync('pcUserInfo', res.data)
									uni.setStorageSync('userInfo', res.data)
									uni.setStorageSync('firstAuthentication', res1.data)
									helper.getListByUserId()
									_this.$store.commit('login', res.data)
									// that.liangxinLogin()
									uni.switchTab({
										url: '/pages/goodSource/index'
									});
									_this.isLoading = false
									// 
									// 
								})
								.catch(res => {
									uni.$u.toast(res.message);
								});


							// that.$request.TokenRequest('post', '/commonUser/api/loginQuickly', {
							// 	mobilePhone: that.model1.phone,
							// 	veriCode: that.model1.code
							// }).then(res1 => {
							// 	uni.setStorageSync('pcUserInfo', res1.data)
							// 	uni.setStorageSync('userInfo', res.data)
							// 	helper.getListByUserId()
							// 	that.$store.commit('login', res.data)
							// 	// that.liangxinLogin()
							// 	uni.switchTab({
							// 		url: '/pages/index/index'
							// 	});
							// 	this.isLoading = false
							// })

						})
						.catch(res => {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							})
						});
				}).catch(errors => {
					this.$refs.uToast.show({
						type: 'error',
						message: "校验失败",
					})
				})
			},
			reset() {
				const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
					'hotel', 'code', 'userInfo.birthday'
				]
				this.$refs.form1.resetFields()
				this.$refs.form1.clearValidate()
				setTimeout(() => {
					this.$refs.form1.clearValidate(validateList)
					// 或者使用 this.$refs.form1.clearValidate()
				}, 10)
			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			goregister() {
				uni.$u.route('/pages/public/register');
			},
		},
	}
</script>

<style lang="scss">
	.content {
		background: url(../../static/images/mine/bg@2x.png);
		background-size: 100% 100%;
		height: 100vh;
		padding-top: 85px;
		position: relative;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: red;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		padding-bottom: 40upx;
		height: 100%;
		padding: 0 30px;
	}

	.code-style {
		background: #2772FB;
		color: white;
	}
	.use-password{
		margin-top: 31rpx;
	}
	.submit{
		margin-top: 40rpx;
	}
</style>
