<template>
	<view class="content">
		<!-- 个人信息 -->
		<view class="user-info">
			<view class="info-box">
				<view class="left">
					<view class="head-box">
						<image class="img" :src="avatar" mode="aspectFill"></image>
					</view>
					<view class="info">
						<text class="name">{{nickName}}</text>
						<text class="title">{{postNames}}</text>
					</view>
				</view>
				<image v-if="!isShow" class="right" src="/static/images/icon_edit.png" mode="aspectFill" @click="editInfo"></image>
			</view>
			<!-- 公司 -->
			<view class="comp-info">
				<image class="img" src="/static/images/icon_comp_white.png" mode="aspectFill"></image>
				<text class="text">{{deptName}}</text>
			</view>
		</view>
		<!-- 详细信息 -->
		<view class="info-detail">
			<view class="item">
				<text class="label">姓名</text>
				<text class="text">{{nickName}}</text>
			</view>
			<view class="item">
				<text class="label">性别</text>
				<text class="text">{{sex}}</text>
			</view>
			<view class="item column" @click="callPhone(phonenumber)">
				<view class="left">
					<text class="label">手机</text>
					<text class="text">{{phonenumber}}</text>
				</view>
				<image class="img" src="/static/images/icon_phone_left.png" mode="aspectFill"  ></image>
			</view>
			<view class="item">
				<text class="label">邮箱</text>
				<text class="text">{{email}}</text>
			</view>
			<view class="item">
				<text class="label">部门</text>
				<text class="text">{{deptName}}</text>
			</view>
			<view class="item">
				<text class="label">岗位</text>
				<text class="text">{{postNames}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {getUserInfoByUserId,getUserInfo} from '@/api/user.js';
	export default {
		data() {
			return {
				avatar:"/static/images/default.png", 
				nickName:"",
				deptName:"",
				postNames:"",
				phonenumber:"",
				email:"",
				sex:"",
				isShow:false,
				userId:undefined,
			}
		},
		onLoad(option) {
			// 修改顶部导航背景色
			// uni.setNavigationBarColor({
			// 	frontColor: '#ffffff',
			// 	backgroundColor: '#4BC9B1',
			// 	animation: {
			// 		duration: 400,
			// 		timingFunc: 'easeIn'
			// 	}
			// })
			if(!this.utils.isEmpty(option.userId)){
				this.userId = option.userId;
				this.isShow=true;
			}
			
		},
		onShow() {
			if(this.isShow){
				this.getUserInfoByUserId(this.userId);
			}
			else{
				this.getUserInfo();
			}
		},
		methods: {
			bindUser(data){
				var that=this;
				that.nickName=data.user.nickName;
				that.deptName=data.user.dept.deptName;
				that.posts=data.post;
				that.phonenumber=data.user.phonenumber;
				that.email=data.user.email;
				if(data.user.sex==0){
					that.sex="男";
				}
				else if(data.user.sex==1){
					that.sex="女";
				}
				else if(data.user.sex==2){
					that.sex="未知";
				}
				if(data.post!=null&&data.post.length>0){
					var posts=[];
					data.post.forEach( (v,i) => {
						posts.push(v);
					},this);
					that.postNames=posts.toString()
				}
				if(!that.utils.isEmpty(data.user.avatar)){
					that.avatar=uni.getStorageSync('requestPath')+data.user.avatar;
				}
			},
			getUserInfoByUserId(userId){
				var data = {userId:userId};
				var that=this;
				getUserInfoByUserId(data).then(
					res => {
						that.bindUser(res);
						
					},
					rej => {}
				);
			},
			getUserInfo(){
				var data = {};
				var that=this;
				getUserInfo(data).then(
					res => {
						that.bindUser(res);
					},
					rej => {}
				);
			},
			// 拨打电话
			callPhone(tel){
				uni.makePhoneCall({
					phoneNumber: tel
				})
			},
			// 个人信息编辑
			editInfo() {
				uni.navigateTo({
					url: '../editUser'
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #fff;
	}
</style>
<style scoped lang="scss">
	.content{
		// 个人信息
		.user-info{
			background: #115296;
			padding: 0 30upx;
			.info-box{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.left{
					display: flex;
					align-items: center;
					justify-content: center;
					padding: 20upx 0;
					.head-box{
						width: 120upx;
						height: 120upx;
						line-height: 100upx;
						font-size: 30upx;
						color: #fff;
						text-align: center;
						margin-right: 20upx;
						.img{
							border-radius: 50%;
							width: 100%;
							height: 100%;
						}
					}
					.info{
						height: 80upx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.name{
							font-size: 30upx;
							color: #fff;
						}
						.title{
							font-size: 28upx;
							color: #fff;
						}
					}
				}
				.right{
					width: 40upx;
					height: 40upx;
				}
			}
			.comp-info{
				padding: 20upx 0 40upx;
				display: flex;
				align-items: center;
				.img{
					width: 30upx;
					height: 30upx;
					margin-right: 20upx;
				}
				.text{
					font-size: 30upx;
					color: #fff;
				}
			}
		}
		// 详细信息
		.info-detail{
			padding: 0 24upx;
			.item{
				padding: 20upx 0;
				border-bottom: 1px solid #f7f7f7;
				.label{
					font-size: 30upx;
					color: #999;
					margin-right: 20upx;
				}
				.text{
					font-size: 30upx;
					color: #333;
				}
				&.column{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.img{
						width: 50upx;
						height: 50upx;
					}
				}
			}
		}
	}
</style>
