<template>
	<view>
		<view class="content">
			<view class="info-item">
				<view class="label">头像</view>
				<view class="right">
					<u-avatar :src="headImg" size="40" @tap="chooseImage()" ></u-avatar>
					<!-- <image  class="head" @tap="chooseImage()" :src="headImg" mode=""></image> -->
				</view>
			</view>
			<view class="info-item">
				<view class="label">姓名</view>
				<view class="right">
					<!-- <text class="text">{{nickName}}</text> -->
					<input type="text" v-model="nickName" placeholder="请输入姓名" class="input"></text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">部门</view>
				<view class="right">
					<text class="text">{{deptName}}</text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">岗位</view>
				<view class="right">
					<text class="text">{{postNames}}</text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">角色</view>
				<view class="right">
					<text class="text">{{roleNames}}</text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">性别</view>
				<view class="right">
					<picker @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="email" placeholder="请输入邮箱" class="input"></text>
			 	</view>
			 </view>
			<view class="info-item">
				<view class="label">手机号</view>
				<view class="right">
					<input type="text" v-model="phonenumber" placeholder="请输入手机号" class="input"></text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">客服二维码</view>
				<view class="right">
					<!-- <u-avatar :src="codeimg" size="40" @tap="chooseImages()" :default-url="defaultUrl"></u-avatar> -->
					<image  class="w90 h90" @tap="chooseImages()" :src="codeimg?codeimg:defaultUrl" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="btn-box">
			<view class="sub-btn" @click="submit()">保存修改</view>
		</view>
	</view>
	

</template>

<script>
	import {setPwd,setHeadImg,setUserInfo,getCompanyUser} from '@/api/user.js';
	export default {
		data() {
			return {
				nickName:"",
				deptName:"",
				postNames:"",
				phonenumber:"",
				email:"",
				sexPicker: ['男', '女','未知'],
				sex: 0, // 性别
				headImg: this.$store.state.imgpath+'/app/images/detault_head.jpg', // 头像,
				headImgUrl:'',
				defaultUrl:'https://beiliyo-2025.obs.cn-north-4.myhuaweicloud.com/fs/20250606/1749180611713.png',
				codeimg:'',
				codeurlimg:'',
				roleNames:''
			}
		},
		onLoad() {
			this.getCompanyUser();
		},
		methods: {
			bindUser(data){
				var that=this;
				that.nickName=data.nickName;
				that.deptName=data.dept.deptName;
				if(data.posts){
					that.postNames=data.posts.map(item=>item.postName).join(",")
				}
				if(data.roles){
					this.roleNames=data.roles.map(item=>item.roleName).join(",")
				}
				// that.posts=data.post;
				that.phonenumber=data.phonenumber;
				that.email=data.email;
				that.sex=data.sex;
				if(!that.utils.isEmpty(data.qrCodeWeixin)){
					that.codeimg=uni.getStorageSync('requestPath')+data.qrCodeWeixin;
					this.codeurlimg=data.qrCodeWeixin;
				}
				if(!that.utils.isEmpty(data.avatar)){
					that.headImg=uni.getStorageSync('requestPath')+data.avatar;
					this.headImgUrl=data.avatar;
				}
			},
			getCompanyUser(){
				var data = {};
				var that=this;
				getCompanyUser(data).then(
					res => {
						that.bindUser(res.user);
						
					},
					rej => {}
				);
			},
			// 性别选择
			pickerSex(e) {
				this.sex = e.detail.value
			},
			// 选择头像照片
			chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						//this.headImg = res.tempFilePaths[0]
						uni.uploadFile({
							url: uni.getStorageSync('requestPath')+'/companyapp/app/common/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							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.headImgUrl=data.fileName;
							}
						});
						
					}
				});
			},
			// 选择头像照片
			chooseImages() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						//this.headImg = res.tempFilePaths[0]
						uni.uploadFile({
							url: uni.getStorageSync('requestPath')+'/companyapp/app/common/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'  // 上传附带参数
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes)
								// 根据接口具体返回格式   赋值具体对应url
								var data=JSON.parse(uploadFileRes.data)
								this.codeimg=uni.getStorageSync('requestPath')+data.fileName;
								this.codeurlimg=data.fileName
							}
						});
						
					}
				});
			},
			// 预览头像
			viewImage() {
				uni.previewImage({
					urls: this.headImg,
					current: this.headImg
				});
			},
			submit(){
				if (this.utils.isEmpty(this.email)) {
					uni.showToast({
						title: "请输入邮箱",
						icon: 'none',
					});
					return
				}
				if (this.utils.isEmpty(this.phonenumber)) {
					uni.showToast({
						title: "请输入手机号",
						icon: 'none',
					});
					return
				}
				var data = {mobile:this.phonenumber,email:this.email,
				name:this.nickName,
				sex:this.sex.toString(),headImg:this.headImgUrl,qrCodeWeixin:this.codeurlimg};
				console.log(data);
				setUserInfo(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
								title: '修改成功',
								duration: 2000
							});
							setTimeout(function() {
								uni.navigateBack({
								  success: () => {
								  }
								})
							}, 500);
						}
						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: #115296;
			border-radius: 44upx;
		}
	}
</style>
