<template>
	<view class="main">
		<view class="one">
			<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索姓名">
		</view>
		<view class="two">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index" @tap="toChat(item)">
						<view class="list_1">
							<view class="left">
								<image class="image"
									:src="item.icon&&item.icon.length>0?item.icon[0].url:config.user_url[0].url">
								</image>
							</view>
							<view class="right">
								<view class="name textOne">{{item.name||'暂无'}}</view>
							</view>
						</view>
						<view class="bottom">
							<button class="warning" size="mini" type="warn" @tap.stop="toView(item)">用户详情</button>
							<button class="danger" size="mini" type="warn" @tap.stop="toExit(item)">退出群聊</button>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				config: {},
				user: {},
				groupInfo: {},
				patientsList: [],
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchToken();
			that.searchConfig();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			async search() {
				const that = this;
				if (that.id) {
					// 群详情
					const arr = await that.$api(`/group/${that.id}`, 'GET', {})
					if (arr.errcode == '0') {
						that.$set(that, `groupInfo`, arr.data)
						that.$set(that, `patientsList`, arr.data.patients)
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none'
						});
					}
					// 群成员
					let info = {
						skip: that.skip,
						limit: that.limit,
						groupId: that.id
					}
					const res = await that.$api(`/group/getPaitentList`, 'GET', {
						...info,
						...that.searchInfo
					})
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						that.$set(that, `list`, list)
						that.$set(that, `total`, res.total)
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 聊天
			toChat(item) {
				const that = this;
				if (that.user.role == 'Doctor') {
					uni.navigateTo({
						url: `/pagesHome/friend/index?patientId=${item._id}&doctorId=${that.user._id}&groupId=${that.id}&title=${item.name}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesHome/friend/index?patientId=${item._id}&doctorId=${that.user.doctor}&groupId=${that.id}&title=${item.name}`
					})
				}
			},
			// 查看用户详情
			toView(item) {
				uni.navigateTo({
					url: `/pagesMy/person/detail?id=${item.id||item._id}`
				})
			},
			// 退出群里
			toExit(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: `您确定该${item.name}用户退出群聊吗?`,
					success: async function(res) {
						if (res.confirm) {
							that.groupInfo.patients = taht.patientsList.filter(i => i != item._id)
							const res = await that.$api(`/group/${item._id||item.id}`, 'POST', that.groupInfo)
							if (res.errcode == 0) {
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;

			input {
				width: 100%;
				padding: 2vw;
				background-color: var(--f1Color);
				font-size: var(--font14Size);
				border-radius: 5px;
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

			.list {
				background-color: var(--mainColor);
				border: 1px solid var(--f5Color);
				padding: 2vw;
				margin: 2vw 2vw 0 2vw;
				border-radius: 5px;

				.list_1 {
					display: flex;

					.left {
						width: 15vw;

						.image {
							width: 15vw;
							height: 15vw;
							border-radius: 1vw;
						}
					}

					.right {
						width: 75vw;
						margin: 0 0 0 2vw;

						.name {
							font-size: var(--font14Size);
						}

						.other {
							padding: 2px 0 0 0;
							font-size: var(--font12Size);
							color: var(--f85Color);
						}
					}
				}

				.bottom {
					margin: 2vw 0 0 0;
					text-align: center;

					.warning {
						background: var(--f3CColor);
					}

					.danger {
						background: var(--fF0Color);
					}

					button {
						margin: 0 1vw 0 0;
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>