<template>
	<view>
		<u-toast ref="messager" style="z-index:100090"></u-toast>
		<!--分享窗口-->
		<u-popup :show="show" :closeOnClickOverlay="false" @close="closeShare" :round="14" @open="openShare" mode="bottom">
			<view style="padding: 20px 20px;line-height: 60upx;">
				<view style="padding: 30upx 20upx;color:#666666;text-align: center;">
					<text>请输入要分享给人的姓名/工号</text>
					<text @click="closeShare" style="float: right;"><image src="/static/images/close.png" style="width: 50upx;height: 50upx;"></image> </text>
				</view>
				<view style="padding: 10upx;">
					<u-search @search="findUsers()" height="40px" bgColor="#ffffff" borderColor="rgba(153,153,153,0.3)" :showAction="false" shape="round" v-model="keyword" placeholder="姓名/工号"></u-search>
				</view>
				<!-- <view style="padding-top:20upx;">
					<u-button @click="findUsers()" type="primary" text="查询"></u-button>
				</view> -->
				<view style="padding-top: 50upx;">
					<view v-if="users.length>0" style="overflow-x: auto;height: 160upx;white-space: nowrap;overflow-y: hidden;">
						<view style="line-height: 60upx;display: inline-block;margin: 10upx;" v-for="(su,suidx) in users" :key="suidx" @click="confirmShare(su)">
							<view style="text-align: center;"><!--用户头像-->
								<view v-if="su.avatar" style="border-radius: 50%;"><image :src="$config.fileUrl+su.avatar" shape="circle" style="width: 80upx;height: 80upx;border-radius: 50%;"></image></view>
								<view v-else>
									<image v-if="su.sex === 1 " shape="circle" src="../../static/images/man.png" style="width: 80upx;height: 80upx;" alt=""></image>
									<image v-else shape="circle" src="../../static/images/woman.png" style="width: 80upx;height: 80upx;" alt=""></image>
								</view>
							</view>
							<view style="text-align: center;font-size: 22upx;">{{su.name}}({{su.code}})</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import apiShares from '@/api/modules/shares.js'
	import apiUser from '@/api/system/user.js'
	import apiStat from '@/api/phase2/stat.js'
	import apiMessage from '@/api/system/message.js'
	import { mapGetters } from 'vuex';
	export default {
		props: {
			data:{
				type:Object,
				default(){
					return{
						id:'',
						type:1,
					}
				}
			},
			type:{
			  type:Number,
			  default:0 //默认是课程
			},
			authorId:{
			  type:String,
			  default:'' //如果不存在就使用data的创建人
			},
			pageType:{
			  type:Number,
			  default:0
			},
			pageParams:{
			  type:String,
			  default:''
			}
		    
		},
		computed:{
			  ...mapGetters(['userInfo']),
		},
		data(){
			return {
				show:false,
				keyword:'',
				users:[],
			}
		},
		methods:{
			messageSave(refId,title,sendName,acceptName,acceptId,typeText){
			    let content='';
			    let conType;
			    if(this.type==1){ content='课程'; conType = this.data.type;  }
			    if(this.type==2){ content='文章' }
			    if(this.type==3){ content='案例' }
			    if(this.type==4){  content='问答' }
				content=sendName+typeText+content+'-'+title
				let message={
					content,
					refId,
					refType:this.type,
					sendName,
					pageType:this.type,
					pageParams:this.data.id,
					acceptName,
					acceptId,
					title:'系统消息',
					sendType:1,
					conType,
					content,
					source:1,
					sendAid:this.userInfo.aid,
				}
				apiMessage.save(message).then(res=>{
					if(res.status==200){
					  
					}
				})
			},
			openShare(){
			    this.show=true;
			},
			findUsers(){
				this.users=[];
				if(!this.keyword){
					//this.$refs.messager.show({message:'请输入工号或姓名',type:'warning'});
					uni.showToast({ title:'请输入工号或姓名',type:'fail'})
					return
				}
				var name=this.keyword;
				var regPos = /^\d+(\.\d+)?$/; //非负浮点数
				let $this=this;
				if(regPos.test(name)){
					apiUser.getByLoginName(name).then(rs=>{
						if(rs.status==200){
							//因为根据工号查询，只会是一个人，所有会有null情况，而json会返回空字符串
							if(rs.result){
								$this.users=[rs.result];
								$this.loadAuthorInfo($this.users,rs.result.aid);
							}else{
								//$this.$refs.messager.show({message:"无此用户",type:'warning'});
								uni.showToast({ title:'无此用户',type:'fail'})
							}
						}else{
							uni.showToast({ title:rs.message, icon: 'exception' });
							//this.$refs.messager.show({message:rs.message,type:'warning'});
						}
					})
				}else{
					apiUser.findByName(name).then(rs=>{
						if(rs.status==200){
							if(rs.result.length==0){
								uni.showToast({title: rs.message,icon: 'exception'});
								//this.$refs.messager.show({message:rs.message,type:'error'});
							}else{
								let ids=[];
								rs.result.forEach(item=>{
									// if(item.avatar){
									// 	item.avatar=this.$config.fileUrl + item.avatar;
									// }else{
									// 	item.sex=1;
									// 	ids.push(item.aid);
									// }
									// item.avatar='';
									item.sex=1;
									ids.push(item.aid);
								});
								this.loadAuthorInfo(rs.result,ids);
								this.users=rs.result;
							}
						}else{
							uni.showToast({ title: rs.message, icon: 'exception' });
							//this.$refs.messager.show({message:rs.message,type:'error'});
						}
					})
				}
			},
			closeShare(){
			  this.show=false;
			},
			confirmShare(u){
				if(this.type==0){
					//this.$refs.messager.show({message:'未指定源类型',type:'error'});
					uni.showToast({ title:'未指定源类型', icon: 'exception' });
					return;
				}
			    let conType;
				if(this.type == 1) {
					conType = this.data.type;
				}
				let postData={
				  objType:this.type,
				  objId:this.data.id,
				  content:'',
				  isRead:false,
				  toAid:u.aid,
				  toAname:u.name,
				  conType:conType,
				}
				if(this.userInfo.aid==postData.toAid){
					uni.showToast({title: '不能分享给自己', icon: 'error' });
					//this.$refs.messager.show({message:'不能分享给自己',type:'error',zIndex:100090});
				}
				let $this=this;
				let authorUserId=this.data.sysCreateAid;
				if(this.authorId){
					authorUserId=this.authorId;
				}
				apiShares.save(postData).then(rs=>{
					if(rs.status==200){
						if(rs.result){
							this.show=false;
							uni.showToast({title: '分享成功', icon: 'success' });
							//this.$refs.messager.show({message:'分享成功',type:'success'});
							let event = {
							  key: "Share",//分享
							  title: "分享",//分享
							  parameters:"author:"+authorUserId,//内容的作者
							  source:"h5",//移动端是2
							  content: '分享',//事件的内容
							  objId: this.data.id,//关联的id
							  objType: this.type,//关联的类型
							  objInfo: this.data.title? this.data.title:this.data.name,
							  aid: this.userInfo.aid, //当前登录人的id
							  aname: this.userInfo.name,//当前人的姓名
							  status: 1 //状态，直接写1
							}
							apiStat.sendEvent(event);
							this.$emit("success", rs.result);
							if(this.type!=3){
								if(this.type==1){
									//发送消息
									this.messageSave(this.data.id,this.data.name,this.userInfo.name,u.name,u.aid,'分享给我的');
								}else{
									this.messageSave(this.data.id,this.data.title,this.userInfo.name,u.name,u.aid,'分享给我的');
								}
							}
				
						}else{
							uni.showToast({title: '已分享过，不能再分享', icon: 'fail' });
							//this.$refs.messager.show({message:'已分享过，不能再分享',type:'warning'});
						}
						
					}else{
						uni.showToast({title:rs.message, icon: 'exception' });
						//this.$refs.messager.show({message:rs.message,type:'error',zIndex:100090});
						
					}
				});
			},
			loadAuthorInfo(list, ids) {
			    //加载作者信息，头像，机构信息
				if (ids.length == 0) {
					return;
				}
				apiUser.getByIds(ids).then(res => {
					if (res.status == 200) {
					  list.forEach((item, index) => {
						res.result.some(author => {
							if (author.aid == item.teacherId) {
								item.name= author.name;
								item.orgInfo=cutOrgNamePath(author.orgInfo);
								item.avatar= author.avatar;
								item.code= author.code;
								item.sex=author.sex;
								// if(author.avatar != '') {
								// 	item.avatar=this.$config.fileUrl + author.avatar;
								// }
								return true;
							}else{
								return false;
							}
						});
					  });
					} else {
					  console.log('获取用户信息失败:'+res.error);
					  //this.$message.error(res.message);
					}
				});
			},
		}
		
	}
</script>

<style>
</style>