<template>
	<view class="jpmain  ">
		
		<u-popup v-model="showMessage" mode="bottom" border-radius="30" >
			<view class="showMessage">
				<view class="title">用户昵称</view>
				<view class="body">
					<u-input :customStyle="customStyle"
					placeholder="请填写用户昵称"
					  v-model="nickName"></u-input>
				</view>
				<view class="botton">
					<u-button @click="showMessage=false"  style="width: 35%;"   shape="square" >取消</u-button>
					
					<u-button  @click="updateName" style="width: 60%;" type="primary"  shape="square" >确认</u-button>
				</view>
			</view>
		</u-popup>
		<u-keyboard ref="uKeyboard"  @change="valChange" @backspace="backspace"
		:tips="carNumber?carNumber:'请输入车牌号'" @confirm="updateCarNumber()"
		mode="car" v-model="showCarNumber"  :abc="abc" ></u-keyboard>
		
		
		
		
		<componentLogin ref="refLogin" @findByOpenId="findByOpenId"></componentLogin>
		

		<view class="body">
			<view class="page">
			
				<view class="page-content">
					<view class="item">
						<view class="title">
							头像
						</view>
						<view class="goto "  @click="uploadPhoto" >
							<view class="photo">
								
								<img class="img" :src="userInfo.headImg" v-if="userInfo.headImg"  alt="">
							</view>
							
							
							<u-icon name="arrow-right"  style="margin-left: 8rpx;"  size="24" color="#BBBBBB"></u-icon>
						</view>
					</view>
				
				</view>
			</view>
			
			<view class="page">

				<view class="page-content">
					<view class="item">
						<view class="title">
							用户昵称

						</view>
						<view class="goto" @click="showMessage=true,nickName=userInfo.nickName">
							{{userInfo.nickName?userInfo.nickName:'未设置'}}
							<u-icon name="arrow-right" style="margin-left: 8rpx;" size="24" color="#BBBBBB"></u-icon>
						</view>
					</view>
					<view class="item">
						<view class="title">
							车牌号
					
						</view>
						<view class="goto" @click="carNumberBtn">
							{{userInfo.carNumber?userInfo.carNumber:'未设置'}}
							<u-icon name="arrow-right" style="margin-left: 8rpx;" size="24" color="#BBBBBB"></u-icon>
						</view>
					</view>
					<view class="item">
						<view class="title">
							手机号码
						</view>
						<view class="goto">
							{{userInfo.phone}}
						</view>
					</view>
				</view>
			</view>
			
			<view class="page" v-if="false">
			
				<view class="button">
					退出账号
				
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/index.js'
	
	import componentLogin from '@/components/componentLogin.vue';
	export default {
		components: {
			componentLogin,
		},
		data() {
			return {
				userInfo:{},
				showMessage:false,
				showCarNumber:false,
				carNumber:"",
				nickName:"",
				abc:false,
				
				customStyle: {
					"border-radius": "50px",
					"background-color": 'rgba(242, 244, 246, 1)',
					padding: "5px 20px",
					margin: "5px 0 ",
					
				},
				
			}
		},
		onLoad() {
			this.userInfo = this.jphelp.getPersonInfo()	
		},
		onReady() {
			this.$refs.refLogin.findByOpenId()
			
		},
		methods: {
			carNumberBtn(){
				 
					this.showCarNumber=1;
					this.carNumber=this.userInfo.carNumber;
					if(!this.carNumber){
						this.carNumber=""
					}
					this.valInit(1)
			},
		
			valInit(bl){
				if(bl){
					this.abc=this.carNumber.length>0
				}else{
					if(this.carNumber.length==1&&this.abc==false){
						this.abc=true
					}
					if(this.carNumber.length==0&&this.abc==true){
						this.abc=false
					}
				}
				
			},
			valChange(val){
				
				if(this.carNumber.length>=8){
					return
				}
				this.carNumber+=val
				this.valInit()
				this.$forceUpdate()
			},
			backspace(){
				//
				var value=this.carNumber
				if(value.length){
					value = value.substr(0, value.length - 1);
					
				}
				this.carNumber=value
				this.valInit()
				
			
			},
			updateCarNumber(){
				this.userInfo.carNumber=this.carNumber
				this.showCarNumber=false;
				this.updatePersonInformation()
			},
			
			updateName(){
				if(this.nickName){
					this.userInfo.nickName=this.nickName
					this.showMessage=false;
					this.updatePersonInformation()
				}else{
					this.showMessage=true;
					uni.showToast({
						title: "用户昵称不能为空",
						icon: "none"
					})	
				}
				
			},
			updatePersonInformation(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var obj={
					nickName:this.userInfo.nickName,
					headImg:this.userInfo.headImg,
					carNumber:this.userInfo.carNumber,
				}
				
				API.updatePersonInformation(obj).then((res) => {
					
					uni.hideLoading();
					this.$refs.refLogin.findByOpenId()
				}).catch(error => {
				
					uni.hideLoading();
					uni.showToast({
						title: error,
						icon: "none"
					})		
				})
			},
			findByOpenId(res){
				this.userInfo = this.jphelp.getPersonInfo()	
				
			},
			saveRecordConfirmMethod(fileData) {
			
				var token = this.jphelp.getToken()
			
				uni.uploadFile({
					url: process.jphelp.BASE_URL + "uploadPicture", //仅为示例，非真实的接口地址
					filePath:fileData,
					header: {
						'Authorization': token,
						//'Content-Type': 'multipart/form-data',
						'X-Requested-With': 'XMLHttpRequest',
						//	'content-type': 'multipart/form-data'
					},
					name: 'photoFile',
					formData: {
						subFolder: "headimg"
					},
					success: (uploadFileRes) => {
						
						
						var obj = JSON.parse(uploadFileRes.data)
						//.log(obj);
						//this.src = obj.data;
						this.userInfo.headImg=obj.data;
						this.updatePersonInformation();
						// uni.hideLoading();
					}
				});
			},
			uploadPhoto() {
				let _self = this;
			
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
				};
			
				// 上传图片
				uni.chooseImage({
					count: 1,
					crop,
					success: async (res) => {
						//(res);
						let tempFile = res.tempFiles[0];
			
						var filePath = res.tempFilePaths[0]
			
						let fileData = await new Promise((callback) => {
							uni.navigateTo({
								url: './cropImage?path=' + filePath +
									`&options=${JSON.stringify(crop)}`,
								animationType: "fade-in",
								events: {
									success: url => {
										callback(url)
									}
								}
							});
						})
			
						this.saveRecordConfirmMethod(fileData);
			
			
			
			
					}
				});
			},
		}
	}
