<template>
	<!-- 登录注册 -->
	<view>
		<u-navbar title="" leftIconSize="0" placeholder></u-navbar>
		<view class="card_login">
			<view class="card_head_logo">
				<image class="mine_image" src="../static/logo.png" mode=""></image>
				<view class="welcome_title">欢迎登录智配送App</view>
				<!-- <view class="welcome_title">欢迎登录美克医疗-智配送App</view> -->
			</view>
			<u--form labelPosition="left" :model="formList" :rules="rules" ref="uForm">
				<u-form-item label="账号" prop="username" labelWidth="50" borderBottom ref="item1">
					<u-input v-model="formList.username" border="none" placeholder="请填写账号"></u-input>
				</u-form-item>
				<u-form-item label="密码" prop="password" labelWidth="50" borderBottom ref="item1">
					<u-input v-show="isText === true" placeholder="请填写密码" border="none" v-model="formList.password"
						class="input_class_pwd" :password="true">
						<template slot="suffix" v-if="formList.password">
							<u-icon name="eye-off" @click="isText = false" size="20"></u-icon>
						</template>
					</u-input>
					<u-input v-show="isText === false" placeholder="请填写密码" border="none" v-model="formList.password"
						class="input_class_pwd" :password="false">
						<template slot="suffix" v-if="formList.password">
							<u-icon name="eye-fill" @click="isText = true" size="20"></u-icon>
						</template>
					</u-input>
				</u-form-item>
			</u--form>
			<!-- <view class="btn_login">
				<view class="title_register" @click="codeLogin">验证码登录</view>
				<view class="title_register" @click="goRegister">去注册</view>
			</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 class="wx_card">
			<view class="card_btn_wx frame">
				<u-icon name="weixin-fill" size="26" color="#07c160"></u-icon>
			</view>
			<span class="title_login_wx">微信登录</span>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formList: {
					username: '',
					password: '',
				},
				rules: {
					username: {
						type: 'string',
						required: true,
						message: '请填写账号',
						trigger: ['blur']
					},
					password: {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur']
					},
				},
				checked: false,
				hintShow: false,
				userInfo: {},
				isText: true,
			}
		},
		computed: {
			disabled() {
				if (this.formList.username == '' || this.formList.password == '') {
					return true;
				} else {
					return false;
				}
			},
		},
		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', {
						mobile: true,
						type: 1,
						username: this.formList.username,
						password: this.formList.password
					}).then((res) => {
						if (res.code == 200) {
							this.$cache.setToken(res.token)
							this.getUserInfo()

							// uni.redirectTo({
							// 	url: '/pages/homePage'
							// 	// url: '/pages/indexRouter'
							// })
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
						uni.hideLoading();
					}).catch(() => {
						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/homePage'
							// url: '/pages/indexRouter'
						})
					} else {
						uni.showToast({
							title: '当前用户不存在',
							icon: 'none'
						});
					}
				})
			},
			// 验证码登录
			codeLogin() {
				uni.redirectTo({
					url: '/pages/codeLogin'
				})
			},
			// 注册
			goRegister() {
				this.$refs.uForm.resetFields()
				uni.navigateTo({
					url: '/pages/register'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.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;
	}

	.title_register {
		display: flex;
		justify-content: flex-end;
		font-size: 28rpx;
		color: #494949;
	}

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

	.btn_login {
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
	}
</style>