<template>
	<view class="container">
		<!-- 优惠券列表区域 -->
		<view class="body">
			<!-- 每一张优惠券区域 -->
			<view class="body_item" v-for="item in list" :key="item.id">
				<img src="../../static/my/couponTitle.png" />
				<view class="item_type" v-if="item.type === 2">折扣卷</view>
				<view class="item_type" v-if="item.type === 1">代金卷</view>
				<view class="item_box">
					<view class="box_left">
						<view class="left_title">{{ item.name }}</view>
						<view class="left_time">{{ item.effectiveStartDate.slice(0, 16) }} - {{ item.effectiveEndDate.slice(0, 16) }} 可领取</view>
						<view class="left_tags">
							<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="box_right">
						<view class="right_info">
							<view class="info_top">{{ item.type === 1 ? '￥' + item.deductionPrice : item.rebatePrice + '折' }}</view>
							<view class="info_bottom" :class="{ color: item.meetPrice === 0 }">{{ item.meetPrice === 0 ? '无门槛' : '满' + item.meetPrice + '可用' }}</view>
							<view class="info_bottom">
								{{ item.type === 2 ? '最多减免' + item.maxDeduction : '' }}
							</view>
							<view class="info_btn" v-if="item.claimStatus === 1" @click="handleGet(item.id, item.effectiveEndDate)">立即领取</view>
							<view class="info_btn2" v-else>已领取</view>
						</view>
					</view>
				</view>
			</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>
	</view>
</template>

<script>
var dayjs = require('dayjs')
export default {
	data() {
		return {
			// 优惠券列表数据
			list: [],
			// 当前页
			page: 1,
			// 每页多少条
			rows: 6,
			// 总条数
			total: null,
			// 指定民宿信息
			hotels: ''
		}
	},
	onLoad() {
		this.getData()
	},
	onReachBottom() {
		if (this.list.length < this.total) {
			this.page++
			this.getData()
		} else {
			uni.showToast({
				title: '没有更多数据了',
				icon: 'none'
			})
		}
	},
	methods: {
		async getData() {
			const res = await this.$myRequest({
				url: '/mhotel/hccouponCollection.action',
				data: {
					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()
		},
		// 领取优惠券请求
		async handleGet(id, endTime) {
			let time = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
			const res = await this.$myRequest({
				url: '/mhotel/hcgetCoupon.action',
				method: 'POST',
				data: {
					complaintId: id,
					userId: uni.getStorageSync('userInfo').id,
					createId: uni.getStorageSync('userInfo').id,
					createDate: time,
					modifyDate: time,
					lapseDate: endTime
				}
			})
			// console.log(res);
			if (res.code === 200) {
				uni.showToast({
					title: res.message,
					icon: 'success',
					mask: true
				})
				setTimeout(() => {
					uni.navigateBack(1)
				}, 1500)
			}
		},
		// 点击弹窗我知道了按钮回调
		handleClose() {
			this.$refs.popup.close()
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	box-sizing: border-box;
	padding: 20rpx 30rpx;
	height: 100vh;
	overflow-y: auto;
	background-color: #f2f3f5;

	.body {
		.body_item {
			position: relative;
			margin-bottom: 20rpx;
			padding-bottom: 22rpx;
			width: 690rpx;
			border-radius: 15rpx;
			background-color: #fff;

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

			.item_type {
				position: absolute;
				top: 0;
				left: 14rpx;
				font-size: 24rpx;
				font-weight: bold;
				color: #fff;
			}

			.item_box {
				padding: 0 25rpx;
				display: flex;

				.box_left {
					flex: 6;
					overflow: hidden;

					.left_title {
						margin: 12rpx 0;
						font-size: 36rpx;
						font-weight: bold;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.left_time {
						display: flex;
						flex-wrap: wrap;
						color: #808080;
						font-size: 20rpx;
					}

					.left_tags {
						display: flex;
						align-items: center;
						margin-top: 20rpx;
						color: #808080;
						font-size: 20rpx;

						.tag {
							display: flex;
							align-items: center;
							margin-right: 44rpx;

							img {
								margin-top: 4rpx;
								margin-left: 4rpx;
								width: 28rpx;
								height: 28rpx;
							}
						}
					}
				}

				.box_right {
					flex: 2;
					display: flex;

					.right_info {
						flex: 1;
						display: flex;
						flex-direction: column;
						align-items: flex-end;
						width: 100rpx;
						color: #ff5733;

						.info_top {
							margin-top: 10rpx;
							font-size: 36rpx;
						}

						.info_bottom {
							margin-top: 10rpx;
							font-size: 20rpx;
						}

						.color {
							color: #a6a6a6;
						}

						.info_btn {
							display: flex;
							justify-content: center;
							align-items: center;
							margin-top: 25rpx;
							width: 122rpx;
							height: 48rpx;
							color: #fff;
							font-size: 24rpx;
							border-radius: 6rpx;
							background-color: #096562;
						}

						.info_btn2 {
							display: flex;
							justify-content: center;
							align-items: center;
							margin-top: 25rpx;
							width: 122rpx;
							height: 48rpx;
							color: #fff;
							font-size: 24rpx;
							border-radius: 6rpx;
							background-color: #cccccc;
						}
					}
				}
			}
		}
	}

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

		img {
			margin-top: 220rpx;
			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;
			height: 135rpx;
			color: rgba(15, 25, 77, 0.6);
			font-size: 28rpx;
			font-weight: bold;
		}

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