<template>
	<view class="main" style="padding-bottom: 0;">
		<!-- 头部 -->
		<status-nav navBarTitle="个人信息"></status-nav>
		<!-- 内容区 -->
		<view class="content">
			<view class="section-other information">
				<view class="item">
					<text>头像</text>
					<view class="txt" @tap="editEv('avatar')">
						<view class="avatar">
							<image :src="userInfo.headimgurl!==''?baseHttps+userInfo.headimgurl:defaultAvatar" mode="aspectFit"></image>
						</view>
						<image class="edit-btn" src="/static/icon/icon-arrow-right.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item" @tap="editEv('nick_name')">
					<text>昵称</text>
					<view class="txt">
						<input type="text" v-model="userInfo.nickname" disabled="true">
						<image class="edit-btn" src="/static/icon/icon-arrow-right.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item" @tap="editEv('mobile')">
					<text>手机号</text>
					<view class="txt">
						<input type="number" v-model="userInfo.mobile" disabled>
					</view>
				</view>
			</view>
		</view>
		<!-- 修改弹窗 -->
		<view class="pull-pop-bg pull-pop-center" v-if="ifShow">
			<view class="pull-pop information-pop">
				<view class="title">{{editTitle}}</view>
				<input :type="editType" v-model="editMsg">
				<view class="btns">
					<view class="btn" @tap="submitEv('nick_name')">确认</view>
				</view>
				<view class="close-btn" @tap="ifShow=false">
					<image src="/static/icon/icon-close.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		postEditInfo,
		postUserInfo,
		postUpload
	} from "@/api/index";
	export default {
		data() {
			return {
				statusHeight:uni.getSystemInfoSync().statusBarHeight, //状态栏高度
				baseHttps:`${getApp().globalData.hostapi}`, //接口链接
				defaultAvatar:'/static/public/logo-avatar.png' ,//默认头像
				userInfo:uni.getStorageSync("userInfo"), //用户信息
				editTitle:'标题', //修改标题
				editType:'text', //修改类型
				editMsg:'', //修改内容
				ifShow:false, //是否显示弹窗
			};
		},
		onLoad(op) {
		},
		methods: {
			// 修改信息
			editEv(type){
				if(type=='avatar'){
					uni.chooseImage({
						count: 1, //默认9
						sourceType: ['album','camera'], //从相册选择
						success: (res)=> {
							// 上传图片
							this.uploadImg(res.tempFilePaths[0]);
						}
					})
				}
				if(type=='nick_name'){
					this.ifShow =true;
					this.editTitle = '昵称修改';
					this.editType = 'text';
					this.editMsg = this.userInfo.nickName;
				}
			},
			// 头像上传
			uploadImg(url){
				uni.showLoading({
					title: '上传中'
				});
				postUpload(url).then(res=>{
					if(res.code==0) {
						this.userInfo.headimgurl = res.data.src;
						// 确认修改
						this.submitEv('avatar')
					}
					uni.hideLoading();
				})
			},
			// 确认修改
			submitEv(type){
				let params = {
					headimgurl:this.userInfo.headimgurl,
					nickname: this.userInfo.nickname,
				};
				if(type=='avatar'){
					params.headimgurl = this.userInfo.headimgurl
				}
				if(type=='nick_name'){
					params.nickname = this.editMsg
				}
				postEditInfo(params).then(res => {
					if(res.code == 0){
						console.log(res,'修改成功');
						if(type=='avatar'){
							this.$toolAll.tools.showToast('头像修改成功');
						}
						if(type=='nick_name'){
							this.$toolAll.tools.showToast('昵称修改成功')
							this.userInfo.nickname = this.editMsg ;
							this.ifShow =false;
						}
					}else{
						this.$toolAll.tools.showToast(res.msg)
					}
				})
			}
		}
	}
</script>
<style scoped>
	
</style>