<template>
	<view class="container" v-if="total !== null">
		<!-- 头部优惠券总数量区域 -->
		<view class="header">
			可使用优惠券
			<text>{{ total }}张</text>
		</view>

		<!-- 优惠券列表区域 -->
		<scroll-view class="body" scroll-y @scrolltolower="scrolltolower">
			<!-- 每一张优惠券区域 -->
			<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_tags">
							<view class="tag">有限期至{{ item.effectiveEndDate.slice(0, 10) }}</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="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>
						<view class="right_radio">
							<radio style="transform: scale(0.8)" color="#096562" :checked="item.isCheck" @click="handleChange(item)" />
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 底部确定按钮区域 -->
		<view class="foot_btn" @click="handleConfirm">确定</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>
export default {
	data() {
		return {
			list: [],
			hotelId: '',
			page: 1,
			rows: 10,
			total: null,
			// 指定民宿信息
			hotels: '',
			totalPrice: null,
			complaintId: null
		}
	},
	onLoad(options) {
		this.hotelId = options.id
		this.totalPrice = options.totalPrice
		this.complaintId = options.complaintId
		this.getCouponNum()
	},
	methods: {
		scrolltolower() {
			if (this.list.length < this.total) {
				this.page++
				this.getCouponNum()
			} else {
				uni.showToast({
					title: '没有更多数据了',
					icon: 'none'
				})
			}
		},
		// 确定按钮回调
		async handleConfirm() {
			let temObj = this.list.find((ele) => ele.isCheck)
			if (temObj) {
				const res = await this.$myRequest({
					url: '/mhotel/hcuseCoupons.action',
					method: 'post',
					data: {
						id: temObj.id,
						complaintId: temObj.complaintId,
						totalPrice: this.totalPrice
					}
				})
				// console.log(res)
				if (res.code === 200) {
					uni.$emit('choose', { data: res.data })
				}
			} else {
				uni.$emit('choose', { data: { discountAmount: 0 } })
			}
			uni.navigateBack(1)
		},
		// 点击指定民宿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 getCouponNum() {
			const res = await this.$myRequest({
				url: '/mhotel/hcusefulCoupon.action',
				data: {
					hotelId: this.hotelId,
					page: this.page,
					rows: this.rows,
					userId: uni.getStorageSync('userInfo').id
				}
			})
			// console.log(res);
			if (res.code === 200) {
				this.list = [...this.list, ...res.page.pageList]
				this.total = res.page.total
				this.list.forEach((ele, index) => {
					ele.hotelIds = ele.hotelIds.split(',')
					this.$set(ele, 'isCheck', false)
				})
				if (this.complaintId !== 'undefined') {
					const num = this.list.findIndex((e) => e.complaintId === this.complaintId)
					this.list[num].isCheck = true
				}
			}
		},
		handleChange(item) {
			if (item.isCheck) {
				item.isCheck = !item.isCheck
			} else {
				this.list.forEach((ele) => {
					if (item.id === ele.id) {
						ele.isCheck = true
					} else {
						ele.isCheck = false
					}
				})
			}
		},
		// 点击弹窗我知道了按钮回调
		handleClose() {
			this.$refs.popup.close()
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	box-sizing: border-box;
	padding: 0 30rpx;
	height: 100vh;
	background-color: #f7f7f7;

	.header {
		display: flex;
		align-items: center;
		height: 100rpx;
		font-size: 32rpx;

		text {
			margin-top: 10rpx;
			margin-left: 15rpx;
			color: #808080;
			font-size: 24rpx;
		}
	}

	.body {
		height: calc(100vh - 245rpx);
		overflow-y: auto;

		.body_item {
			position: relative;
			margin-bottom: 10rpx;
			padding-bottom: 10rpx;
			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_tags {
						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;
							}
						}
					}
				}

				.box_right {
					flex: 2;
					display: flex;

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

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

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

						.color {
							color: #a6a6a6;
						}
					}

					.right_radio {
						display: flex;
						justify-content: center;
						align-items: center;
						width: 40rpx;
					}
				}
			}
		}
	}

	.foot_btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 32rpx 0 17rpx;
		width: 690rpx;
		height: 96rpx;
		color: #fff;
		font-size: 32rpx;
		font-weight: bold;
		border-radius: 64rpx;
		background-color: #096562;
	}

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