<template>
	<view class="content">
		<image class="logo" src="/static/login/login.png"></image>
		<view class="text-area">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
				<el-form-item prop="phone">
					<el-input type="text" placeholder="请输入手机号" required="required" v-model="ruleForm.phone"
						prefix-icon="el-icon-user-solid"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="ruleForm.password"
						@keyup.enter.native="toSubmitForm('ruleForm')"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="login">登录</el-button>
				</el-form-item>
			</el-form>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'login',
		components: {},
		data() {
			var loginValidatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'))
				} else {
					callback()
				}
			}
			var loginValidateUser = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('手机号不能为空'))
				} else {
					const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
					// eslint-disable-next-line no-useless-escape
					const reg2 = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
					if ((reg.test(value) || reg2.test(value))) {
						callback()
					} else {
						callback(new Error('请输入正确的手机号'))
					}
				}
			}
			return {
				ruleForm: {
					phone: '',
					password: ''
				},
				rules: {
					phone: [{
						required: true,
						validator: loginValidateUser,
						trigger: 'blur'
					}],
					password: [{
						required: true,
						validator: loginValidatePass,
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			isRightPhone(value) {
				const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
				// eslint-disable-next-line no-useless-escape
				const reg2 = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
				return value !== '' && (reg.test(value) || reg2.test(value))
			},
			loginCheck() {
				return this.isRightPhone(this.ruleForm.phone) && this.ruleForm.password.length >= 6
			},
			login() {
				if (!this.loginCheck()) {
					return
				}
				axios.get('/login', {
					params: {
						phonenumber: this.ruleForm.phone,
						password: this.ruleForm.password
					}
				}).then(response => {
					if (response.data.code === '400') {
						this.$message({
							message: '用户名或密码错误',
							type: 'error'
						})
					} else if (response.data.code === '200') {
						this.$globalVar.userId = response.data.userId
						this.$message({
							message: '登录成功',
							type: 'success'
						})
						console.log(this.$globalVar.userId)
						this.$router.push('/homepage')
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 480rpx;
		width: 500rpx;
		margin-top: 100rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}
</style>