<template>
	<view>
		<view class="remain">
			<h1>欢迎回来</h1>
			<view class="wel">请登录您的账号</view>
		</view>
		<view class="form">
			<u-form :model="form" ref="uForm" label-width="100px" label-position="top">
				<u-form-item label="手机号" prop="phone">
					<view class="form-input">
						<u-input v-model="form.phone" type="number" border="surround" maxlength='11'
							placeholder='请输入手机号码' :clearable='false' />
					</view>
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<view class="form-input">
						<u-input v-model="form.password" type="password" placeholder='请输入密码' :clearable='false' />
					</view>
				</u-form-item>
			</u-form>
			<view style="font-size: 13px;">忘记密码？</view>
			<view class="btn">
				<view style="margin-bottom: 10px;">
					<u-radio-group v-model="value" :wrap="true">
						<u-radio :label="radioLabel"></u-radio>
					</u-radio-group>
				</view>
				<view>
					<u-button type="error" shape="circle" text="登录" @click="submit()"></u-button>
				</view>
				<view class="tip" @click="toRegister()">还没有账号？马上注册</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sliderVerifyFLag: false, //滑块验证
				form: {
					phone: '',
					password: ''
				},
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: 'blur,change'
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur,change'
					}]
				},
				radioLabel: "登录同意《用户协议》和《隐私政策》",
				value: '',
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			loginFn() {
				if (!this.form.key) {
					this.$common.toast('请输入登录名');
					return;
				}
				if (!this.form.password) {
					this.$common.toast('请输入密码');
					return;
				}
				let openid = uni.getStorageSync('openid');
				if (!openid) {
					openid = uni.getStorageSync('login_openid');
				}
				this.form.openid = openid
				this.$api.doLogin(this.form).then(resp => {
					let data = resp.data;
				})
			},
			submit() {
				
				this.$refs.uForm.validate().then(res => {
					this.$api.doLogin(this.form).then(resp => {
						console.log(resp)
						let data = resp.data;
						if(resp.code==200){
							let data = resp.data;
							uni.$u.toast('登录成功，欢迎你'+data.appUser.name)
							localStorage.setItem('token',data.tokenInfo.tokenValue)
							uni.navigateTo({
								url: '/pages/login/chooseRole'
							});
						}
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			radioChange() {
				console.log("1111")
			},
			toRegister() {
				uni.navigateTo({
					url: '/pages/login/register'
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
		padding: 30px;
	}

	.remain {
		margin: 40px 50px 0;
		text-align: center;
	}

	.wel {
		margin-top: 10px;
	}

	.form {
		// margin: 0 30px;
		height: 200px;
		// display: flex;
		// align-items: center;
		// justify-content: center;
	}

	.form-input {
		width: 100%;
		background-color: #E8E8E8;
		border-radius: 5px;
		margin-top: 5px;
	}

	.btn {
		margin: 25px 10px;
	}

	.tip {
		margin-top: 30px;
		text-align: center;
		font-size: 14px;
	}
</style>