<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" @input='passwordInput' border="none" placeholder="请输入手机号码">
					</u--input>
				</u-form-item>
				<u-form-item label="密码" prop="password" borderBottom>
					<u--input v-model="dataDetails.password" @input='passwordInput' border="none"
						placeholder="请输入密码,6-16位字符"></u--input>
				</u-form-item>
			</u--form>
			<u-button @tap="login" class="code-style submit" :disabled="isdisabled">登录</u-button>
			<u-button @click='goregister' class="code-style submit">手机号一键注册</u-button>
			<view class='flex' style='color:#6A6A6A;margin-top:10px;'>
				<view style='flex:1;text-align:center;border-right:1px solid #E8E9ED;' class="Regular"
					@click="forgetpass()">忘记密码</view>
				<view @click='gocode' style='flex:1;text-align:center;' class="Regular">验证码登录</view>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var _this
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				isdisabled: true,
				dataDetails: {
					phone: '',
					password: '',
					loginFlag: 2
				},
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur'],
						}
					],
					password: {
						type: 'string',
						required: true,
						min: 6,
						max: 16,
						message: '长度在6-16位字符之间',
						trigger: ['blur'],
					},
				}
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.loginForm.setRules(this.rules)
		},
		onLoad() {
			_this = this
		},
		methods: {
			passwordInput() {
				if (this.dataDetails.password && this.dataDetails.phone) {
					this.isdisabled = false
				} else {
					this.isdisabled = true
				}
			},
			forgetpass() {
				uni.navigateTo({
					url: '/pages/mine/settings/editPassword'
				})
			},
			goregister() {
				uni.navigateTo({
					url: '/pages/public/register'
				})
			},
			gocode() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			navBack() {
				uni.navigateBack();
			},
			codeChange(text) {
				this.tips = text;
			},

			login() {
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.loginForm.validate().then(res => {
					uni.showLoading({
						title: '登录中',
						mask: true
					})
					_this.$request.baseRequest('get', '/commonUser/login', _this.dataDetails).then(res => {
							if (res.data.code == '11002') {
								uni.$u.toast('验证码错误或过期，请重新获取')

								return
							} else if (res.data.code == '11004') {
								uni.$u.toast('该手机号已注册，可直接登录')

								return
							} else if (res.data.code == '11006') {
								uni.$u.toast('手机号或密码错误!')

								return
							}
							_this.$request.baseRequest('', '/driverInfo/firstAuthentication', {
									driverPhone: _this.dataDetails.phone,
								}).then(res1 => {
									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'
									});
									uni.hideLoading()
									// 
									// 
								})
								.catch(res => {
									uni.$u.toast(res.message);
								});
						})
						.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>
