<template>
	<view class="content">
		<u-gap height="30" bgColor="#F7F8FA"></u-gap>
		<view class="myTab">
			<u-cell-group :border="false">
				<u-cell title="头像" value="新版本" :isLink="true">
					<view class="userImg" slot="value">
						<u-upload :fileList="fileList" @afterRead="afterRead" name="" multiple :maxCount="1" width="36"
							height="36">
							<image :src="baseImg" mode="widthFix" style="width: 72rpx;height: 72rpx;"></image>
						</u-upload>
					</view>
				</u-cell>
				<u-cell title="昵称" :isLink="true">
					<view class="nickName" slot="value">
						<u--input placeholder="请输入内容" inputAlign="right" @blur="changeUserName" border="none"
							v-model="nickname"></u--input>
					</view>
				</u-cell>
				<u-cell title="手机号" :value="phone" :border="false" :isLink="true"></u-cell>
			</u-cell-group>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
				fileList: [],
				nickname: '',
				phone: '',
				baseImg: 'http://amy.yyinhong.cn/uploads/wximg/pc_wdhy.png'
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			//获取用户信息
			getUserInfo() {
				this.$request('/user-info', 'GET', {}).then(res => {
					console.log('请求成功');
					this.userInfo = res.data;
					this.phone = res.data.phone;
					this.nickname = res.data.nickname;
					this.baseImg = res.data.avatar;
				}).catch(err => {
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$common.BASE_URL + '/upload-file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file_obj',
						formData: {
							file_obj: url
						},
						success: (res) => {
							setTimeout(() => {
								this.updateInfoimg(JSON.parse(res.data).data)
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			//修改用户名
			changeUserName() {
				if (this.userInfo.nickname != this.nickname) {
					this.updateInfoimg(this.userInfo.avatar)
				}
			},
			updateInfoimg(e) {
				console.log(e)
				this.$request('/edit-user', 'POST', {
					name: this.nickname,
					avatar: e
				}).then(res => {
					this.$refs.uToast.show({
						type: 'success',
						message: "修改个人信息成功",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
					})
					this.fileList = [];
					this.getUserInfo()
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		background: #F7F8FA;
	}

	.myTab {
		width: calc(100% - 120rpx);
		min-height: 104rpx;
		margin: 0 40rpx;
		padding: 20rpx;
		border-radius: 24rpx;
		background-color: #ffffff;
	}

	.nickName {
		font-size: 28rpx;
	}
</style>