<template>
	<view class="campus black_style">
		<Nav className="black_nav" :scroll="scroll">
			<block slot="content" class="title">
				<view class="ico">
					<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/me_userInfo_logo.png"></image>
				</view>
				<view class="name omit">{{ $t('home.title') }}</view>
			</block>
		</Nav>
		<view class="body_scroll" :style="getHeight">
			<view class="th">
				<view class="classify" :style="classifyWidth">
					<!-- :class="{noBefore: classifyAngle === 'left', noAfter: classifyAngle === 'right'}" -->
					<scroll-view
						:scroll-x="true"
						class="classify_scroll__view no_scroll noBefore noAfter"
						@scrolltoupper="classifyScrolltoupper"
						@scrolltolower="classifyScrolltolower">
						<view class="item transition" v-for="(item, key) in classifyList" :key="key" :class="{active: key === classifyIndex}" @click="classifyChange(item, key)">{{ item.displaySchoolName | across }}</view>
					</scroll-view>
				</view>
				<view class="btns">
					<view class="btn" @click="link('/pages/index/news')">
						<view class="iconfont">
							<i class="icomoon icon-news"></i>
						</view>
						<text class="value" v-if="options.messagesNum > 0">{{ options.messagesNum > 9 ? `9+` : options.messagesNum }}</text>
					</view>
					<view class="btn" @click="openChage">
						<view class="iconfont">
							<i class="icomoon icon-add"></i>
						</view>
					</view>
				</view>
			</view>
			<scroll-view
				:style="scrollHeight"
				:scroll-y="true"
				:refresher-enabled="true"
				:refresher-triggered="triggered"
				@refresherrefresh="onRefresh"
				@refresherrestore="onRestore"
				@scrolltolower="scrolltolower"
				class="body_scroll_view">
				<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"></image>
						</view>
						<view class="subtitle" v-if="item.team">{{ item.team.teamName }}</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>
				<!-- noData -->
				<view class="no_data">
					<view v-if="!classifyList.length">
						<view class="name">在这里，和校友一起分享互助</view>
						<view class="more arrow" @click="link('/pages/campus/add')">添加校园</view>
					</view>
					<view v-else>
						<view class="name">没有喜欢的交流？不如发起一场</view>
						<view class="more arrow" @click="link('/pages/index/communication')">发起交流</view>
					</view>
				</view>
				<!-- 最近活跃小队 -->
				<template v-if="teamList && teamList.length">
					<view class="brisk_title">
						<view class="title">最近活跃小队</view>
					</view>
					<view class="list">
						<view class="item" :style="scrollItem" v-for="(item, key) in teamList" :key="key" @click="link(`/pages/share/team?id=${item.teamId}&rid=${item.rid}&type=brisk`)">
							<view class="line">
								<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/square_room_line.gif"></image>
							</view>
							<view class="subtitle" v-if="item.teamName">{{ item.teamName }}</view>
							<view class="highlight">
								<image class="pic" mode="aspectFill" src="https://daifeiclub.com/image/pics/highlight.png"></image>
							</view>
							<view class="core">
								<view class="name lineClamp2">{{ item.topic | across }}</view>
								<view class="member" v-if="item.users && item.users.length">
									<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>
				</template>
				<view :style="'height: ' + (60 + bottom) + 'px;'">&nbsp;</view>
			</scroll-view>
		</view>
		<!-- menus -->
		<meuns v-if="isOpen" :visible="isOpen" :params="openInfo" @callback="menusCallback" />
		<!-- loading -->
		<loading :visible="loading" />
	</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],
			campusRandom: [Number, String]
		},
		mixins: [mixins],
		data() {
			return {
				isOpen: false,
				openInfo: {},
				classifyIndex: 0,
				classifyAngle: 'left',
				classifyList: [],
				schoolCode: '',
				options: {
					messagesNum: 0 // 用户未读的消息数
				},
				teamList: [] // 最近活跃的小队
			}
		},
		watch: {
			active() {
				const index = uni.getStorageSync('index')
				if (index === 0) {
					this.getUnreadMessagesNum()
					this.getClassifyList()
				}
			},
			scroll(value) {

			},
			campusRandom() {
				this.getUnreadMessagesNum()
				this.getClassifyList()
			}
		},
		computed: {
			classifyWidth() {
				let width = this.System.screenWidth - 100
				return `width: ${width}px;`
			},
			scrollHeight() {
				let height = this.CustomBar + 50
				return `height: calc(100vh - ${height}px);`
			},
			scrollItem() {
				let height = ((this.System.screenWidth - 14) / 2) * 0.8770949720670391
				return `height: ${height}px;`
			}
		},
		created() {
			this.bottom = this.System.safeAreaInsets.bottom
			this.getUnreadMessagesNum()
			this.getClassifyList()
		},
		methods: {
			// 获取校园
			async getClassifyList () {
				const result = await this.$axios.get(this.API.getSchoolUser, {
					params: {
						userUid: uni.getStorageSync('uid')
					}
				})
				this.triggered = false
				if (result && result.length) {
					this.classifyList = result
					this.classifyIndex = 0
					this.schoolCode = this.classifyList[this.classifyIndex].schoolCode
					this.getList()
					this.getTeamList()
				} else {
					this.classifyList = []
				}
			},
			// 查询用户未读的消息数
			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.getSchoolRooms, {
					params: {
						schoolCode: this.schoolCode,
						page: this.page
					}
				})
				this.loading = false
				this.triggered = false
				if (result && result.schoolRooms && result.schoolRooms.length) {
					const { schoolRooms, end } = result
					if (type === 'push') {
						this.list = this.list.concat(schoolRooms)
					} else {
						this.list = schoolRooms || []
					}
					this.end = end
				} else {
					this.list = []
				}
			},
			// 获取列表
			async getTeamList() {
				this.loading = true
				const result = await this.$axios.get(this.API.getRecentlyActiveTeams, {
					params: {
						schoolCode: this.schoolCode
					}
				})
				this.loading = false
				if (result && result.length) {
					this.teamList = result
				} else {
					this.teamList = []
				}
			},
			// 跳转到房间
			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}`)
					}
				}
			},
			// 跳转
			link (path) {
				this.teamVisible = false
				uni.navigateTo({
					url: path
				})
			},
			// 分类 切换
			classifyChange (item, key) {
				this.classifyIndex = key
				this.schoolCode = item.schoolCode
				this.getList()
				this.getTeamList()
			},
			// 分类 滚动到左边
			classifyScrolltoupper () {
				this.classifyAngle = 'left'
			},
			// 分类 滚动到右边
			classifyScrolltolower (e) {
				this.classifyAngle = 'right'
			},
			// open 打开
			openChage (e) {
				this.openInfo = {
					right: 2,
					top: this.CustomBar + 50
				}
				setTimeout(() => {
					this.isOpen = !this.isOpen
				}, 200)
			},
			// 菜单回调
			menusCallback (e) {
				this.isOpen = false
			},
			// 自定义下拉刷新被触发
			onRefresh () {
				this.page = 1
				this.triggered = true
				this.getList()
				this.getTeamList()
			},
			// 自定义下拉刷新被复位
			onRestore () {
				this.triggered = false
			},
			// 滚动到底部
			scrolltolower (e) {
				if (this.end === '0') {
					this.page++
					this.getList('push')
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.campus {
		.title {
			display: flex;
			align-items: center;
			.ico {
				height: 36rpx;
				width: 36rpx;
			}
			.name {
				padding-left: 20rpx;
				color: $J49494;
			}
		}
		.body_scroll {
			.th {
				display: flex;
				align-items: center;
				line-height: 50px;
				position: relative;
				z-index: 10;
				.classify {
					box-sizing: border-box;
					padding-right: 20rpx;
					white-space: nowrap;
					flex: 1;
					.classify_scroll__view {
						position: relative;
						&.noBefore {
							&::before {
								content: none;
							}
						}
						&.noAfter {
							&::after {
								content: none;
							}
						}
						&::before {
							content: '';
							position: absolute;
							top: 0;
							left: 0;
							bottom: 0;
							z-index: 1;
							width: 100rpx;
							background-image: linear-gradient(to right, $E32121, transparent);
						}
						&::after {
							content: '';
							position: absolute;
							top: 0;
							right: 0;
							bottom: 0;
							z-index: 1;
							width: 100rpx;
							background-image: linear-gradient(to left, $E32121, transparent);
						}
						.item {
							display: inline-block;
							letter-spacing: 2rpx;
							position: relative;
							line-height: 48rpx;
							padding: 0 30rpx;
							font-size: 34rpx;
							font-weight: 600;
							color: $W45454;
							&.active {
								color: $AA2929;
								top: -6rpx;
								&::before {
									background-color: $AA2929;
								}
							}
							&::before {
								content: '';
								position: absolute;
								bottom: -6rpx;
								left: 50%;
								width: 60rpx;
								height: 10rpx;
								border-radius: 10px;
								margin-left: -30rpx;
								background-color: transparent;
								-webkit-transition: all .2s ease;
								-moz-transition: all .2s ease;
								-ms-transition: all .2s ease;
								-o-transition: all .2s ease;
								transition: all .2s ease;
							}
						}
					}
				}
				.btns {
					min-width: 100px;
					width: 100px;
					display: flex;
					align-items: center;
					.btn {
						color: rgba($color: $D2D2D2, $alpha: 0.8);
						height: 50px;
						width: 50px;
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						font-size: 36rpx;
						.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: 20%;
							top: 20%;
						}
						.iconfont {
							height: 40rpx;
							width: 40rpx;
						}
					}
				}
			}
			.body_scroll_view {
				box-sizing: border-box;
				align-items: center;
				position: relative;
				display: flex;
				.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: 42%;
									}
									&.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: -65rpx;
										left: 24%;
									}
									&.user_08 {
										bottom: -62rpx;
										left: 120rpx;
									}
								}
							}
						}
					}
				}
				.no_data {
					height: 360rpx;
					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;
						}
					}
				}
				.brisk_title {
					padding: 20rpx 52rpx 20rpx 26rpx;
					position: relative;
					align-items: center;
					line-height: 48rpx;
					height: 48rpx;
					display: flex;
					.title {
						font-size: 34rpx;
						font-weight: 600;
						color: $J49494;
						flex: 1;
					}
				}
			}
		}
	}
</style>
