<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>
			<view class="login_input_box">
				<u--input v-model="formList.account" fontSize="18px" placeholder="请输入账号" border="bottom"
					clearable></u--input>
			</view>
			<view class="login_input_box">
				<u--input v-model="formList.password" fontSize="18px" placeholder="请输入密码" password border="bottom"
					clearable></u--input>
			</view>
			<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: {},
			}
		},
		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;
	}

	.login_input_box {
		margin-bottom: 30rpx;
	}

	.card_hint {
		margin-top: 100rpx;
		display: flex;
		align-items: flex-start;
	}

	.agreement_title {
		font-size: 30rpx;
		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>