<template>
	<view class="page group-info">
		<view class="group-members">
			<view class="member-items">
				<view v-for="(member,idx) in  groupMembers" :key="idx">
					<view class="member-item" v-if="idx<9" @click="onShowUserInfo(member.userId)">
						<view class="avatar">
							<image class="head-image" :src="member.headImage" mode="aspectFill"> </image>
						</view>
						<view class="text">
							<text>{{member.aliasName}}</text>
						</view>
					</view>
				</view>
				<view class="invite-btn" @click="onInviteMember()">
					<view class="iconfont icon-add-circle"></view>
				</view>
			</view>
			<view class="member-more" @click="onShowMoreMmeber()">查看更多群成员 ></view>
		</view>
		<view class="group-detail">
			<uni-section title="群聊名称:" titleFontSize="30rpx">
				<template v-slot:right>
					<text>{{groupInfo.name}}</text>
				</template>
			</uni-section>
			<uni-section title="群主:" titleFontSize="30rpx">
				<template v-slot:right>
					<text>{{ownerName}}</text>
				</template>
			</uni-section>

			<uni-section title="群聊备注:" titleFontSize="30rpx">
				<template v-slot:right>
					<text> {{groupInfo.remark}}</text>
				</template>
			</uni-section>
			<uni-section title="我在本群的昵称:" titleFontSize="30rpx">
				<template v-slot:right>
					<text> {{groupInfo.aliasName}}</text>
				</template>
			</uni-section>
			<uni-section title="群公告:" titleFontSize="30rpx">
				<uni-notice-bar :text="groupInfo.notice" />
			</uni-section>
			<view class="group-edit" @click="onEditGroup()">修改群聊资料 > </view>
		</view>
		<view class="btn-group">
			<button class="btn" type="primary" @click="onSendMessage()">发消息</button>
			<button class="btn" v-show="!isOwner" type="warn" @click="onQuitGroup()">退出群聊</button>
			<button class="btn" v-show="isOwner" type="warn" @click="onDissolveGroup()">解散群聊</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupId: null,
				groupMembers: []
			}
		},
		methods: {
			onFocusSearch() {},
			onInviteMember() {
				uni.navigateTo({
					url: `/pages/group/group-invite?id=${this.groupId}`
				})
			},
			onShowUserInfo(userId) {
				uni.navigateTo({
					url: "/pages/common/user-info?id=" + userId
				})
			},
			onShowMoreMmeber() {
				uni.navigateTo({
					url: `/pages/group/group-member?id=${this.groupId}`
				})
			},
			onEditGroup() {
				uni.navigateTo({
					url: `/pages/group/group-edit?id=${this.groupId}`
				})
			},
			onSendMessage() {
				let chat = {
					type: 'GROUP',
					targetId: this.groupInfo.id,
					showName: this.groupInfo.remark,
					headImage: this.groupInfo.headImage,
				};
				this.$store.commit("openChat", chat);
				uni.navigateTo({
					url: "/pages/chat/chat-box?chatIdx=0"
				})
			},
			onQuitGroup() {
				uni.showModal({
					title: '确认退出?',
					content: `退出群聊后将不再接受群里的消息，确认退出吗?`,
					success: () => {
						if (res.cancel)
							return;
						this.$http({
							url: `/group/quit/${this.groupId}`,
							method: 'DELETE'
						}).then(() => {
							this.$store.commit("removeGroup", this.groupId);
							this.$store.commit("removeGroupChat", this.groupId);
							uni.showModal({
								title: `退出成功`,
								content: `您已退出群聊'${this.groupInfo.name}'`,
								showCancel: false,
								success: () => {
									uni.navigateBack();
								}
							})
						});
					}
				});
			},
			onDissolveGroup() {
				uni.showModal({
					title: '确认解散?',
					content: `确认要解散群聊'${this.groupInfo.name}'吗?`,
					success: (res) => {
						if (res.cancel)
							return;
						this.$http({
							url: `/group/delete/${this.groupId}`,
							method: 'delete'
						}).then(() => {
							this.$store.commit("removeGroup", this.groupId);
							this.$store.commit("removeGroupChat", this.groupId);
							uni.showModal({
								title: `解散成功`,
								content: `群聊'${this.groupInfo.name}'已解散`,
								showCancel: false,
								success: () => {
									uni.navigateBack();
								}
							})
						});
					}
				});

			},
			loadGroupInfo(id) {
				this.$http({
					url: `/group/find/${id}`,
					method: 'GET'
				}).then((group) => {
					// 更新聊天页面的群聊信息
					this.$store.commit("updateChatFromGroup", group);
					// 更新聊天列表的群聊信息
					this.$store.commit("updateGroup", group);

				});
			},
			loadGroupMembers(id) {
				this.$http({
					url: `/group/members/${id}`,
					method: "GET"
				}).then((members) => {
					this.groupMembers = members.filter(m => !m.quit);
				})
			}
		},
		computed: {
			groupInfo() {
				let groups = this.$store.state.groupStore.groups;
				return groups.find(g => g.id == this.groupId)
			},
			ownerName() {
				let member = this.groupMembers.find((m) => m.userId == this.groupInfo.ownerId);
				return member && member.aliasName;
			},
			isOwner() {
				return this.groupInfo.ownerId == this.$store.state.userStore.userInfo.id;
			}
		},
		onLoad(options) {
			this.groupId = options.id;
			// 查询群聊信息
			this.loadGroupInfo(options.id);
			// 查询群聊成员
			this.loadGroupMembers(options.id)
		}
		
	}
</script>

<style lang="scss" scoped>
	.group-info {
		.group-members {
			padding: 30rpx;
			background: white;

			.member-items {
				display: flex;
				flex-wrap: wrap;

				.member-item {
					width: 120rpx;
					display: flex;
					flex-direction: column;
					margin: 8rpx 2rpx;
					position: relative;

					align-items: center;
					padding-right: 5px;
					background-color: #fafafa;
					white-space: nowrap;

					.avatar {
						display: flex;
						justify-content: center;
						align-items: center;
						width: 100rpx;
						height: 100rpx;

						.head-image {
							width: 100%;
							height: 100%;
							border-radius: 10%;
							border: #d8d8d8 solid 1px;
						}
					}

					.text {
						width: 100%;
						flex: 1;
						overflow: hidden;
						text-align: center;
						white-space: nowrap;
					}
				}

				.invite-btn {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 100rpx;
					height: 100rpx;
					margin: 10rpx;
					border: #686868 dashed 2px;
					border-radius: 10%;

					.iconfont {
						font-size: 70rpx;
						color: #888888;
					}
				}
			}

			.member-more {
				padding: 20rpx;
				text-align: center;
				font-size: 30rpx;
			}
		}


		.group-detail {
			margin-top: 30rpx;
			padding: 30rpx;
			background: white;


			.group-edit {
				padding: 20rpx;
				text-align: center;
				font-size: 30rpx;
			}
		}


		.btn-group {
			margin-top: 30rpx;
			padding: 30rpx;
			background: white;


			.btn {
				margin: 10rpx;
			}
		}


	}
</style>