<template>
	<view>
		<u-toast ref="messager"></u-toast>
		<!--分享窗口-->
		<u-popup :show="show" @close="closeShare" @open="openShare" mode="right">
			<view style="padding: 10px 20px;line-height: 60upx;">
				<view style="padding: 10upx;">请输入要分享给人的姓名</view>
				<view style="padding: 10upx;">
					<u--input placeholder="请输入人员姓名" border="surround" v-model="keyword"></u--input>
				</view>
				<view style="padding-top:20upx;">
					<u-button @click="findUsers()" type="primary" text="查询"></u-button>
				</view>
				<view style="padding-top: 50upx;">
					<!--查询内容列表-->
					<view style="line-height: 60upx;" v-for="(su,suidx) in users" :key="suidx" @click="confirmShare(su)">{{suidx+1}}, {{su.name}} （{{su.code}}）</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import apiShares from '@/api/modules/shares.js'
	import { mapGetters } from 'vuex';
	export default {
		props: {
			data:{
				type:Object,
				default(){
					return{
						id:'',
						type:1,
					}
				}
			},
			type:{
			  type:Number,
			  default:0 //默认是课程
			},
		    
		},
		computed:{
			  ...mapGetters(['userInfo']),
		},
		data(){
			return {
				show:false,
				keyword:'',
				users:[],
			}
		},
		methods:{
			openShare(){
			    this.show=true;
			},
			findUsers(){
				this.users=[];
				if(!this.keyword){
					return this.$refs.messager.show({message:'请输入工号或姓名',type:'warning'});
				}
				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];
							}else{
								$this.$refs.messager.show({message:"无此用户",type:'warning'});
							}
						}else{
							//uni.showToast({ title: rs.message, icon: 'none' });
							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: 'none'});
								this.$refs.messager.show({message:rs.message,type:'error'});
							}else{
								this.shareInfo.users=rs.result;
							}
						}else{
							//uni.showToast({ title: rs.message, icon: 'none' });
							this.$refs.messager.show({message:rs.message,type:'error'});
						}
					})
				}
			},
			closeShare(){
			  this.show=false;
			},
			confirmShare(u){
			    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: 'none' });
					this.$refs.messager.show({message:'不能分享给自己',type:'error'});
				}
				apiShares.save(postData).then(rs=>{
					if(rs.status==200){
						if(rs.result){
							this.shareInfo.show=false;
							this.$refs.messager.show({message:'分享成功',type:'success'});
							this.$emit("share-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: 'none'});
							this.$refs.messager.show({message:'分享失败,您不能将同一资源多次分享给同一个人',type:'warning'});
						}
					}else{
						// this.$message({message:'分享失败',type:'success'});
						this.$refs.interactToast.show({message:'分享处理失败',type:'error'});
						console.log(rs.message);
					}
				});
			},
		}
		
	}
</script>

<style>
</style>