<template>
	<!-- 登录注册 -->
	<view>
		<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="account" borderBottom ref="item1">
					<u--input v-model="formList.account" border="none" placeholder="请输入账号"></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="password" borderBottom ref="item1">
					<u--input v-model="formList.password" type="password" border="none" placeholder="请填写密码"></u--input>
				</u-form-item>
			</u--form>
			<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>
</template>

<script>
	export default {
		data() {
			return {
				formList: {
					account: '',
					password: '',
				},
				checked: false,
				hintShow: false,
				userInfo: {},
				rules: {
					account: {
						type: 'string',
						required: true,
						message: '请填写账号',
						trigger: ['blur']
					},
					password: {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur']
					},
				},
			}
		},
		computed: {
			disabled() {
				if (this.formList.account == '' || this.formList.password == '') {
					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/login', {
						type: 1,
						mobile: true,
						username: this.formList.account,
						password: this.formList.password
					}).then((res) => {
						if (res.code == 200) {
							this.$cache.setToken(res.token)
							this.getUserInfo()
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
						uni.hideLoading();
					})
				}
			},
			getUserInfo() {
				this.$api.get('/api/user/profile').then(res => {
					if (res.code == 200) {
						this.userInfo = res.data.user
						this.$cache.setCache('userInfo', this.userInfo)
						uni.redirectTo({
							url: '/pages/indexRouter'
						})
					} else {
						uni.showToast({
							title: '当前用户不存在',
							icon: 'none'
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.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;
	}

	.card_hint {
		margin-top: 100rpx;
		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;
	}
</style>