<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索群组名称">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toAdd">添加</button>
			</view>
		</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="toInfo(item)">
						<view class="name">{{item.name||'暂无群组名称'}}</view>
						<view class="other textOver">
							简介：{{item.content||'暂无'}}
						</view>
						<view class="button">
							<button class="warning" size="mini" type="warn" @tap.stop="toEdit(item)">修改</button>
							<button class="warning" size="mini" type="warn" @tap.stop="toCode(item)">二维码</button>
							<button class="warning" size="mini" type="warn" @tap.stop="toPerson(item)">成员</button>
							<button class="danger" size="mini" type="warn" @tap.stop="toDel(item)">删除</button>
						</view>
					</view>
				</view>
				<view class="is_bottom" v-if="is_bottom">
					<text>{{config.bottom_title||'到底了！'}}</text>
				</view>
			</scroll-view>
		</view>
		<!-- 修改/添加信息 -->
		<uni-popup ref="popup" background-color="#fff" type="center" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>信息管理</text>
					<uni-icons @tap="toClose" type="close" size="20"></uni-icons>
				</view>
				<view class="info_1">
					<uni-forms ref="valiForm" :rules="rules" :modelValue="form">
						<uni-forms-item label="群名" required name="name">
							<uni-easyinput v-model="form.name" placeholder="请输入群名" />
						</uni-forms-item>
						<uni-forms-item label="简介" required name="content">
							<uni-easyinput type="textarea" autoHeight v-model="form.content" placeholder="请输入简介" />
						</uni-forms-item>
					</uni-forms>
					<button class="button" size="mini" type="primary" @click="submit('valiForm')">保存</button>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="code" background-color="#fff" type="center" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>二维码</text>
					<uni-icons @tap="toClose" type="close" size="20"></uni-icons>
				</view>
				<view class="info_1">
					二维码
					<button class="button" size="mini" type="primary" @click="submit('valiForm')">保存二维码</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				config: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				form: {},
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '群名不能为空'
						}]
					},
					content: {
						rules: [{
							required: true,
							errorMessage: '简介不能为空'
						}]
					}
				},
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 禁止滚动穿透
				show: false
			}
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.searchToken();
			await that.searchConfig();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			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;
				let user = that.user;
				let info = {
					skip: that.skip,
					limit: that.limit,
				}
				if (that.user.role == 'Doctor') info.doctor = that.user._id
				else info.doctor = that.user.doctor
				const res = await that.$api(`/group`, '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,
					});
				}
			},
			// 输入框
			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();
			},
			// 添加
			toAdd() {
				const that = this;
				that.$refs.popup.open()
			},
			// 二维码
			toCode(item) {
				const that = this;
				console.log(item);
				that.$refs.code.open()
			},
			// 成员
			toPerson(item) {
				const that = this;
				uni.navigateTo({
					url: `/pagesMy/person/index?id=${item.id||item._id}`
				})
			},
			// 修改
			async toEdit(item) {
				const that = this;
				const res = await that.$api(`/group/${item._id}`, 'GET', {})
				if (res.errcode == '0') {
					that.$set(that, `form`, res.data)
					that.$refs.popup.open()
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					});
				}
			},
			// 保存
			submit(ref) {
				const that = this;
				const form = that.form;
				that.$refs[ref].validate().then(async data => {
					let res;
					if (that.user.role == 'Doctor') data.doctor = that.user._id
					else data.doctor = that.user.doctor
					if (form._id) res = await that.$api(`/group/${form._id}`, 'POST', data)
					else res = await that.$api(`/group`, 'POST', data)
					if (res.errcode == '0') {
						that.toClose();
						that.clearPage();
						that.search();
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				})
			},
			// 删除
			async toDel(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '您确定删除该数据吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/group/${item._id||item.id}`, 'DELETE', {})
							if (res.errcode == 0) {
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$set(that, `form`, {})
				that.$refs.popup.close();
				that.$refs.code.close();
			},
			// 详情
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesHome/group/index?id=${item.id||item._id}&title=${item.name}`
				})
			},
			// 分页
			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`, 10)
				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;

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

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

			.one_2 {
				padding: 2vw 0;

				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
				}
			}
		}

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

			.list:first-child {
				margin: 2vw;
			}

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

				.name {
					font-size: var(--font14Size);
					font-weight: bold;
					padding: 2vw 0;
				}

				.other {
					font-size: var(--font12Size);
					color: var(--f85Color);
				}


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

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

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

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

		.uni-popup {
			z-index: 9999 !important;
		}

		.popup {
			display: flex;
			flex-direction: column;
			width: 90vw;
			height: 45vh;
			background-color: var(--f9Color);

			.close {
				display: flex;
				justify-content: space-between;
				padding: 2vw;

				text:first-child {
					font-size: var(--font16Size);
					font-weight: bold;
				}
			}

			.info_1 {
				position: relative;
				display: flex;
				flex-direction: column;
				max-height: 40vh;
				padding: 2vw;

				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
					font-size: var(--font14Size);
				}
			}
		}
	}

	.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>