<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="info">
			<scroll-view scroll-y="true" class="scroll-view">
				<view class="list-scroll-view">
					<view class="one">
						<view class="one_1">
							<text class="name textOver">{{info.name||'暂无'}}</text>
							<text @tap='toInfo(info)'>购买须知<text class="iconfont icon-dayuhao"></text></text>
						</view>
						<view class="one_2">
							<view class="time time_1" v-if="date">{{date}}</view>
							<view class="time_2" v-else>请选择时间!</view>
							<button type="button" @click="open">选择日期</button>
						</view>
						<view class="one_3">
							<view class="left">选择数量</view>
							<view class="right">
								<uni-number-box :min="1" :max="99" v-model="num" @change="changeNum" />
							</view>
						</view>
						<view class="one_4" v-if="couponList.length>0">
							<view class="left">优惠券</view>
							<view class="right" @tap="toCoupon">
								<text class="text" v-if="discountInfo.name">{{discountInfo.name}}</text>
								<text class="iconfont icon-dayuhao"></text>
							</view>
						</view>
					</view>
					<view class="two">
						<view class="two_1">
							<text class="text_1">游客信息</text>
							<text class="text_2">需填<text class="num">{{num}}</text>位，用于入园身份验证</text>
						</view>
						<view class="two_2" v-for="(item, index) in identityList" :key="index">
							<view class="list_1">
								<text>游客{{index+1}}</text>
								<text v-if="item.name||item.card||item.phone" @tap="toDel(item,index)"
									class="iconfont icon-shanchu"></text>
							</view>
							<view class="list_2">
								<view v-if="item.name||item.card||item.phone" class="form_1">
									<view class="form_1_1">
										<text v-if="item.name">{{item.name}}</text>
										<text style="color: red;" v-else>缺少姓名信息 请填写补充!</text>
									</view>
									<view class="form_1_1">
										<text v-if="item.card">{{item.card}}</text>
										<text style="color: red;" v-else>缺少证件号信息 请填写补充!</text>
									</view>
									<view class="form_1_1">
										<text v-if="item.phone">{{item.phone}}</text>
										<text style="color: red;" v-else>缺少手机号信息 请填写补充!</text>
									</view>
								</view>
								<view v-else class="form_2" @tap="toWrite(item,index)">点击填写游客信息</view>
							</view>
							<view class="list_3" @tap="toWrite(item,index)">编辑</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="foot">
			<view class="money">
				<text>￥{{info.original_price||'暂无'}}</text>
				<text>￥{{info.money||'暂无'}}起</text>
			</view>
			<view class="button">
				<button type="warn" @tap.stop="toOrder()">提交订单</button>
			</view>
		</view>
		<uni-calendar ref="calendar" :clear-date="true" :date="date" :insert="false" :lunar="true"
			:startDate="startDate" :endDate="endDate" :range="false" @confirm="confirm" />
		<uni-popup ref="popup" background-color="#fff" type="bottom" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>新增游客信息</text>
					<text @click="toClose" class="iconfont icon-shanchu"></text>
				</view>
				<view class="info_1">
					<uni-forms ref="form" :rules="rules" :modelValue="form" :label-width="80">
						<uni-forms-item label="证件类型">
							<uni-easyinput disabled v-model="type" />
						</uni-forms-item>
						<uni-forms-item label="姓名" name="name" required>
							<uni-easyinput v-model="form.name" placeholder="必填,请输入证件上的姓名" />
						</uni-forms-item>
						<uni-forms-item label="证件号" name="card" required>
							<uni-easyinput v-model="form.card" minlength="18" maxlength="18" placeholder="必填,请输入证件号" />
						</uni-forms-item>
						<uni-forms-item label="手机号" name="phone" required>
							<uni-easyinput v-model="form.phone" minlength="11" maxlength="11"
								placeholder="必填,请输入联系人手机号" />
						</uni-forms-item>
					</uni-forms>
					<button class="button" type="primary" @click="submit">保存</button>
				</view>
			</view>
		</uni-popup>
		<!-- 领取优惠卷 -->
		<uni-popup ref="coupon" background-color="#fff" type="bottom" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>抵用券</text>
					<text @click="toCouponClose" class="iconfont icon-shanchu"></text>
				</view>
				<view class="info_1">
					<scroll-view scroll-y="true" class="scroll-view">
						<view class="list-scroll-view">
							<view class="list" v-for="(item, index) in couponList" :key="index">
								<view class="list_1">
									<view class="left">
										<view class="left_1">
											￥<text>{{item.discount_config.min}}</text>
										</view>
										<view class="left_2">
											<text v-if="item.discount_config.limit=='0'">无门槛</text>
											<text
												v-else>满{{item.discount_config.limit}}减{{item.discount_config.min}}</text>
										</view>
									</view>
									<view class="center">
										<view class="name textOver">{{item.name}}</view>
										<view class="content">有效日期：{{item.time}}</view>
									</view>
									<view class="right">
										<button class="button_1" type="primary" @tap="toUser(item)">使用</button>
									</view>
								</view>
								<view class="list_2">
									<uni-collapse>
										<uni-collapse-item title="使用规则补充说明" :open="false">
											<view class="content">{{item.brief}}</view>
										</uni-collapse-item>
									</uni-collapse>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: '',
				user: {},
				info: {
					file: []
				},
				// 身份信息
				identityList: [{
					id: 0,
				}],
				// 优惠券
				couponList: [],
				discountInfo: '',
				// 数量
				num: 1,
				// 日期
				date: '',
				startDate: '',
				endDate: '',
				// 游客信息
				form: {
					name: '',
					card: '',
					phone: ''
				},
				type: '身份证',
				// 禁止滚动穿透
				show: false,
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请填写姓名'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请填写手机号码',
						}, {
							validateFunction: function(rule, value, data, callback) {
								let iphoneReg =
									/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
								if (!iphoneReg.test(value)) {
									callback('手机号码格式不正确，请重新填写')
								}
								return true
							}
						}]
					},
					card: {
						rules: [{
							required: true,
							errorMessage: '请填写证件号',
						}, {
							validateFunction: function(rule, value, data, callback) {
								let iphoneReg =
									/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9]||[Xx]$/
								if (!iphoneReg.test(value)) {
									callback('证件号格式不正确，请重新填写')
								}
								return true
							}
						}]
					}
				},
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchToken();
			that.search();
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			async searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						that.$set(that, `user`, res);
						const arr = await that.$api(`/userCoupon/specialQuery`, 'GET', {
							user: res._id || res.id,
							is_use: "0"
						})
						if (arr.errcode == '0') that.$set(that, `couponList`, arr.data)
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					const res = await that.$api(`/ticket/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						if (res.data.date) {
							that.$set(that, `startDate`, res.data.date[0])
							that.$set(that, `endDate`, res.data.date[1])
						}
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 详情
			toInfo(e) {
				uni.navigateTo({
					url: `/pagesHome/ticket/info?id=${e.id||e._id}`
				})
			},
			//日期弹窗
			open() {
				const that = this;
				that.$refs.calendar.open()
			},
			// 选择日期
			confirm(e) {
				const that = this;
				that.$set(that, `date`, e.fulldate)
			},
			// 数字选择器
			changeNum(value) {
				const that = this;
				that.$set(that, `num`, value)
				const data = []
				const identity = []
				identity.length = value;
				// 处理数量+1 游客信息+1
				for (let [index, val] of identity.entries()) {
					data.push({
						id: index
					})
				}
				that.$set(that, `identityList`, data)
			},
			// 删除游客信息
			toDel(item, index) {
				const that = this;
				const data = []
				for (let val of that.identityList) {
					if (val.id == item.id) data.push({
						id: index
					})
					else data.push(val)
				}
				that.$set(that, `identityList`, data)
			},
			// 编辑游客信息
			toWrite(item, index) {
				const that = this;
				that.$set(that, `form`, item)
				that.$refs.popup.open()
			},
			// 保存
			submit() {
				const that = this;
				that.$refs.form.validate().then(form => {
					// 根据id 寻找添加的游客信息
					for (let val of that.identityList) {
						if (val.id == that.form.id) {
							form.id = that.form.id
							val = form
						}
					}
					that.toClose()
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none',
						duration: 2000
					});
				})
			},
			// 领卷
			toCoupon() {
				const that = this;
				that.$refs.coupon.open()
			},
			// 关闭弹框
			toCouponClose() {
				const that = this;
				that.$refs.coupon.close();
			},
			// 去使用
			toUser(item) {
				const that = this;
				if (item.type == '0') {
					uni.showToast({
						title: '该优惠券不符合使用类型！',
						icon: 'none',
						duration: 2000
					});
				} else {
					const date = moment().format('YYYY-MM-DD HH:mm:ss');
					const time = item.time.split('至');
					// 判断日期是否在开始日期和结束日期中间，并且可能是开始日期或者结束日期
					const startDate = time[0];
					const endDate = time[1];
					const format = 'YYYY-MM-DD HH:mm:ss';
					const diffStart =
						moment(startDate, format).diff(moment(date, format), 'days') <= 0;
					const diffEnd =
						moment(endDate, format).diff(moment(date, format), 'days') >= 0;
					const diffDtate = diffStart && diffEnd;
					if (diffDtate) that.$set(that, `discountInfo`, item);
					else {
						uni.showToast({
							title: '该优惠券过期了 无法使用！',
							icon: 'none',
							duration: 2000
						});
					}
				}
				that.toCouponClose();
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$set(that, `form`, {});
				that.$refs.popup.close();
			},
			// 提交订单
			async toOrder() {
				const that = this;
				if (!that.date) {
					uni.showToast({
						title: '请选择日期!',
						icon: 'none',
						duration: 2000
					});
					return
				}
				// 判断每个游客信息是否都填写
				const name = that.identityList.every(function(item) {
					return item.name && item.card && item.phone;
				})
				if (name) {
					const data = {
						user: that.user._id || that.user.id,
						source: that.info,
						source_name: that.info.name,
						type: '1',
						money: that.$multiply(that.info.money, that.num),
						date: that.date,
						num: that.num,
						identity: that.identityList,
						discount: that.discountInfo._id || that.discountInfo.id,
						buy_time: moment().format('YYYY-MM-DD HH:mm:ss'),
						pay_time: '',
						pay: [],
						status: '1'
					}
					const res = await that.$api(`/order`, 'POST', data);
					if (res.errcode == '0') {
						uni.redirectTo({
							url: `/pagesIndex/order/index?status=${'1'}`
						})
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: '未填写游客信息!',
						icon: 'none',
						duration: 2000
					});
					return
				}
			}
		}
	}
</script>

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

		.info {
			position: relative;
			flex-grow: 1;
			background-image: linear-gradient(to top, rgba(241, 241, 241, 1), rgba(241, 241, 241, 1), rgba(0, 122, 255, 1));
			border-bottom: 1px solid var(--f9Color);

			.one {
				margin: 2vw;
				padding: 2vw;
				border-radius: 10px;
				background-color: var(--mainColor);

				.one_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 2vw;
					border-bottom: 1px solid var(--f9Color);

					.name {
						width: 85vw;
						font-size: var(--font18Size);
						font-weight: bold;
					}

					text:last-child {
						width: 20vw;
						text-align: right;
						font-weight: bold;
						font-size: var(--font12Size);
						color: var(--f3CColor);
					}
				}

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

					.time {
						text-align: center;
						padding: 2vw;
						margin: 2vw;
						border-radius: 10px;
						border: 1px solid;
					}

					.time_1 {
						color: var(--f3CColor);
					}

					.time_2 {
						padding: 2vw;
						font-size: var(--font12Size);
						color: var(--fF0Color);
					}
				}

				.one_3 {
					display: flex;
					justify-content: space-between;
					padding: 4vw 2vw;
					border-bottom: 1px solid var(--f9Color);

					.left {
						font-size: var(--font18Size);
						font-weight: bold;
					}
				}

				.one_4 {
					display: flex;
					justify-content: space-between;
					padding: 2vw;
					border-bottom: 1px solid var(--f9Color);

					.left {
						font-size: var(--font18Size);
						font-weight: bold;
					}

					.right {
						.text {
							padding: 4px;
							font-size: var(--font12Size);
							color: var(--mainColor);
							border-radius: 5px;
							background: linear-gradient(to right, #FFA500, #FF0000);
						}
					}
				}
			}

			.two {
				margin: 0 2vw;
				padding: 2vw;
				border-radius: 10px;
				background-color: var(--mainColor);

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

					.text_1 {
						font-size: var(--font18Size);
					}

					.text_2 {
						font-size: var(--font14Size);
						color: var(--f85Color);
						padding: 0 2vw;

						.num {
							padding: 0 2px;
							color: var(--fF0Color);
						}
					}
				}

				.two_2 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 2vw;
					border-bottom: 1px solid var(--f9Color);

					.list_1 {
						display: flex;
						flex-direction: column;
						align-items: center;
						font-size: var(--font14Size);
						color: var(--f85Color);

						text {
							padding: 2px 0;
						}
					}

					.list_2 {
						.form_1 {
							width: 55vw;
							padding: 0 2px;

							.form_1_1 {
								padding: 3px 0;
								font-size: var(--font12Size);
							}
						}

						.form_2 {
							color: var(--f3CColor);
							font-size: var(--font14Size);
						}
					}

					.list_3 {
						font-size: var(--font12Size);
					}
				}
			}
		}

		.foot {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 2vw;
			background-color: var(--mainColor);

			.money {
				text-align: right;
				color: var(--fF0Color);
				font-size: var(--font16Size);
				font-weight: bold;

				text:first-child {
					text-decoration: line-through;
					color: var(--f69Color);
					font-size: var(--font12Size);
					font-weight: 400;
				}
			}

			.button {
				button {
					width: 30vw;
					font-size: var(--font14Size);
					border-radius: 40px;
					background: linear-gradient(to right, #00BFFF, #007AFF);
				}
			}
		}
	}

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

	.popup {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 60vh;

		.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;
			height: 54vh;
			padding: 2vw;

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

				.list_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						width: 20vw;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						padding: 3vw;
						color: var(--fF0Color);
						font-size: var(--font14Size);

						.left_1 {
							text {
								font-weight: bold;
								font-size: 25px;
							}
						}

						.left_2 {
							font-size: var(--font12Size);
						}
					}

					.center {
						width: 40vw;

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

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

					.right {
						.button {
							font-size: 12px;
							border-radius: 20px;
							background: linear-gradient(to right, #FFA500, #FF0000);
						}

						.button_1 {
							font-size: 12px;
							border-radius: 20px;
							border: 1px solid var(--fF0Color);
							color: var(--fF0Color);
							background: var(--mainColor);
						}
					}
				}

				.list_2 {
					font-size: var(--font12Size);

					/deep/.uni-collapse-item__title-box {
						padding: 0 !important;
						font-size: 12px !important;
					}

					/deep/.uni-collapse-item__title-text {
						font-size: 12px !important;
					}

					.content {
						padding: 2vw;
					}
				}
			}

			button {
				font-size: var(--font14Size);
				background: linear-gradient(to right, #00BFFF, #007AFF);
			}
		}
	}

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

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