<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.name" @blur="toInput" placeholder="搜索商品名称">
			</view>
			<view class="two">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
					<view class="list-scroll-view">
						<view class="list" v-for="(item, index) in list" :key="index" @click="toShare(item)">
							<view class="image">
								<image class="file" v-for="(tag, indexs) in item.persons.slice(0,7)" :key="indexs"
									v-if="tag.status==0" :src="tag.icon&&tag.icon.length>0?tag.icon[0].url:''" mode="">
								</image>
							</view>
							<view class="list_1">
								<view class="name">
									{{item.person_limit||0}}人即可开团成功
								</view>
								<view class="some">
									<text>团长</text>
									<text>{{item.leader.name||'暂无'}}</text>
								</view>
								<view class="some">
									<text>参团人数</text>
									<text>{{item.persons.length||0}}人</text>
								</view>
								<view class="some">
									<text>开团时间</text>
									<text>{{item.time||'暂无'}}</text>
								</view>
							</view>
							<view class="other">
								<view v-if="item.status=='0'" class="btn">
									<text>{{item.zhStatus||'暂无状态'}}</text>
								</view>
								<view v-else-if="item.status=='-1'" class="button">
									{{item.zhStatus||'暂无状态'}}
								</view>
								<view v-else-if="item.status=='2'" class="button">
									已退团
								</view>
								<view v-else class="button">
									{{item.zhStatus||'暂无状态'}}
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import moment from 'moment'
	export default {
		data() {
			return {
				user: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				statusList: [],
			};
		},
		onShow: function() {
			const that = this;
			that.watchLogin()
		},
		methods: {
			// 分享
			toShare(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesHome/group/share?id=${e._id}`
				})
			},
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						let status = await that.$api(`/dictData`, 'GET', {
							code: "group_status"
						});
						if (status.errcode == '0') {
							that.$set(that, `statusList`, status.data)
						}
						that.search()
					},
					fail: function(err) {
						uni.reLaunch({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				if (user._id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
						persons: user._id
					}
					let res = await that.$api(`/group`, 'GET', {
						...info,
						...that.searchInfo
					})
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						for (let val of list) {
							let personsstatus = val.persons.find(i => i.customer == user._id)
							if (personsstatus.status == 1) {
								val.status = '2';
							}
							val.persons = val.persons.filter(i => i.status == '0')
							let status = that.statusList.find(i => i.value == val.status)
							if (status) val.zhStatus = status.label;
							val.time = moment(val.meta.createdAt).format('YYYY-MM-DD HH:mm:ss')
						}
						that.$set(that, `list`, list);
						that.$set(that, `total`, res.total)
					}
				}
			},
			// 分页
			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 uni.showToast({
					title: '没有更多数据了'
				});
			},
			// 清空列表
			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">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			border-bottom: 1px solid var(--f85Color);
			padding: 2vw;

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

		.two {
			position: relative;
			flex-grow: 1;
			padding: 2vw 0 0 0;

			.list {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				width: 91vw;
				border: 1px solid var(--f1Color);
				margin: 2vw 2vw 0 2vw;
				padding: 0 2vw;
				border-radius: 5px;

				.image {
					display: flex;
					flex-wrap: wrap;
					justify-content: center;
					align-items: center;
					width: 27vw;
					height: 27vw;
					border-radius: 5px;
					margin: 0 2vw 0 0;

					.file {
						width: 9vw;
						height: 9vw;
					}
				}

				.list_1 {
					display: flex;
					flex-direction: column;
					flex-grow: 1;
					padding: 2vw 0;
					width: 35vw;

					.name {
						font-size: var(--font16Size);
						margin: 0 0 1vw 0;
					}

					.some {
						color: var(--f85Color);
						font-size: var(--font14Size);
						margin: 0 0 1vw 0;

						text:last-child {
							margin: 0 0 0 1vw;
							color: var(--f00Color);
						}
					}
				}

				.other {
					.btn {
						display: flex;
						flex-direction: column;
						align-items: center;
						margin: 0 2vw;
						padding: 2vw 3vw;
						background-color: var(--ff0Color);
						color: var(--fffColor);
						border-radius: 2vw;
						font-size: var(--font14Size);

						text {
							font-size: var(--font12Size);
						}
					}

					.button {
						margin: 0 2vw;
						padding: 2vw 3vw;
						background-color: var(--fcColor);
						color: var(--f00Color);
						border-radius: 2vw;
						font-size: var(--font14Size);
					}
				}
			}
		}
	}
</style>