</script>
<style>
	page {
		background-color: rgba(242, 244, 246, 1);
	}
</style>
<style scoped lang="scss">
	/* styles.css */
	.showMessage{
		
		.carNumber {
			background-color: #F2F4F6;
			padding: 5px 20px;
			margin: 5px 0 15px 0;
			height: 90rpx;
			display: flex;
			align-items: center;
			border-radius: 50px;
			
		}
		.carNumber1{
			color: #bcbcbc;
		}
		
		padding: 24rpx 56rpx;
		.title{
			color: rgba(16,16,16,1);
			font-size: 36rpx;
			margin-top: 32rpx;
			
			font-weight: bold;
		}
		.body{
			color: rgba(16,16,16,1);
			font-size: 32rpx;
			padding-bottom: 66rpx;
		}
		.botton{
			    display: flex;
		}
	}

	.body {
		padding: 32rpx;
	}



	

	.page {

		border-radius: 16rpx;
		background-color: rgba(255, 255, 255, 1);
		color: rgba(16, 16, 16, 1);

		padding:0 32rpx;
		margin-bottom: 32rpx;
		font-size: 32rpx;
		color: rgb(16, 16, 16);

		

		.page-content {
			.item:not(:last-child) {
			  border-bottom:1px solid rgba(232,232,232,1);
			}
			.item {
				padding: 32rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				.title {
					display: flex;
					justify-content: space-between;
					display: flex;
					align-items: center;
					color: rgba(51,51,51,1);
					
					
				}
				.goto{
					display: flex;
					align-items: center;
					color: rgba(119,119,119,1);
					
				}
				.photo {
					border-radius: 50px;
					background-color: rgba(229, 229, 229, 1);
					height: 80rpx;
					width: 80rpx;
					overflow: hidden;
					  
					.img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}


		.button{
			text-align: center;
			color: red;
			    padding: 32rpx 0;
		}
	}
</style>