<template>
	<view>
		<!-- :id="`zp-id-${item.zp_index}`"和:key="item.zp_index" 必须写，必须写！！！！ -->
		<!-- 这里for循环的index不是数组中真实的index了，请使用item.zp_index获取真实的index -->
		<view class="item" :id="`zp-id-${item.zp_index}`" :key="item.zp_index" v-for="(item,index) in orderList">
			<view-item :index="index" :item="item" @onLongPress="onLongPress"></view-item>
		</view> 
		
		<view class="shade" v-show="showShade" @tap="hidePop">
			<view class="pop" :style="popStyle" :class="{'show':showPop}">
				<view v-for="(item,index) in popButton" :key="index" @tap="pickerMenu" :data-index="index">{{item}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import ViewItem from '@/pages/tabbar/components/message/viewItem.vue';
	import ImConversationApi from '@/sheep/api/im/memberConversation';
	import dayjs from 'dayjs';
	import relativeTime from "dayjs/plugin/relativeTime";
	dayjs.extend(relativeTime);
	export default {
		components: {  
			ViewItem,
		},
		props: {
			virtualList: {
				type: Array,
				default: [],
			},
		},
		data() {
			return {
				winSize: {},
				/* 显示遮罩 */
				showShade: false,
				/* 显示操作弹窗 */
				showPop: false,
				/* 弹窗按钮列表 */
				popButton: [],
				/* 弹窗定位样式 */
				popStyle: "",
				/* 选择的用户下标 */
				pickerUserIndex: -1,
				paddingTop: 0,
				paddingBottom: 100,
			}
		}, 
		computed: {
			orderList() {
				this.virtualList.forEach((order) => order.time = this.showDayTime(order.lastMessageTime));
				return this.virtualList;
			},
		},
		created() {
			this.getWindowSize();
			this.$emit('reload');
		},
		methods: {
			/* 获取窗口尺寸 */
			getWindowSize() {
				uni.getSystemInfo({
					success: (res) => {
						this.winSize = {
							"witdh": res.windowWidth,
							"height": res.windowHeight
						}
					}
				})
			},
			/* 长按监听 */
			onLongPress(e) {
				let [touches, style, index] = [e.touches[0], "", e.currentTarget.dataset.index];
				
				/* 因 非H5端不兼容 style 属性绑定 Object ，所以拼接字符 */
				if (touches.clientY > (this.winSize.height / 2)) {
					style = `bottom:${this.winSize.height-touches.clientY}px;`;
				} else {
					style = `top:${touches.clientY}px;`;
				}
				if (touches.clientX > (this.winSize.witdh / 2)) {
					style += `right:${this.winSize.witdh-touches.clientX}px`;
				} else {
					style += `left:${touches.clientX}px`;
				}
				
				this.popStyle = style;
				this.pickerUserIndex = Number(index);
				var conversation = this.orderList[this.pickerUserIndex];
				if(conversation.pinned == '0'){
					this.popButton = ["置顶聊天", "删除该聊天", "拉黑该用户"];
				}else{
					this.popButton = ["取消置顶", "删除该聊天", "拉黑该用户"];
				}
				this.showShade = true;
				this.$nextTick(() => {
					setTimeout(() => {
						this.showPop = true;
					}, 10);
				});
			},
			pinned(id, pinned) {
				var form = {
					pinned: pinned,
					id: id,
				};
				ImConversationApi.pinnedMemberConversation(form).then(res => {
					this.$emit('reload');
					this.$refs.paging.reload();
				});
			},
			delete(id) {
				ImConversationApi.deleteMemberConversation(id).then(res => {
					this.$emit('reload');
				});
			},
			slashed(id) {
				ImConversationApi.slashedMemberConversation(id).then(res => {
					this.$emit('reload');
				});
			},
			/* 隐藏弹窗 */
			hidePop() {
				this.showPop = false;
				this.pickerUserIndex = -1;
				setTimeout(() => {
					this.showShade = false;
				}, 250);
			},
			/* 选择菜单 */
			pickerMenu(e) {
				let index = Number(e.currentTarget.dataset.index);
				console.log(`第${this.pickerUserIndex+1}个用户,第${index+1}个按钮`);
				var conversation = this.orderList[this.pickerUserIndex];
				
				if(index == 0){
					if(conversation.pinned == '0'){
						this.pinned(conversation.id, '1');
					}else{
						this.pinned(conversation.id, '0');
					}
				}else if(index == 1){
					this.delete(conversation.id);
				}else if(index == 2){
					this.slashed(conversation.id);
				}
			
				this.hidePop();
			},
			showDayTime(datetime) {
			  if (!datetime) return "";
			  return dayjs(datetime).fromNow();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		background-color: #fff;
	}
	
	/* 遮罩 */
	.shade {
		position: fixed;
		z-index: 100;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		-webkit-touch-callout: none;
	
		.pop {
			position: fixed;
			z-index: 101;
			width: 260rpx;
			box-sizing: border-box;
			font-size: 28upx;
			text-align: left;
			color: #333;
			background-color: #fff;
			box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
			line-height: 80upx;
			transition: transform 0.15s ease-in-out 0s;
			user-select: none;
			-webkit-touch-callout: none;
			transform: scale(0, 0);
	
			&.show {
				transform: scale(1, 1);
			}
	
			&>view {
				padding: 0 20upx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				user-select: none;
				-webkit-touch-callout: none;
	
				&:active {
					background-color: #f3f3f3;
				}
			}
		}
	}
</style>