<template>
	<view>
		<u-navbar title="个人资料"></u-navbar>
		<view class="data">
			<view class="data-img">
				<view class="data-icon">
					<u-icon name="camera-fill" custom-prefix="custom-icon" color="#fff" size="32"></u-icon>
				</view>
				<u-avatar :src="form.headImg+'?x-oss-process=image/resize,m_fill,w_256,h_256'" size="216" @click="uploadPhoto"></u-avatar>
			</view>
			<view class="data-input">
				<u-form :model="form" ref="uForm" >
					<u-form-item label-position="top" label="昵称"><u-input v-model="form.nickName" /></u-form-item>
				</u-form>
			</view>
			<u-button class="login-btn" type="success" shape="circle" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import * as userApi from '@/apis/user.js'
	import * as loginApi from '@/apis/login.js'
	
	export default {
		data() {
			return {
				userId: '',
				form: {
					nickName: '',
					headImg: '',
				},
			}
		},
		onReady() {	
			this.getUserInfo();
		},
		methods: {
			getUserInfo() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})		
				loginApi.findByOpenId({
					openId: this.carhelp.getOpenId(),
				}).then((res) => {
					uni.hideLoading();
					
					this.form.headImg = res.data.regUser.headImg;
					this.form.nickName = res.data.regUser.nickName;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			uploadPhoto() {
				// 上传图片
				let _self = this;
				uni.chooseImage({
					count: 1, //默认9
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						let imgFile = res.tempFilePaths;

						var token = this.carhelp.getToken()
						for (let i = 0; i < imgFile.length; i++) {
			
							wx.uploadFile({
								url: process.car.BASE_URL + "uploadPicture",	
								name: 'photoFile',
								header: {		 
									'Authorization': token,	 
									'accept': 'application/json',
									//#ifdef MP-WEIXIN
									"Content-Type": "multipart/form-data", //记得设置
									//#endif
			
								},
								filePath: imgFile[0],
								success: function(result) {
									let imgUrls = JSON.parse(result.data)
									_self.form.headImg = imgUrls.data;
								}
							})
						}
					},
				});
			},
			submit() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})		
				userApi.updatePersonInformation(this.form).then((res) => {
					uni.hideLoading();
					
					uni.redirectTo({
						url: '/pages/user/index'
					})
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			}
		}
	}
</script>
<style>
	page{
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.data-icon{
		height: 28px;
		width: 28px;
		background-color: #00B962;
		border-radius: 14px;
		border: 2px solid #fff;
		text-align: center;
		line-height: 24px;
		position: absolute;
		z-index: 999;
		right: 0;
		bottom:0px
	}
	.data-img{
		margin: 30px auto;
		height: 108px;
		width: 108px;
		position: relative;
	}
	.data-input{
		margin: 0 40px;
	}
	.login-btn {
		margin: 28px ;
		background-color:#00B962!important;
		border-color: #00B962!important;
		color:#fff!important;
	}
</style>
