<template>
	<view>
		<cu-custom bgColor="bg-white"  :isBack="true">
			<template #backText></template>
			<template #content>个人信息</template>
		</cu-custom>
		<view class="padding flex justify-start align-center">
			<block v-for="(items,indexs) in imglist" :key="indexs" v-if="network_log == 'none'">
				<view class='cu-avatar lg radius mr-15' v-if="detail.imgname === items.name" @tap="showAvatar(detail)" :style="'background-image:url('+items.path+')'"></view>
			</block>
			<view v-else class='cu-avatar lg radius mr-15' @tap="showAvatar(detail)" :style="'background-image:url('+detail.avatar+')'"></view>
			
			<view class='im-flex im-justify-content-start im-columns'>
				<view class="mb-5">{{detail.friend?.nickname?detail.friend.nickname:detail.realname}}</view>
				<view class="text-gray">{{detail.account}}</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item"  v-if="globalConfig.sysInfo.runMode==2 && detail.friend && userInfo.user_id!=detail.user_id" @tap="setNickname">
				<view class="content">
					<text class="cuIcon-edit text-green"></text>
					<text>备注</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">{{detail.friend.nickname || '未设置'}}</text>
					<text class="text-grey text-sm ml-5 cuIcon-write"></text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-mail text-green"></text>
					<text>昵称</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">{{detail.realname}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-mail text-green"></text>
					<text>邮箱</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">{{detail.email ?? ''}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-safe text-green"></text>
					<text>性别</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">{{ sex(detail.sex)}}</text>
				</view>
			</view>
			<view class="cu-item" v-if="parseInt(globalConfig.sysInfo.ipregion)">
				<view class="content">
					<text class="cuIcon-location text-green"></text>
					<text>IP</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm" v-if="detail.last_login_ip">{{ detail.last_login_ip || "未知"}} （{{detail.location || "未知"}}）</text>
					<text class="text-grey text-sm" v-else>未知</text>
				</view>
			</view>
			<view class="cu-item" @click="toFriendsCircle(detail)">
				<view class="content flex">
					<view class="flex justify-center" style="width: 1.6em;margin-right: 0.3125rem;"><uni-icons type="pyq" size="15" color="#45ba5b"></uni-icons></view>
					<view>朋友圈</view>
				</view>
				<view class="action">
					<text class="text-grey cuIcon-right"></text>
				</view>
			</view>
		</view>
		
		<template class="" v-if="userInfo.user_id!=detail.user_id">
			<view class="padding flex flex-direction" v-if="globalConfig.sysInfo.runMode==1 || detail.friend">
				<button class="cu-btn bg-green mt-10 lg" @tap="sendMsg(detail)">发消息</button>
				<button class="cu-btn bg-blue mt-10 lg" v-if="validatePhone" @tap="callPhone()">打电话</button>
				<!-- #ifdef APP | H5 -->
				<button class="cu-btn bg-grey mt-10 lg" @tap="modelName='callRtc'" v-if="parseInt(globalConfig.chatInfo.webrtc) && parseInt(globalConfig.chatInfo.simpleChat)">音视频通话</button>
				<!-- #endif -->
				
				<button class="cu-btn bg-red  mt-10 lg" @tap="delFriend()" v-if="globalConfig.sysInfo.runMode==2">删除好友</button>
			</view>
			<view class="padding flex flex-direction" v-if="globalConfig.sysInfo.runMode==2 && !detail.friend">
				<button class="cu-btn bg-green lg" @tap="addFriend()">加好友</button>
			</view>
		</template>
		<view class="cu-modal bottom-modal" :class="modelName=='callRtc'?'show':''"  @tap="modelName=''">
			<view class="cu-dialog">
				<view class="manage-content">
					<view class="cu-list menu bg-white">
						<view class="cu-item" @tap="calling(0)">
							<view class="content padding-tb-sm">
								<text class="cuIcon-dianhua"></text>
								<text>语音通话</text>
							</view>
						</view>
						<view class="cu-item" @tap="calling(1)">
							<view class="content padding-tb-sm">
								<text class=" cuIcon-record"></text>
								<text>视频通话</text>
							</view>
						</view>
						<view class="parting-line-5"></view>
						<view class="cu-item" @tap="modelName=''">
							<view class="content padding-tb-sm">
								<text class="c-red">取消</text>
							</view>
						</view>

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

