<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<uni-forms ref="form" v-model="form" :rules="rules" label-width="auto">
					<uni-forms-item label="人数限制" name="person_limit">
						<uni-easyinput type="number" v-model="form.person_limit" placeholder="请输入人数限制" />
					</uni-forms-item>
					<uni-forms-item label="开始时间" name="start_time">
						<uni-datetime-picker type="datetime" v-model="form.start_time" @change="startChange" />
					</uni-forms-item>
					<uni-forms-item label="结束时间" name="end_time">
						<uni-datetime-picker type="datetime" v-model="form.end_time" @change="endChange" />
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" size="mini" @click="onSubmit('form')">确认开团</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="two_1" v-if="list.length>0">{{list[0].goods&&list[0].goods.name}}</view>
						<view class="list" v-for="(item,index) in list" :key="index">
							<view class="list_1">
								<view class="list_1_1">
									<view class="goods">
										<view class="goods_1">
											<image class="image"
												v-if="item.spec&&item.spec.file&&item.spec.file.length>0"
												:src="item.spec.file&&item.spec.file.length>0?item.spec.file[0].url:''"
												mode=""></image>
											<image class="image" v-else
												:src="item.goods&&item.goods.file&&item.goods.file.length>0?item.goods.file[0].url:''"
												mode=""></image>
										</view>
										<view class="goods_2">
											<view class="goodsname textOver">
												{{item.spec&&item.spec.name||'暂无'}}
											</view>
											<view class="other_2">
												会员提成金额：<text>￥{{item.leader_get||'暂无'}}</text>
											</view>
											<view class="other_2">
												团购价：<text>￥{{item.price||'暂无'}}</text>
											</view>
											<view class="other_2">
												会员价：<text>￥{{item.leader_price||'暂无'}}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				id: '',
				form: {},
				list: [],
				rules: {
					person_limit: {
						rules: [{
							required: true,
							errorMessage: '请输入人数限制',
						}]
					},
					start_time: {
						rules: [{
							required: true,
							errorMessage: '请选择开始时间'
						}]
					},
					end_time: {
						rules: [{
							required: true,
							errorMessage: '请选择结束时间'
						}]
					}
				},
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `form`, e || {});
		},
		onShow: function(e) {
			const that = this;
			that.watchLogin();
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async function(res) {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `user`, user);
							let res = await that.$api(`/goodsConfig`, `GET`, that.form, 'group');
							if (res.errcode == '0') {
								that.$set(that.form, `group_config`, res.data);
								that.$set(that, `list`, res.data);
							}
						}
					},
					fail: function(err) {
						uni.navigateTo({
							url: '/pages/login/index'
						})
					}
				})
			},
			// 开始时间选择
			startChange(e) {
				const that = this;
				that.$set(that.form, `start_time`, e);
			},
			// 结束时间选择
			endChange(e) {
				const that = this;
				that.$set(that.form, `end_time`, e);
			},
			// 提交保存
			onSubmit(ref) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定是否开团吗?',
					success: async function(res) {
						that.$set(that.form, `leader`, that.user.id || '');
						that.$set(that.form, `status`, '0');
						that.$refs[ref].validate().then(async params => {
							// 创建团数据
							let res = await that.$api(`/group`, 'POST', that.form, 'group');
							if (res.errcode == '0') {
								uni.reLaunch({
									url: `/pagesHome/group/share?id=${res.data.goods}&group=${res.data._id}`
								})
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						})
					}
				});
			},
		}
	}
</script>

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

		.one {
			padding: 2vw;

			.toLoacl {
				margin: 1vw 0 0 0;
			}

			.picker {
				border: 1px solid #3333;
				border-radius: 5px;
				padding: 2vw;
			}

			.btn {
				text-align: center;

				button {
					width: 30%;
					font-size: 14px;
					background-color: var(--f35BColor);
				}
			}
		}

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

			.two_1 {
				background-color: #fff;
				padding: 2vw;
				margin: 2vw;
				border-radius: 5px;
			}

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

				.list_1 {
					.list_1_1 {
						border-bottom: 1px solid #f1f1f1;

						.goods {
							display: flex;
							padding: 0 0 2vw 0;

							.goods_1 {
								width: 20vw;
								height: 20vw;

								.image {
									width: 100%;
									height: 100%;
									border-radius: 5px;
								}
							}

							.goods_2 {
								width: 70vw;
								padding: 0 0 0 2vw;

								.goodsname {
									font-size: 16px;
									margin: 0 0 1vw 0;
								}

								.time {
									font-size: var(--font14Size);

									text {
										color: #858585;
									}
								}

								.other_2 {
									font-size: var(--font14Size);

									text {
										color: var(--fFB1Color);
										padding: 0 2vw 0 0;
									}
								}
							}
						}
					}
				}
			}
		}
	}
</style>
