<template>
	<view class="team black_style">
		<Nav className="black_nav" :backVisible="true" :scroll="scroll">
			<block slot="content" class="title">
				<view class="name omit">{{ $t('team.square.title') }}</view>
				<view class="btns">
					<view class="btn" @click="link('/pages/index/news')">
						<view class="iconfont">
							<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/email.png"></image>
							<text class="value" v-if="options.messagesNum > 0">{{ options.messagesNum > 9 ? `9+` : options.messagesNum }}</text>
						</view>
					</view>
					<view class="btn" @click="openChage">
						<view class="iconfont">
							<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/add.png"></image>
						</view>
						<!-- menus -->
						<meuns :visible="isOpen" :params="openInfo" @callback="menusCallback" />
					</view>
				</view>
			</block>
		</Nav>
		<view :style="{paddingTop: `${CustomBar}px;`}">
			<scroll-view
				class="body_scroll clearfix"
				:scroll-y="true"
				:style="scrollHeight"
				:refresher-enabled="true"
				:refresher-triggered="triggered"
				@refresherrefresh="onRefresh"
				@refresherrestore="onRestore"
				@scrolltolower="scrolltolower">
				<view class="square_list" v-if="list && list.length">
					<view class="list">
						<view class="item" :style="scrollItem" v-for="(item, key) in list" :key="key" @click="toRoom(item)">
							<view class="line">
								<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/square_room_line.gif" v-if="item.onlineNum !== 0"></image>
								<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/quiet_line.png" v-else></image>
							</view>
							<view class="subtitle">{{ item.teamName | across }}</view>
							<view class="highlight">
								<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/highlight.png"></image>
							</view>
							<view class="core">
								<view class="number_of__people">{{ item.onlineNum || 0 }}人</view>
								<view class="name lineClamp2">{{ item.topic | across }}</view>
								<view class="member">
									<view class="user" v-for="(i, k) in item.users" :key="k" :class="'user_0' + (k + 1)">
										<image class="pic" mode="aspectFill" :src="i.userAvatarUrl" v-if="i.id"></image>
										<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/member_mask.png" v-else></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- noData -->
				<view class="no_data active" v-if="!loading && !list.length">
					<view>
						<view class="name">没有喜欢的交流？不如发起一场</view>
						<view class="more arrow" @click="link('/pages/index/communication')">发起交流</view>
					</view>
				</view>
				<view class="no_data" v-else>
					<view>
						<view class="name">没有喜欢的交流？不如发起一场</view>
						<view class="more arrow" @click="link('/pages/index/communication')">发起交流</view>
					</view>
				</view>
			</scroll-view>
			<!-- loading -->
			<loading :visible="loading" />
		</view>
	</view>
</template>

<script>
	import mixins from '@/utils/mixins'
	import Meuns from '@/wxcomponents/menus/menus.vue'

	export default {
		components: {
			Meuns
		},
		props: {
			scroll: {
				type: [Number, String],
				default: 0
			},
			active: [Number, String]
		},
		mixins: [mixins],
		data() {
			return {
				isOpen: false,
				openInfo: {},
				options: {
					messagesNum: 0 // 用户未读的消息数
				},
			}
		},
		computed: {
			classifyWidth() {
				let width = this.System.screenWidth - 100
				return `width: ${width}px;`
			},
			scrollItem() {
				let height = ((this.System.screenWidth - 14) / 2) * 0.8770949720670391
				return `height: ${height}px;`
			}
		},
		created() {
			uni.$on('square', data => {
				this.getList()
				this.getUnreadMessagesNum()
			})
			this.getList()
			this.getUnreadMessagesNum()
		},
		onUnload() {
			// 移除监听事件
			uni.$off('square')
		},
		methods: {
			// 查询用户未读的消息数
			async getUnreadMessagesNum () {
				this.options.messagesNum = 0
				const result = await this.$axios.get(this.API.getUnreadMessagesNum)
				if (result) {
					this.options.messagesNum = result
				}
			},
			// 关注
			async getList (type) {
				this.loading = true
				const result = await this.$axios.get(this.API.getFocusTeams, {
					params: {
						page: this.page
					}
				})
				this.loading = false
				this.triggered = false
				if (result && result.focusTeams && result.focusTeams.length) {
					const { focusTeams, end } = result
					if (type === 'push') {
						this.list = this.list.concat(focusTeams)
					} else {
						this.list = focusTeams || []
					}
					this.end = end
				} else {
					this.list = []
				}
			},
			// 跳转
			link (path) {
				uni.navigateTo({
					url: path
				})
			},
			// 交流房间跳转
			async toRoom (item) {
				this.loading = true
				const result = await this.$axios.get(this.API.judgeJoinRestrictions, {
					data: {
						rid: item.rid
					}
				})
				this.loading = false
				if (result) {
					// 是否可以进入房间，0否1是
					const { canJoin, room } = result
					if (canJoin === '1') {
						this.link(`/pages/campus/voiceRoom?teamId=${room.teamId}&rid=${room.rid}`)
					} else {
						this.link(`/pages/share/communication?rid=${room.rid}`)
					}
				}
			},
			// open 打开
			openChage (e) {
				this.openInfo = {
					right: -2,
					top: 40
				}
				setTimeout(() => {
					this.isOpen = !this.isOpen
				}, 200)
			},
			// 菜单回调
			menusCallback (e) {
				this.isOpen = false
			},
			// 自定义下拉刷新被触发
			onRefresh () {
				this.page = 1
				this.triggered = true
				this.getList()
			},
			// 自定义下拉刷新被复位
			onRestore () {
				this.triggered = false
			},
			// 滚动到底部
			scrolltolower (e) {
				if (this.end === '0') {
					this.page++
					this.getList('push')
				}
			}
		},
		// 分享
		onShareAppMessage (e) {
			return {
				title: `同学们都在这里抱团，等你加入！`, // 分享标题
				path: `/pages/team/square`, // 页面 path
				imageUrl: 'https://daifeiclub.com/image/pics/team_share.png'
			}
		}
	}
