<template>
	<view class="content">
		<view class="user-v" @click="avatarClick">
			<image v-if="!user.avatar" class="user-icon" src="/static/user.png"></image>
			<image v-else class="user-icon" :src="user.avatar"></image>
		</view>
		<view class="text-area">
			<text class="title">{{user.username}}</text>
		</view>
		<view class="login-btn">
			<button v-if="!token" type="primary" plain="true" size="mini" @click="avatarClick">登录</button>
			<button v-else type="default" plain="true" size="mini" @click="dialogToggle">注销</button>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="取消" confirmText="注销" title="注销" content="确定是要注销吗?"
				@confirm="logout"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		ref
	} from 'vue';
	export default {
		data() {
			const token = ref(null)
			const user = ref(null)
			return {
				token,
				user,
			}
		},
		onLoad() {

		},
		onShow() {
			this.token = uni.getStorageSync('token');
			this.user = uni.getStorageSync('user');
		},
		methods: {
			avatarClick() {
				if (this.token) {

				} else {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				}

			},
			logout() {
				uni.removeStorageSync('token')
				uni.removeStorageSync('user')
				this.token = uni.getStorageSync('token');
				this.user = uni.getStorageSync('user');
			},
			dialogToggle() {
				this.$refs.alertDialog.open()
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.user-v {
		background: rgba(202, 202, 202, 0.5);
		border-radius: 50%;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 36rpx;
		height: 140rpx;
		width: 140rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		overflow: hidden;
	}

	.user-icon {
		height: 100rpx;
		width: 100rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.login-btn {
		margin-top: 20rpx;
	}
</style>