<template>
	<view class="page">
		<view class="action">
			<view>
				<image src="../../../static/img/userInfo/scan.png"></image>
			</view>
			<view>
				<image src="../../../static/img/userInfo/setting.png"></image>
			</view>
		</view>
		<view class="user" @click="onUserInfoClick()">
			<image class="avatar" :src="user.avatar"></image>
			<view class="name">
				<text>{{ user.username }}</text>
				<text>{{ user.bio }}</text>
			</view>
			<image class="right" src="../../../static/img/userInfo/right.png"></image>
		</view>
		<view class="card" v-for="(v, k) in menus" :key="k">
			<view class="menu">
				<view class="item" v-for="(v_, k_) in v" :key="k_" @click="menuClick(v_)">
					<image :src="v_.icon"></image>
					<view>{{v_.name}}</view>
					<image class="arrow" v-if="v_.arrow" src="../../../static/img/userInfo/right.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {}, // 直接在data中定义user
				menus: [
					[{
							index: 0,
							name: '上传管理',
							icon: '../../../static/img/userInfo/self.png',
							arrow: true
						},
						{
							index: 1,
							name: '我的账户',
							icon: '../../../static/img/userInfo/self.png',
							arrow: true
						},
						{
							index: 2,
							name: '我的设置',
							icon: '../../../static/img/userInfo/self.png',
							arrow: false
						},
					],
					[{
							index: 3,
							name: '联系客服',
							icon: '../../../static/img/userInfo/self.png',
							arrow: true
						},
						{
							index: 4,
							name: '退出登录',
							icon: '../../../static/img/userInfo/self.png',
							arrow: false
						},
					]
				]
			}
		},
		onLoad() {
			// 尝试从本地缓存中读取userInfo
			this.user = uni.getStorageSync('userInfo');
		},
		onShow() {
			uni.$on("refreshData", res=>{
				this.user = uni.getStorageSync('userInfo');
				// console.log("刷新数据");
				uni.$off('refreshData');
			})
		},
		methods: {
			menuClick(e) {
				switch (e.index) {
					case 0:
						uni.navigateTo({
							url: './uploadMgr'
						});
						break;
					case 4:
						uni.reLaunch({
							url: '../login/login'
						});
						break;
					default:
						console.log('Default case triggered for index: ', e.index);
						break;
				}
				console.log(e);
			},
			onUserInfoClick() {
				uni.navigateTo({
					url: './userInfo'
				});
			}
		}
	}
</script>

<style lang="scss">
	.page {
		background: #f5f5f5;
		min-height: 80vh;

		.action {
			display: flex;
			justify-content: flex-end;
			padding: 10upx;
			background: #3f5b71;

			view {
				padding: 10rpx;
				margin: 0 10rpx;
				border-radius: 8rpx;
				width: 45rpx;
				height: 45rpx;

				&:active {
					background: #ccc;
				}

				image {
					width: 45rpx;
					height: 45rpx;
				}
			}
		}

		.user {
			display: flex;
			align-items: center;
			padding: 25rpx;
			background: #3f5b71;

			&:active {
				background: #f0f0f0;
			}

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 12rpx;
				border: 3px solid #abb0b6;
			}

			.right {
				width: 30rpx;
				height: 30rpx;
			}

			.name {
				flex-grow: 1;
				display: flex;
				flex-direction: column;
				padding-left: 25rpx;
				gap: 15rpx;

				text {
					color: #fff;

					&:nth-child(1) {
						font-size: 24px;
						font-weight: bold;
					}

					&:nth-child(2) {
						font-size: 12px;
						color: #f0f0f0;
					}
				}
			}
		}

		.card {
			background: #fff;
			box-sizing: border-box;
			width: 700rpx;
			margin: auto;
			padding: 15rpx;
			margin-top: 20rpx;
			border-radius: 12rpx;

			.menu {

				.item {
					display: flex;
					height: 100rpx;
					align-items: center;
					padding: 0 20rpx;
					box-sizing: border-box;
					width: 700upx;

					&:active {
						background: #f0f0f0;
					}

					image {
						height: 45rpx;
						width: 45rpx;
					}

					view {
						font-size: 14px;
						color: #444;
						padding-left: 15upx;
						box-sizing: border-box;
						width: calc(100% - 100rpx);
					}

					.arrow {
						width: 26upx;
						height: 26upx;
					}
				}
			}
		}
	}
</style>
