<template>
	<view class="coupon-list">
		<view v-for="(item,index) in couponData" style="padding:20rpx;" :key="index">
			<view>
				<view class="card">
					<view class="card-left">
						<view class="">
							{{switchType(item.stockType)}}
						</view>
					</view>
					<view class="card-center">
						<view class="card-center-top"></view>
						<view class="card-center-bottom"></view>
					</view>
					<view class="card-right">
						<view class="card-content">
							<view class="card-info">{{item.stockName}}</view>
							<view class="card-type">可用于
								<text class="card-type-text">专业喂养</text>
								<text class="card-type-text">专业遛狗</text>
								<!-- <text class="card-type-text">{{ item.goodsName }}</text> -->
							</view>
							<view class="card-time">有效期至: {{item.availableEndTime.slice(0, 16)}}</view>
						</view>
						<!-- <view style="width: 22%;">
							<u-button @click="receiveCoupon(item.id)" shape="circle" size="mini" color="#ffaa48" text="立即领取"></u-button>
						</view> -->
						<view style="width: 132rpx;height: 52rpx;background-color: #FFAA48; display: flex;align-items: center;justify-content: center;border-radius: 56rpx;">
							<text @click="receiveCoupon(item.id)" style="font-size: 24rpx; font-weight: 500; color: #FFFFFF;">立即领取</text>
						</view>
					</view>
				</view>
				<view class="card-bottom">
					<view class="card-bottom-text">
						优惠券不可兑换现金
					</view>
					<view class="card-bottom-text" @click="showRulePopup(item)">
						查看详细规则>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 优惠券详细规则弹窗 -->
		<uni-popup ref="rulePopup" type="center">
			<view class="rule-popup">
				<view class="rule-popup-title">优惠券详细规则</view>
				<view class="rule-popup-content">
					<view class="rule-item">
						<view class="rule-label">名称：</view>
						<view class="rule-value">{{currentCoupon.stockName}}</view>
					</view>
					<view class="rule-item">
						<view class="rule-label">折扣：</view>
						<view class="rule-value">{{getDiscountText(currentCoupon)}}</view>
					</view>
					<view class="rule-item">
						<view class="rule-label">使用规则：</view>
						<view class="rule-value">可用于专业喂养和专业遛狗服务</view>
					</view>
					<view class="rule-item">
						<view class="rule-label">有效日期：</view>
						<view class="rule-value">{{currentCoupon.availableEndTime ? currentCoupon.availableEndTime.slice(0, 16) : ''}}</view>
					</view>
					<view class="rule-item">
						<view class="rule-label">特别说明：</view>
						<view class="rule-value">单笔订单仅限使用1张优惠券；优惠券仅限用户本人使用，不可赠送、不可提现、不得找零。</view>
					</view>
				</view>
				<view class="rule-popup-close" @click="closeRulePopup">关闭</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	import {
		getCouponList,
		receiveCoupon,
	} from "@/api/system/user"
	export default {
		data() {
			return {
				couponData: [],
				currentCoupon: {}, // 当前选中的优惠券
			}
		},
		onLoad() {
			// this.openIdStr = this.$globalData.openIdStr;
			this.getCouponListAuth()
		},
		methods: {
			getCouponListAuth() {
				getCouponList().then(res => {
					if (res.code == 200) {
						this.couponData = res.data
						console.log("this.couponData", this.couponData)
					} else {
						this.$modal.showToast('获取优惠券失败')
					}
				})
			},
			switchType(type) {
				if (type == 'PNORMAL') {
					return '满减券'
				}
				if (type == 'PDISCOUNT') {
					return '折扣券'
				}
				if (type == 'PTRAIL') {
					return '体验券'
				}
				return '优惠券'
			},
			receiveCoupon (id) {
				let data = {
					stockId: id
				}
				receiveCoupon(data).then(res => {
					console.log("this.receiveCoupon", res)
					if (res.code == 200) {
						this.$modal.showToast('优惠券领取成功')
					} else {
						this.$modal.showToast('领取优惠券失败')
					}
				})
			},

			// 显示优惠券规则弹窗
			showRulePopup(item) {
				this.currentCoupon = item || {};
				this.$refs.rulePopup.open();
			},

			// 关闭优惠券规则弹窗
			closeRulePopup() {
				this.$refs.rulePopup.close();
			},

			// 获取优惠券折扣文本
			getDiscountText(coupon) {
				if (!coupon || !coupon.stockType) return '';

				if (coupon.stockType === 'PNORMAL') {
					return '满100可减10元';
				} else if (coupon.stockType === 'PDISCOUNT') {
					return '打8折';
				} else if (coupon.stockType === 'PTRAIL') {
					return '免费体验一次';
				}
				return '';
			}
		}
	}
