<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="list" @tap="toView">
					<image class="image" :src="goodsInfo.file&&goodsInfo.file.length>0?goodsInfo.file[0].url:''" mode="">
					</image>
					<view class="list_1_1">
						<view class="name">
							{{goodsInfo.name||'暂无'}}
						</view>
						<view class="specs">
							<text>规格:{{specsInfo.name}}</text>
							<text>店铺:{{shopInfo.name}}</text>
						</view>
						<view class="money">
							原价￥<text>{{specsInfo.sell_money}}</text>团购价￥<text>{{specsInfo.group_config.money}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="two">
				<button type="default" size="default" @click="toGroup">我要开团</button>
			</view>
			<view class="thr">
				<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">
							<view class="image">
								<image class="file" v-for="(tag, indexs) in item.persons.slice(0,9)" :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 class="btn" @click="onJoin(item)">
									<text>{{item.zhStatus||'暂无状态'}}</text>立即参团
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="bottom">
			<view class="content">
				<view class="one">
					<text>数量</text>
					<view class="count">
						<uni-number-box :min="1" :max="specsInfo.num" v-model="num" @change="toCount">
						</uni-number-box>
					</view>
					<text>库存{{specsInfo.num||0}}</text>
				</view>
				<view class="btn">
					<text @click="onSubmit" class="button">确认开团</text>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup1" background-color="#fff" type="bottom">
			<view class="content">
				<view class="one">
					<text>数量</text>
					<view class="count">
						<uni-number-box :min="1" :max="specsInfo.num" v-model="num" @change="toCount">
						</uni-number-box>
					</view>
					<text>库存{{specsInfo.num||0}}</text>
				</view>
				<view class="btn">
					<text @click="onSubmit(join)" class="button">确认参团</text>
				</view>
			</view>
		</uni-popup>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				query: {},
				user: {},
				// 商品详情
				goodsInfo: {},
				// 规格详情
				specsInfo: {},
				// 店铺详情
				shopInfo: {},
				// 参团列表
				list: [],
				statusList: [],
				// 参团数据
				join: '',
				num: 1,
				total: 0,
				skip: 0,
				limit: 10,
				page: 0
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `query`, {
				goods_id: e.goods_id || '',
				specs_id: e.specs_id || ''
			});
			await that.watchLogin();
			await that.search();
		},
		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)
						}
					},
					fail: function(err) {}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let res;
				res = await that.$api(`/viewGoods/goodsDetail`, `POST`, {
					id: that.query.goods_id
				});
				if (res.errcode == '0') {
					that.$set(that, `goodsInfo`, res.data.goods)
					that.$set(that, `shopInfo`, res.data.shop)
				}
				res = await that.$api(`/goodsSpec/${that.query.specs_id}`, 'GET')
				if (res.errcode == '0') {
					that.$set(that, `specsInfo`, res.data);
				}
				let info = {
					skip: that.skip,
					limit: that.limit,
					goodsSpec: that.query.specs_id,
					status: '0'
				}
				res = await that.$api(`/group`, 'GET', {
					...info
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						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 {}
			},
			// 开团
			async toGroup() {
				const that = this;
				that.$refs.popup.open();
			},
			// 参团
			async onJoin(e) {
				const that = this;
				that.$set(that, `join`, e)
				that.$refs.popup1.open();
			},
			// 计数器
			toCount(e) {
				const that = this;
				that.num = e;
			},
			// 确认开团 立即参团
			async onSubmit(e) {
				const that = this;
				let user = that.user;
				if (user._id) {
					let data = {
						customer: user._id,
						shop: that.shopInfo._id,
						goods: that.goodsInfo._id,
						goodsSpec: that.specsInfo._id,
						num: that.num,
						type: '1'
					}
					if (e._id) {
						data.group = e._id
					}
					let arr = await that.$api(`/util/checkCanBuy`, 'POST', data)
					if (arr.errcode == '0') {
						if (arr.data.result == true) {
							that.clearPage();
							if (e._id) {
								uni.navigateTo({
									url: `/pagesHome/order/order?key=${arr.data.key}&group_id=${e._id}`
								})
							} else {
								uni.navigateTo({
									url: `/pagesHome/order/order?key=${arr.data.key}`
								})
							}
						} else {
							uni.showToast({
								title: arr.data.msg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none'
						})
					}
				} else {
					uni.navigateTo({
						url: `/pages/login/index`
					})
				}
			},
			// 清空列表
			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 {

			.list {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				width: 91vw;
				border: 0.5vw dashed var(--ff0Color);
				margin: 2vw 2vw 0 2vw;
				padding: 2vw;
				border-radius: 5px;

				.image {
					width: 25vw;
					height: 25vw;
					border-radius: 5px;
					margin: 0 2vw 0 0;
				}

				.list_1_1 {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					flex-grow: 1;
					padding: 2vw 0;

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

					.specs {
						display: flex;
						justify-content: space-between;
						color: var(--f85Color);
						font-size: var(--font12Size);
					}

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

						text {
							font-size: var(--font16Size);
							font-weight: bold;
							margin: 0 1vw 0 0;
						}
					}
				}
			}
		}

		.two {
			margin: 4vw 0 0 0;

			button {
				margin: 0 2vw 2vw 2vw;
				background-color: #FFA500;
				color: var(--fffColor);
			}

		}

		.thr {
			position: relative;
			flex-grow: 1;

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

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

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

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

	.content {
		height: 60vw;

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

			text {
				margin: 0 2vw 0 0;
			}

			text:last-child {
				margin: 0 0 0 2vw;
				font-size: var(--font12Size);
				color: var(--f85Color);
			}
		}

		.btn {
			display: flex;
			justify-content: space-between;
			position: fixed;
			bottom: 0;

			.button {
				width: 100vw;
				padding: 4vw 0;
				background-color: var(--fFB1Color);
				text-align: center;
				font-size: var(--font18Szie);
				color: var(--mainColor);
			}
		}
	}
</style>
