<template>
	<view class="container">
		<view class="content">
			<image src="/static/login-logo.png" class="login-logo"></image>
			<view class="first-title">登录</view>
			<form @submit="login" class="login-form">
				<view class="uni-form-item uni-column">
					<view class="title">用户名</view>
					<view>
						<input class="uni-input" name="username" placeholder="请输入账号" />
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">密码</view>
					<view>
						<input class="uni-input" name="password" password type="text" placeholder="请输入密码" />
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title"></view>
					<view>
						<label>
							<switch name="remember" :checked="true" />下次自动登录
						</label>
					</view>
				</view>
				<view class="uni-btn-v " style="margin-top: 36px;">
					<button :loading="loading" form-type="submit" class="login-bth">立即登录</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import helper from '../../common/helper'
	export default {
		data() {
			return {
				loading: false
			}
		},
		methods: {
			login(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				var formdata = e.detail.value
				// uni.showModal({
				// 	content: '表单数据内容：' + JSON.stringify(formdata),
				// 	showCancel: false
				// });
				console.log(formdata)
				this.loading = true;
				uni.request({
					method: 'POST',
					url: `${helper.serverUrl}/user/login`,
					data: {
						...formdata
					},
					success(resp) {
						console.log(resp)
						if (resp?.data?.code !== 200) {
							uni.showModal({
								content: resp.data.msg || '登录失败',
								title: '登录失败'
							})
						} else {
							helper.tempToken = resp.data.data.token;
							uni.showToast({
								icon: 'success',
								title: '登录成功'
							});
							if (formdata.remember) {
								uni.setStorage({
									key: 'token',
									data: resp.data.data.token,
									success() {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								})
							} else {
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}
						}
					},
					fail(err) {
						console.log(resp)
					},
					complete() {
						this.loading = false
					}
				})
			}
		}
	}
</script>

<style scoped>
	.login-bth {
		background: linear-gradient(-90deg, #01C4B4, #01DC7F);
		border-radius: 36px;
		color: #fff;
	}

	.login-form {
		width: 80%;
		height: 100%;
		margin-bottom: 70px;
	}

	.uni-form-item .title {
		padding: 20rpx 0;
		color: #526967;
	}

	.first-title {
		font-size: 28px;
	}

	.login-logo {
		position: absolute;
		top: -50px;
		left: calc(50% - 50px);
		width: 100px;
		height: 100px;
	}

	.content {
		position: relative;
		/* height: 70vh; */
		flex: 1;
		margin: 0 24px;
		background-color: #ffffffa3;
		border-radius: 20px;
		display: flex;
		align-items: center;
		flex-direction: column;
		padding-top: 70px;
	}

	.container {
		background: url(/static/login-bg.png);
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>