<template>
	<view class="page">
		<view class="content">
			<view class="header">
				<text>{{i18n.headerImg}}</text>
				<image @click="upimage" class="header-img"
					:src="filePath" mode=""></image>
			</view>
			<view class="bottom-text" @click="toName">
				<text>{{i18n.nickname}}</text>
				<AllRight :name="nickname"></AllRight>
			</view>
		</view>
		<view class="bottom" style="">
			<view class="red" @click="update">{{i18n.preserve}}</view>
		</view>
	</view>
</template>

<script>
	import AllRight from '../mineComponent/allRight/allRight.vue'
	export default {
		components: {
			AllRight
		},
		data() {
			return {
				nickname: '张三',
				filePath: '',
				language:'cn'
			};
		},
		computed: {
			i18n() {
				return this.$t('index')
			}
		},
		mounted() {
			if(uni.getStorageSync('user_info')){
				this.filePath = uni.getStorageSync('user_info').avatar
				this.nickname = uni.getStorageSync('user_info').nickname
			}
			if(uni.getStorageSync('language')){
				this.language = uni.getStorageSync('language')
				if(this.language == 'zh-CN'){
					this.language = 'cn'
				}else if(this.language == 'en-US'){
					this.language = 'en'
				}else if(this.language == 'es-ES'){
					this.language = 'es'
				}else if(this.language == 'it-IT'){
					this.language = 'ita'
				}
			}
			uni.setNavigationBarTitle({
				title: this.i18n.information
			})
		},
		methods: {
			update(){
				uni.$u.http.post('/api/member/update', {
					lang: this.language.slice(0,2),
					avatar: this.filePath,
					nickname: this.nickname
				}).then((res) => {
					uni.setStorageSync('user_info',res)
					this.$u.toast('保存成功')
					setTimeout(()=>{
						const eventChannel = this.getOpenerEventChannel();
						eventChannel.emit("loadData");
						uni.navigateBack()
					},500)
				}).catch(() => {
				
				})
			},
			upimage() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'https://cbec.hdlkeji.com/api/upload/images', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							// formData: {
							// 	'user': 'test'
							// },
							success: (uploadFileRes) => {
								this.filePath = JSON.parse(uploadFileRes.data).data.filePath
							}
						});
					}
				});
			},
			toName() {
				var that = this
				uni.navigateTo({
					url: '/pageC/editName/editName?name=' + this.nickname,
					events:{
						getname(res){
							console.log(res);
							that.nickname = res
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bottom {
		width: 750rpx;
		height: 166rpx;
		background: #FFFFFF;
		padding: 16rpx 32rpx 64rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
	}

	.red {
		width: 686rpx;
		height: 88rpx;
		background: #F83224;
		box-shadow: 0rpx 16rpx 40rpx -12rpx rgba(255, 21, 21, 0.5);
		border-radius: 44rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		font-style: normal;
	}

	.page {
		padding: 20rpx 24rpx;

		.content {
			background-color: #fff;
			border-radius: 16rpx;
			padding: 0 20rpx;

			.header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 176rpx;
				border-bottom: 2rpx solid rgba(151, 151, 151, .2);

				.header-img {
					height: 116rpx;
					width: 116rpx;
					border-radius: 50%;
				}
			}

			.bottom-text {
				display: flex;
				height: 116rpx;
				justify-content: space-between;
				align-items: center;
			}
		}
	}
</style>