<template>
	<view>
		<!-- 头像区域 -->
		<view class="avatar-region">
			<view class="flex-items">
				<view class="font32 mar-l-20">
					头像
				</view>
				<view class="mar-l-auto mar-r-20">
					<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="1">
						<view class="flex-items">
							<image class="avatar" :src="user.avatar||'../../../static/images/avatar.png'"
								mode="aspectFill">
							</image>
							<uni-icons color="#d0d7df" type="right" size="30"></uni-icons>
						</view>
					</u-upload>
				</view>
			</view>
		</view>
		<!-- 基本信息区域 -->
		<view class="info-region">
			<view class="font-gray mar-l-20 mar-b-30">
				基本信息
			</view>
			<view class="flex-items mar-b-50">
				<view class="font32 mar-l-20">
					昵称
				</view>
				<view class="mar-l-auto mar-r-20" @click="changeAccount">
					<view class="flex-items">
						<view class="font32 font-gray">
							{{user.account}}
						</view>
						<view>
							<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
						</view>
					</view>
				</view>
			</view>
			<view class="flex-items mar-b-50" @click="changeBirthday">
				<view class="font32 mar-l-20 ">
					生日
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">
						<view class="font32 font-gray">
							{{user.birthday}}
						</view>
						<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
					</view>
				</view>
			</view>
			<view class="flex-items mar-b-50" @click="changeSex">
				<view class="font32 mar-l-20 ">
					性别
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">
						<view class="font32 font-gray" v-if="user.sex==0">
							女
						</view>
						<view class="font32  font-gray" v-if="user.sex==1">
							男
						</view>
						<view class="font32 font-gray " v-if="user.sex==2">
							其他
						</view>
						<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
					</view>
				</view>
			</view>
			<view class="flex-items mar-b-50">
				<view class="font32 mar-l-20 ">
					上级昵称
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">


						<view class="font32 font-gray ">
							{{user.parentAccount||'-'}}
						</view>
					</view>
				</view>
			</view>
			<view class="flex-items mar-b-50" @tap="goLevelPath">
				<view class="font32 mar-l-20 ">
					会员等级
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">


						<view class="font32 font-gray ">
							<text v-if="user.level == '0'">普通会员</text>
							<text v-else-if="user.level == '1'">S级会员</text>
							<text v-else-if="user.level == '2'">SS级会员</text>
							<text v-else-if="user.level == '3'">SSS级会员</text>
							<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
						</view>
					</view>
				</view>
			</view>
			<view class="flex-items mar-b-50" @tap="goAgentPath">
				<view class="font32 mar-l-20 ">
					代理等级
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">
						<view class="font32 font-gray ">
							<text v-if="user.agent == 2">荣耀代理</text>
							<text v-else-if="user.agent == 3">超凡代理</text>
							<text v-else>无</text>
							<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
						</view>
					</view>
				</view>
			</view>

			<!-- <view class="flex-items mar-b-20">
				<view class="font32 mar-l-20 ">
					国家/地区
				</view>
				<view class="mar-l-auto mar-r-20">
					<view class="flex-items">
						<view class="font32 font-gray">
							{{user.nation}}
						</view>
						<uni-icons color="#d0d7df" type="right" size="20"></uni-icons>
					</view>
				</view>
			</view> -->

		</view>
		<view class=" flex-center">
			<!-- <button class="loginBtn" style="background-color: #3C86F7;" type="primary"
				@click="handlerLogout">退出登录</button> -->
			<button @click="handlerLogout" class="logout-button">
				退出登录
			</button>
		</view>

	</view>
</template>

<script>
	import {
		memberDetail,
		memberUpdate
	} from '@/config/api';

	import {
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex';
	import {
		UPLOAD_URL
	} from '@/common/config.js'
	export default {
		data() {
			return {
				fileList: [],
				user: {},
				token: "",
			};
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		onLoad() {
			this.token = uni.getStorageSync('access_token')
		},
		onShow() {
			this.detail()
			console.log(this.user)
		},

		methods: {
			...mapActions(['logout']),
			goAgentPath() {
				uni.showModal({
					title: '升级/续费代理',
					content: '购买代理套餐即可升级或续费代理,\n立即前往?',
					success: (res) => {
						if (res.confirm) {
							// 这里可以跳转到代理升级页面
							this.$route('/packageShop/pages/search/product-list?serachKey=' + "代理")
						}
					}
				});
			},
			goLevelPath() {
				this.$route('/packageUser/pages/level/index')
			},
			handlerLogout() {
				this.logout()
				uni.$u.toast("退出登录")
				uni.navigateBack({
					delta: 1 // delta 表示返回的页面数，1 表示返回上一页
				});
			},
			changeSex() {
				this.$route('/packageUser/pages/user-info/sex-change?id=' + this.user.sex + '&id=' + this.user.id)
			},

			changeAccount() {
				this.$route('/packageUser/pages/user-info/account-change?id=' + this.user.account + '&id=' + this.user.id)
			},
			changeBirthday() {
				this.$route('/packageUser/pages/user-info/birthday-change?account=' + this.user.birthdat + '&id=' + this
					.user.id)
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				console.log(lists, "list")
				let fileListLen = this.fileList.length
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					this.user.avatar = result.data.link
					let item = this.fileList[fileListLen]
				}
				memberUpdate(this.user)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: UPLOAD_URL,
						filePath: url,
						header: {
							"Blade-Auth": this.token
						},
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(JSON.parse(res.data))
						}
					});
				})
			},
			async detail() {
				const data = await memberDetail()
				if (data.code == 200) {
					this.user = data.data
					uni.setStorageSync('user', this.user)
				}
			},
			goPage() {
				// this.$Router.push({ path: '/pages/user/set-avatar' });
			}
		},

	};
</script>

<style>
	.arrow {
		width: 32rpx;
		height: 32rpx;
		margin-top: 13rpx;
	}

	.h28 {
		height: 56rpx;
	}

	.avatar-region {
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
		border-radius: 40rpx;
		width: 700rpx;
		height: auto;
		padding-bottom: 30rpx;
		padding-top: 30rpx;
		background-color: white;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		margin-left: 25rpx;
	}

	.info-region {
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
		border-radius: 40rpx;
		width: 700rpx;
		height: auto;
		padding-bottom: 30rpx;
		padding-top: 30rpx;
		background-color: white;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		margin-left: 25rpx;
	}

	.avatar-container {
		margin-left: 50rpx;
		margin-right: 50rpx;
	}

	.avatar {
		width: 130rpx;
		height: 130rpx;
		border-radius: 50%;
		overflow: hidden;
	}

	.logout-button {
		width: 686rpx;
		height: 88rpx;
		background: #FF4748;
		border-radius: 44rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		margin-top: 100rpx;
	}
</style>