<template>
	<view>
		<view class="content">
			<view class="info-item">
				<view class="label">头像</view>
				<view class="right">
					<image  class="head" @tap="chooseImage()" :src="headImg" mode=""></image>
				</view>
			</view>
			<view class="info-item">
				<view class="label">姓名</view>
				<view class="right">
					<input type="text" v-model="nickName" placeholder="请输入姓名" class="input" />
				</view>
			</view>
			<view class="info-item">
				<view class="label">性别</view>
				<view class="right x-end"  style="width:80%;height: 40px;justify-content: end;">
					<picker style="width: 100%;text-align: right;" @change="pickerSex" :value="sex" :range="sexPicker">
						<view class="picker">
							{{sex>-1?sexPicker[sex]:''}}
						</view>
					</picker>
				</view>
			</view>
			<view class="info-item">
				<view class="label">手机号</view>
				<view class="right">
					<input type="text" v-model="phonenumber" placeholder="请输入手机号" class="input" />
				</view>
			</view>
		</view>
		
		<view class="btn-box">
			<view class="sub-btn" @click="submit()">保存修改</view>
		</view>
	</view>
	

</template>

<script>
	import {setPwd,setHeadImg,setUserInfo,getUserInfo} from '@/api/user.js';
	import { premissionCheck } from "@/js_sdk/wa-permission/permission.js"
	export default {
		data() {
			return {
				nickName:"",
				deptName:"",
				postNames:"",
				phonenumber:"",
				email:"",
				sexPicker: ['男', '女','未知'],
				sex: 0, // 性别
				headImg: '/static/images/detault_head.jpg', // 头像,
				headImgUrl:'',
			}
		},
		onLoad() {
			 
			this.getUserInfo();
		},
		methods: {
			bindUser(data){
				var that=this;
				that.nickName=data.user.nickName;
				that.posts=data.post;
				that.phonenumber=data.user.phone;
				that.email=data.user.email;
				if(data.user.sex!=undefined){
					that.sex=data.user.sex;
				}
				if(data.post!=null&&data.post.length>0){
					var postNameStr="";
					data.post.forEach( (v,i) => {
						postNameStr+=v+",";
					},this);
					that.postNames=postNameStr.substr(0,postNameStr.length-1);
					
				}
				if(!that.$isEmpty(data.user.avatar)){
					//this.headImg=uni.getStorageSync('requestPath')+data.user.avatar;
					this.headImg=data.user.avatar;
					this.headImgUrl=data.user.avatar;
				}
			},
			getUserInfo(){
				var data = {};
				var that=this;
				getUserInfo(data).then(
					res => {
						that.bindUser(res);
						
					},
					rej => {}
				);
			},
			// 性别选择
			pickerSex(e) {
				this.sex = e.detail.value
			},
			// 选择照片
			chooseImage() {
				// #ifdef APP-PLUS
				if (!this.$isAgreePrivacy()) {
					uni.showToast({
						title: "请同意隐私政策",
						icon: "none"
					});
					return;
				}
				uni.showActionSheet({
					itemList: ['拍摄','从相册选择'],
					success: async (res) => {
						if (res.tapIndex == 0) { //拍摄
							await this.handleSelectImage('camera', "CAMERA");
						} else { //相册
							await this.handleSelectImage('album', "EXTERNAL_STORAGE");
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
				// #endif
			},
			async handleSelectImage(sourceType, permissionName) {
				let result = await premissionCheck(permissionName);
				if (result == 1) {
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: [sourceType], //从相册选择
						success: (res) => {
							this.uploadImage(res.tempFilePaths[0]);
						}
					});
				}
			},
			uploadImage(filePath) {
				uni.uploadFile({
					url: uni.getStorageSync('requestPath') + '/app/common/uploadOSS', //仅为示例，非真实的接口地
					filePath: filePath,
					name: 'file',
					formData: {
						'user': 'test' // 上传附带参数
					},
					success: (uploadFileRes) => {
						console.log(uploadFileRes)
						// 根据接口具体返回格式   赋值具体对应url
						var data = JSON.parse(uploadFileRes.data)
						//this.headImg=uni.getStorageSync('requestPath')+data.fileName;
						this.headImg = data.url;
						this.headImgUrl = data.url;
					}
				});
			},
			// 预览头像
			viewImage() {
				uni.previewImage({
					urls: this.headImg,
					current: this.headImg
				});
			},
			submit(){
				if (this.$isEmpty(this.phonenumber)) {
					uni.showToast({
						title: "请输入手机号",
						icon: 'none',
					});
					return
				}
				var data = {phone:this.phonenumber,email:this.email,sex:this.sex.toString(),avatar:this.headImgUrl,nickName:this.nickName};
				console.log(data);
				setUserInfo(data).then(res => {
						if(res.code==200){
							uni.showToast({
								title: '修改成功',
								duration: 2000
							});
						}
						else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
							});
						}
						
					},
					rej => {}
				);
			}
		}
	}
</script>


<style scoped lang="scss">
	.content{
		padding-top: 20rpx;
		border-top: 1px solid #F5F6FA;
	}
	.info-item{
		height: 104upx;
		background: #FFFFFF;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #F5F6FA;
		&:last-child{
			border-bottom: none;
		}
		.label{
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		.right{
			display: flex;
			align-items: center;
			justify-content: center;
			.text{
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
			.image{
				margin-left: 10upx;
				width: 30upx;
				height: 30upx;
			}
			.input{
				text-align: right;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
		}
		.head{
			border-radius: 50%;
			width: 80upx;
			height: 80upx;
		}
		.arrow{
			width: 30upx;
			height: 30upx;
		}
		.text{
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		.input{
			text-align: right;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		&.password{
			margin-top: 20upx;
			.right{
				display: flex;
				align-items: center;
				.text{
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #0F1826;
					margin-right: 15upx;
				}
			}
		}
	}
	.btn-box{
		margin-top: 15rpx;
		height: 120upx;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
		.sub-btn{
			width: 100%;
			height: 88upx;
			line-height: 88upx;
			text-align: center;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			background: #FF5C03;
			border-radius: 44upx;
		}
	}


</style>
