<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.name" @blur="toInput" placeholder="搜索商品名称">
			</view>
			<view class="two">
				<tabs :tabs="tabs" @tabsChange="tabsChange">
					<view class="tabsList">
						<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,9)" :key="indexs" :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>
				</tabs>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	import tabs from '@/components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				statusList: [],
				tabs: {
					active: '0',
					menu: [{
							title: '开团中',
							active: '0'
						},
						{
							title: '已散团',
							active: '-1'
						},
						// {
						// 	title: '已退团',
						// 	active: '2'
						// },
						{
							title: '已结束',
							active: '1'
						}
					]
				},
				status: '0',
			};
		},
		onShow: function() {
			const that = this;
			that.watchLogin();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 监听用户是否登录
			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.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				if (user._id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
						person: user._id,
						status: that.status,
					}
					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';
							}
							if (that.status != '-1') {
								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 {}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (e.detail.value) that.$set(that.searchInfo, `name`, e.detail.value);
				that.clearPage();
				that.search();
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active);
				that.clearPage();
				that.search()
			},
			// 分享
			toShare(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesHome/group/share?id=${e._id}`
				})
			},

			// 清空列表
			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 {
			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;

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.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);
						}
					}
				}
			}
		}
	}

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