</script>

<style lang="scss">
	.coupon-list {

		/* 优惠券规则弹窗样式 */
		.rule-popup {
			width: 600rpx;
			background-color: #FFFFFF;
			border-radius: 16rpx;
			overflow: hidden;
		}

		.rule-popup-title {
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: 600;
			color: #FFFFFF;
			background-color: #FFAA48;
		}

		.rule-popup-content {
			padding: 30rpx;
		}

		.rule-item {
			display: flex;
			margin-bottom: 20rpx;
		}

		.rule-label {
			width: 140rpx;
			font-size: 28rpx;
			color: #666666;
			flex-shrink: 0;
		}

		.rule-value {
			flex: 1;
			font-size: 28rpx;
			color: #333333;
			line-height: 40rpx;
		}

		.rule-popup-close {
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			font-size: 30rpx;
			color: #FFAA48;
			border-top: 1px solid #EEEEEE;
		}
		.card {
			display: flex;
			align-items: center;
			width: 100%;
			padding: 10px 0;
			background: #fff;
			border-radius: 8px 8px 0 0;
		}
		
		.card-bottom {
			display: flex;
			background-color: #FFF1E0;
			height: 50rpx;
			align-items: center;
			justify-content: space-between;
			padding: 0 20rpx 0 20rpx;
			border-radius: 0 0 8px 8px;
		
			.card-bottom-text {
				color: #AAAAAA;
				font-size: 24rpx;
				font-weight: 400;
			}
		}

		.card-left {
			width: 88px;
			text-align: center;
			color: #FF530A;
			font-size: 28rpx;
			font-weight: 900;
		}
		
		.card-center {
			display: flex;
			flex-direction: column;
			// align-items: center;
		
			.card-center-top {
				width: 40rpx;
				height: 20rpx;
				border-radius: 0 0 20rpx 20rpx;
				background-color: #F5F5F7;
				line-height: 20rpx;
				// border-bottom: 1px solid #FDA714;
				// border-left: 1px solid #FDA714;
				// border-right: 1px solid #FDA714;
				margin-top: -22rpx;
				margin-bottom: 20rpx;
				margin-left: -19rpx;
			}
		
			.card-center-bottom {
				border-right: 1px dashed #AAAAAA;
				width: 1px;
				height: 120rpx;
			}
		}

		.card-right {
			padding: 0px 12px;
			display: flex;
			flex: 1;
			/* flex-direction: column; */
			justify-content: space-between;
			align-items: center;
			height: 60px;

			.card-content {
				width: 77%;
			}

			.card-icon {
				position: relative;
				right: -10px;
				top: -10px;
			}
		}

		.card-info {
			margin: 0;
			font-size: 28rpx;
			line-height: 28rpx;
			color: #333333;
			font-weight: 500;
		}
		
		.card-type {
			font-size: 24rpx;
			font-weight: 400;
			line-height: 24rpx;
			font-weight: 400;
			color: #AAAAAA;
			margin-top: 10rpx;
		
			.card-type-text {
				color: #FFAA48;
				font-size: 24rpx;
				font-weight: 400;
				line-height: 24rpx;
				border: #FFAA48 1px solid;
				border-radius: 7rpx;
				margin-left: 8rpx;
			}
		}

		.card-time {
			font-size: 24rpx;
			font-weight: 400;
			line-height: 24rpx;
			font-weight: 400;
			color: #AAAAAA;
			margin-top: 10rpx;
		}
	}
</style>