<template>
	<view class="container">
		<!-- 分段器区域 -->
		<view class="segmented">
			<view class="segmented_box">
				<uni-segmented-control :current="activeCurrent" :values="headerList" style-type="text" active-color="#096562" @clickItem="onClickItem" />
			</view>
		</view>

		<!-- 领券中心横幅区域 -->
		<view class="banner">
			<img mode="aspectFill" src="../../static/my/banner.png" />
			<view class="banner_title">领券中心</view>
			<view class="banner_msg">
				<view class="msg_top">关注领券中心</view>
				<view>系统会不定时发放代金券</view>
			</view>
			<view class="banner_btn" @click="handleGoPage('/pages/couponCenter/couponCenter')">去逛逛</view>
		</view>

		<!-- 代金券列表区域 -->
		<scroll-view class="body" scroll-y @scrolltolower="handlePull">
			<!-- 每一个代金券区域 -->
			<view class="body_box" v-for="item in list" :key="item.id">
				<!-- 代金券类型区域 -->
				<img src="../../static/my/couponTitle.png" />
				<view class="box_title">
					<view class="title_type">{{ item.type === 1 ? '代金券' : '折扣券' }}</view>
					<!-- <view class="title_num">x{{ item.count }}张</view> -->
				</view>
				<!-- 代金券信息区域 -->
				<view class="box_info">
					<view class="info_left">
						<view class="left_top">{{ item.name }}</view>
						<view class="left_bottom">
							<view class="tag">有限期{{ item.effectiveStartDate.slice(0, 10) }}至{{ item.effectiveEndDate.slice(0, 10) }}</view>
							<!-- <view class="tag">可通用</view> -->
							<view class="tag" v-if="item.hotelIds.includes('-1')">全名宿</view>
							<view class="tag" v-else @click="handleClickTag(item.hotelIds)">
								指定民宿
								<img src="../../static/index/right2.png" />
							</view>
						</view>
					</view>
					<view class="info_right">
						<view class="right_top">{{ item.type === 1 ? '￥' + item.deductionPrice : item.rebatePrice + '折' }}</view>
						<view class="right_bottom" :class="{ color: item.meetPrice === 0 }">{{ item.meetPrice === 0 ? '无门槛' : '满' + item.meetPrice + '可用' }}</view>
						<view class="right_bottom">{{ item.type === 2 ? '最多减免' + item.maxDeduction : '' }}</view>
					</view>
				</view>
				<!-- 代金券按钮区域 -->
				<view class="box_btn" @click="handleUse(item)">去使用</view>
			</view>

			<view class="noData" v-if="list.length === 0">
				<img lazy-load :lazy-load-margin="0" src="../../static/images/noData.png" />
				暂无数据
			</view>

			<!-- 指定民宿弹窗区域 -->
			<uni-popup ref="popup" type="center" :is-mask-click="false">
				<view class="popup_body">
					<view class="popup_header">
						<img src="../../static/my/popup_title.png" />
						<view class="header_title">指定民宿</view>
						<img src="../../static/my/popup_title.png" />
					</view>
					<view class="popup_center">{{ hotels }}</view>
					<view class="popup_btn" @click="handleClose">我知道了</view>
				</view>
			</uni-popup>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 分段器当前激活索引
			activeCurrent: 0,
			// 分段器数组
			headerList: ['全部', '代金券', '折扣券'],
			list: [],
			// 当前页
			page: 1,
			// 每页多少条
			rows: 6,
			// 总条数
			total: null,
			types: '1,2',
			// 指定民宿信息
			hotels: ''
		}
	},
	onShow() {
		this.activeCurrent = 0
		this.types = '1,2'
		this.page = 1
		this.list = []
		this.getData()
	},
	// onLoad() {
	// 	this.getData()
	// },
	methods: {
		async getData() {
			const res = await this.$myRequest({
				url: '/mhotel/hccardCouponPage.action',
				data: {
					userId: uni.getStorageSync('userInfo').id,
					types: this.types,
					page: this.page,
					rows: this.rows
				}
			})
			// console.log(res);
			if (res.code === 200 && res.page.pageList) {
				this.list = [...this.list, ...res.page.pageList]
				this.total = res.page.total
				this.list.forEach((ele) => {
					ele.hotelIds = ele.hotelIds.split(',')
				})
			}
		},
		// 点击指定民宿tag回调
		async handleClickTag(ids) {
			const res = await this.$myRequest({
				url: '/mhotel/hcdesignatedHotel.action',
				data: {
					hotelIds: ids.join()
				}
			})
			// console.log(res);
			if (res.code === 200) {
				this.hotels = res.data.name
			}
			this.$refs.popup.open()
		},
		// 去使用按钮回调
		handleUse(item) {
			if (item.hotelIds.length === 1 && !item.hotelIds.includes('-1')) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${item.hotelIds[0]}`
				})
			} else {
				uni.switchTab({
					url: '/pages/home3/home3'
				})
			}
		},
		// 切换分段器回调
		onClickItem(e) {
			this.activeCurrent = e.currentIndex
			if (this.activeCurrent === 0) {
				this.types = '1,2'
			} else if (this.activeCurrent === 1) {
				this.types = '1'
			} else if (this.activeCurrent === 2) {
				this.types = '2'
			}
			this.page = 1
			this.list = []
			this.getData()
		},
		// 列表下拉到底部回调
		handlePull() {
			if (this.list.length < this.total) {
				this.page++
				this.getData()
			} else {
				uni.showToast({
					title: '没有更多数据了',
					icon: 'none'
				})
			}
		},
		// 跳转页面回调
		handleGoPage(url) {
			uni.navigateTo({
				url
			})
		},
		// 点击弹窗我知道了按钮回调
		handleClose() {
			this.$refs.popup.close()
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	height: 100vh;
	background-color: #f2f3f5;
	overflow: hidden;

	.segmented {
		box-sizing: border-box;
		padding-bottom: 28rpx;
		height: 100rpx;
		background-color: #fff;

		.segmented_box {
			width: 50%;
		}
	}

	.banner {
		position: relative;
		padding: 0 20rpx;
		height: 158rpx;

		img {
			margin-top: 31rpx;
			width: 710rpx;
			height: 95rpx;
		}

		.banner_title {
			position: absolute;
			top: 42rpx;
			left: 55rpx;
			color: #fff;
			font-size: 50rpx;
			font-weight: bold;
		}

		.banner_msg {
			position: absolute;
			top: 46rpx;
			left: 280rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-left: 15rpx;
			height: 61rpx;
			color: #fff;
			font-size: 20rpx;
			border-left: 1rpx dotted #fff;

			.msg_top {
				margin-top: -5rpx;
			}
		}

		.banner_btn {
			position: absolute;
			top: 46rpx;
			left: 556rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 133rpx;
			height: 61rpx;
			color: #000;
			font-size: 28rpx;
			border-radius: 12rpx;
			background: linear-gradient(90deg, #ffeb3b 0%, #ffb300 100%);
		}
	}

	.body {
		box-sizing: border-box;
		padding: 0 30rpx 20rpx;
		height: calc(100vh - 258rpx);

		.body_box {
			position: relative;
			padding-bottom: 18rpx;
			margin-bottom: 20rpx;
			// height: 218rpx;
			border-radius: 15rpx;
			background-color: #fff;

			img {
				width: 135rpx;
				height: 36rpx;
			}

			.box_title {
				position: absolute;
				top: 0;
				left: 16rpx;
				display: flex;
				align-items: center;
				color: #fff;

				.title_type {
					font-size: 24rpx;
					font-weight: bold;
				}

				.title_num {
					margin-left: 5rpx;
					margin-top: 10rpx;
					font-size: 16rpx;
				}
			}

			.box_info {
				display: flex;
				padding: 0 30rpx 0 25rpx;

				.info_left {
					flex: 4;

					.left_top {
						margin: 12rpx 0;
						font-size: 36rpx;
					}

					.left_bottom {
						display: flex;
						flex-wrap: wrap;
						color: #808080;
						font-size: 24rpx;

						.tag {
							display: flex;
							align-items: center;
							margin-right: 32rpx;
							margin-bottom: 15rpx;

							img {
								margin-top: 4rpx;
								margin-left: 4rpx;
								width: 28rpx;
								height: 28rpx;
							}
						}
					}
				}
				.info_right {
					flex: 1;
					color: #ff5733;

					.right_top {
						margin: 10rpx 0;
						text-align: end;
						font-size: 36rpx;
					}

					.right_bottom {
						margin-bottom: 8rpx;
						text-align: end;
						font-size: 20rpx;
					}

					.color {
						color: #a6a6a6;
					}
				}
			}

			.box_btn {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-right: 30rpx;
				margin-top: 11rpx;
				margin-left: auto;
				width: 98rpx;
				height: 40rpx;
				color: #fff;
				font-size: 24rpx;
				border-radius: 6rpx;
				background-color: #096562;
			}
		}

		.noData {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding-bottom: 20rpx;

			img {
				margin-top: 75rpx;
				width: 600rpx;
				height: 600rpx;
			}
		}

		.popup_body {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 481rpx;
			// height: 404rpx;
			border-radius: 22.5rpx;
			background-color: #fff;

			.popup_header {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 123rpx;
				font-size: 34rpx;
				font-weight: bold;
				color: #0f194d;

				img {
					width: 16rpx;
					height: 16rpx;
				}

				.header_title {
					margin: 0 10rpx;
				}
			}

			.popup_center {
				box-sizing: border-box;
				padding: 0 20rpx 20rpx;
				max-height: 400rpx;
				line-height: 42rpx;
				// height: 135rpx;
				color: rgba(15, 25, 77, 0.6);
				font-size: 28rpx;
				font-weight: bold;
				overflow-y: auto;
			}

			.popup_btn {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 20rpx;
				width: 396rpx;
				height: 76rpx;
				color: #fff;
				font-size: 26rpx;
				border-radius: 43rpx;
				background: linear-gradient(90deg, #0bc196 0%, #096562 100%);
			}
		}
	}
}
</style>