<script>
	import { useMsgStore } from '@/store/message';
	import { useloginStore } from '@/store/login';
	// #ifdef APP-PLUS
	import {getSavedImages} from '@/utils/LocalFileSystemURL.js'
	// #endif
	import getUserInfo from '@/service/getUserInfo';
	import pinia from '@/store/index'
	const msgStore = useMsgStore(pinia)
	const userStore = useloginStore(pinia)
	export default {
		data() {
			return {
				modelName:'',
				detail:{},
				userInfo:userStore.userInfo,
				globalConfig:userStore.globalConfig,
				network_log:'',
				imglist:[],
				ids:0
			}
		},
		computed: {
		    validatePhone(){
				let reg = /^1[3456789]\d{9}$/;
				return reg.test(this.detail.account);
			}
		},
		onLoad(options) {
			this.network_log = uni.getStorageSync('network_log')
			
			const _this = this
			if(_this.network_log == 'none'){
				this.getImagePath()
				this.getUserInfoList(options.id)
			}else{
				this.ids = options.id
				this.getInfo()
				// this.getImagePath()
				// this.getUserInfoList(options.id)
			}
		}, 
		methods: {
			getInfo(){
				this.$api.msgApi.getUserInfo({user_id:this.ids}).then((res)=>{
					if(res.code==0){
						this.detail=res.data;
						const list = []
						list.push(res.data)
						this.insertUserInfo(list)
					}
				})
			},
			async insertUserInfo(val){
				await getUserInfo.batchInsertOrUpdate(val)
			},
			async getUserInfoList(user_id){
				try {
					const list = await getUserInfo.getList({user_id:user_id});
					list.forEach((item)=>{
						const parts = item.avatar.split('/');
						let lastPart = parts.pop() || parts.pop() || '';
						const isNumber = !isNaN(lastPart)&&!isNaN(parseFloat(lastPart));
						item.imgname = isNumber ? lastPart+'.png' : lastPart;
						item.friend = JSON.parse(item.friend);
						item.setting = JSON.parse(item.setting);
					})
					this.detail = list[0];
					console.log('获取个人信息数据',list.length,list);
				} catch (error) {
				    console.error(error);
				}
			},
			async getImagePath(){
				this.imglist = await getSavedImages()
				this.imglist.map(item => {
				  item.path = plus.io.convertLocalFileSystemURL(item.path)
				});
				// console.info(this.imglist,'读取地址');
			},
			showAvatar(detail){
				let imgs=[];
				imgs.push(detail.avatar);
				uni.previewImage({urls : imgs})
			},
			sendMsg(info){
				uni.reLaunch({
					url:"/pages/message/chat?id="+info.user_id
				})
			},
			toFriendsCircle(info){
				uni.navigateTo({
					url:"/pages/compass/personalcircleoffriends?user_id="+info.user_id+"&avatar="+info.avatar+"&realname="+info.realname
				})
			},
			sex(value) {
						let arr = ['女', '男','未知']
						return arr[value] || '未知';
			},
			callPhone(){
				uni.makePhoneCall({
					phoneNumber: this.detail.account
				});
			},
			calling(is_video){
				if(msgStore.webrtcLock){
					return uni.showToast({
						title:'其他终端正在通话中',
						icon:'none'
					})
				}
				this.modelName='';
				let msg_id=this.$util.getUuid();
				uni.navigateTo({
				  url: '/pages/message/call?msg_id='+msg_id+'&type='+is_video+'&status=1&id='+this.detail.user_id+'&name='+this.detail.realname+'&avatar='+encodeURI(this.detail.avatar)
				})
			},
			delFriend(){
				uni.showModal({
					title: '确定要删除该好友吗？',
					success: (res)=>{
						if (res.confirm) {
							let data={ id: this.detail.user_id};
							this.$api.friendApi.delFriend(data).then((res)=>{
								if(res.code==0){
									msgStore.deleteContacts(data);
									// console.log(data,'123456');
									uni.reLaunch({
										url: '/pages/index/index'
									})
								}
							})
						}
					},
				})
				
			},
			addFriend(){
				uni.showModal({
					title: '请输入验证信息',
					editable:true,
					success: (res)=>{
						if (res.confirm) {
							if(res.content==''){
								return uni.showToast({
									title:'请输入备注！',
									icon:'error'
								})
							}
							this.$api.friendApi.addFriend({user_id:this.detail.user_id,remark:res.content}).then((e)=>{
								if(e.code==0){
									uni.showToast({
										title:e.msg,
										icon:'none'
									})
									this.getInfo()
								}
							})
						}
					}
				});
			},
			setNickname(){
				let friend_id=this.detail.friend.friend_id ?? '';
				if(!this.detail.friend){
					return uni.showToast({
						title:'无法设置',
						icon:'error'
					})
				}
				uni.showModal({
					title: '请输入备注信息',
					editable:true,
					success: (res)=>{
						if (res.confirm) {
							// if(res.content==''){
							// 	return uni.showToast({
							// 		title:'请输入好友备注！',
							// 		icon:'error'
							// 	})
							// }
							this.$api.friendApi.setNickname({friend_id:friend_id,nickname:res.content}).then((e)=>{
								if(e.code==0){
									this.detail.friend.nickname=res.content;
									// 修改备注后修改联系人
									msgStore.updateContacts({
										id:this.detaild.user_id,
										displayName:res.content
									})
									uni.showToast({
										title:e.msg,
										icon:'none'
									})
								}
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