</script>

<style lang="scss" scoped>
	.team {
		min-height: calc(100vh);
		box-sizing: border-box;
		
		.title {
			display: flex;
			align-items: center;
			.name {
				color: $J49494;
				flex: 1;
			}
			.btns {
				display: flex;
				align-items: center;
				.btn {
					position: relative;
					min-height: 48rpx;
					padding: 0 20rpx;
					font-size: 36rpx;
					font-weight: 100;
					color: $D2D2D2;
					display: flex;
					align-items: center;
					justify-content: center;
					.value {
						background-color: $FF0000;
						border-radius: 50%;
						position: absolute;
						line-height: 26rpx;
						text-align: center;
						font-size: 20rpx;
						font-weight: 400;
						color: $white;
						height: 26rpx;
						width: 26rpx;
						right: 10rpx;
						top: -4rpx;
					}
					.iconfont {
						height: 40rpx;
						width: 40rpx;
					}
				}
			}
		}
		.body_scroll {
			.square_list {
				position: relative;
				.list {
					box-sizing: border-box;
					overflow: hidden;
					padding: 7px;
					.item {
						background: url('https://daifeiclub.com/image/pics/campus_item.png') no-repeat center center;
						background-size: 100% 100%;
						box-sizing: border-box;
						padding: 32rpx;
						float: left;
						width: 50%;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						position: relative;
						.line {
							position: absolute;
							top: 24rpx;
							right: 16rpx;
							left: 18rpx;
							z-index: 1;
							height: 30rpx;
						}
						.subtitle {
							background: url('https://daifeiclub.com/image/pics/identification.png') no-repeat center center;
							background-size: 100% 100%;
							position: absolute;
							top: 9rpx;
							right: 13rpx;
							z-index: 2;
							width: 176rpx;
							height: 36rpx;
							line-height: 36rpx;
							text-align: center;
							font-size: 20rpx;
							font-weight: 400;
							color: $E8E8E8;
						}
						.highlight {
							position: absolute;
							top: 14rpx;
							right: 20rpx;
							z-index: 3;
							width: 54rpx;
							height: 54rpx;
							color: $E8E8E8;
						}
						.core {
							position: relative;
							flex: 1;
							.number_of__people {
								position: absolute;
								text-align: center;
								line-height: 32rpx;
								font-size: 22rpx;
								font-weight: 400;
								color: $J49494;
								width: 136rpx;
								bottom: -62rpx;
								right: -10rpx;
								z-index: 4;
							}
							.name {
								padding: 18rpx 0 0 70rpx;
								line-height: 44rpx;
								min-height: 88rpx;
								font-size: 32rpx;
								font-weight: 400;
								color: $white;
							}
							.member {
								.user {
									border-radius: 50%;
									overflow: hidden;
									height: 32rpx;
									width: 32rpx;
									position: absolute;
									background-color: $ccc;
									-wekit-filter: blur(1px);
									filter: blur(1px);
									&::before {
										content: '';
										position: absolute;
										top: 0;
										right: 0;
										bottom: 0;
										left: 0;
										height: 32rpx;
										width: 32rpx;
										border-radius: 50%;
										overflow: hidden;
										background: url('https://daifeiclub.com/image/pics/member_mask.png') no-repeat center center;
										background-size: 100% 100%;
									}
									&.user_01 {
										top: -45rpx;
										left: 120rpx;
									}
									&.user_02 {
										top: -45rpx;
										left: 24%;
									}
									&.user_03 {
										top: -28rpx;
										left: 8%;
									}
									&.user_04 {
										top: 20rpx;
										left: 0;
									}
									&.user_05 {
										bottom: 2rpx;
										left: 0;
									}
									&.user_06 {
										bottom: -48rpx;
										left: 8%;
									}
									&.user_07 {
										bottom: -62rpx;
										left: 24%;
									}
									&.user_08 {
										bottom: -62rpx;
										left: 120rpx;
									}
								}
							}
						}
					}
				}
			}
			.no_data {
				height: 140rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				&.active {
					position: absolute;
					top: 50%;
					right: 0;
					left: 0;
					margin-top: -70rpx;
				}
				.name {
					line-height: 42rpx;
					font-size: 30rpx;
					font-weight: 400;
					color: $C2C2C2;
				}
				.more {
					margin: 40rpx auto 0 auto;
					line-height: 44rpx;
					text-align: center;
					font-size: 32rpx;
					font-weight: 600;
					color: $AA2929;
					width: 128rpx;
					&::after {
						border-bottom: 4rpx solid $AA2929;
						border-left: 4rpx solid $AA2929;
					}
				}
			}
		}
	}
</style>
