<template>
	<view class="whole">
		<view class="nav_tab">
			<view v-for="(item,index) in couponTitleList" :key="item.id" @click="getChang(item)" class="titleTab" :class="status == item.id ? 'nav_title' : ''">{{item.title}}</view>
		</view>

		<view class="coupon-list sonin">
			<scroll-view scroll-y="true" style="width: 100%;height:100%;" @scrolltolower="scorllBottom">
				<template v-if="couponList.length !== 0">
					<view class="coupon-box" v-for="(item, index) in couponList" :key="index">
						<view class="oneBox">
							<view class="coupon-left" :class="status == 1 ? 'textColor1' : 'textColor2'">
								<text class="sign">￥<text class="money">{{item.amount}}</text></text>
								<view class="title" v-if="item.full_amount * 100 > 0">满{{item.full_amount}}元可用</view>
								<view class="title" v-else-if="item.type == 51">抵扣金额</view>
								<view class="title" v-else>无门槛</view>
								<view class="vipTag" v-if="item.type == 12">会员优惠券</view>
							</view>
							<view class="coupon-right">
								<view class="birth" v-if="item.source_type == 'birth'">生日优惠券</view>
								<view v-if="item.use_type == 20 || item.use_type === 31" class="text chang" :class="status == 1 ? 'textColor' : 'textColor2'">仅该团购可用：{{item.extend.title}}</view>
								<view v-else-if="item.use_type == 21 && item.type === 50" class="text" :class="status == 1 ? 'textColor' : 'textColor2'">
									{{item.extend && item.extend.title || '积分商品可用'}}
									<view class="red_label">
										<view class="red_col red_col1">积分实物兑换</view>
										<view class="red_col red_col2">抵扣券</view>
									</view>
								</view>
								<view v-else-if="item.type === 51" class="text" :class="status == 1 ? 'textColor' : 'textColor2'">
									{{item.extend && item.extend.title || '实物商品可用'}}
									<view class="red_label">
										<view class="red_col red_col1">实物商品兑换</view>
										<view class="red_col red_col2">抵扣券</view>
									</view>
								</view>
								<view v-else-if="item.use_type === 40 || item.use_type === 41" class="text" :class="status == 1 ? 'textColor' : 'textColor2'">
									<text>指定团购分类可用</text>
								</view>
								<view v-else class="text" :class="status == 1 ? 'textColor' : 'textColor2'">
									<text v-if="Number(item.amount) < 3">全场通用</text>
									<text v-else>部分团可用</text>
								</view>
								<view v-if="item.shop_groups && (item.use_type === 40 || item.use_type === 41)">
									<view class="solid" :class="status == 1 ? 'red' : ''" v-for="it in item.shop_groups" :key="it.id">{{it.name}}</view>
								</view>
								<text class="time">{{item.finish_time.substring(0, 16)}}前有效</text>
								<view class="tips">福利款、活动产品、跨境产品不能使用且不能叠加</view>
							</view>
						</view>
						
						<view v-if="status == 2" class="cannot one" :style="{backgroundImage: 'url(' + sp2 + ')'}"></view>
						<view v-if="status == 3" class="cannot two" :style="{backgroundImage: 'url(' + sp2 + ')'}"></view>
						<view v-if="status == 4" class="cannot three"></view>
						
						<view class="tag" v-if="item.is_transfer">
							<up-tag size="mini" type="warning" text="好友赠送"></up-tag>
						</view>
						
						<view class="twoBox" v-if="status == 1">
							<view class="coupon-btn" @click="gotoHome(item)">立即使用</view>
						</view>
					</view>
					<view style="height: 1px;"></view>
				</template>
				<template v-if="!loading && !couponList.length">
					<up-empty mode="coupon" icon="https://ct-upimg.yx090.com/g.ii090/images/sprite/empty/coupon.png" text="暂无优惠券"></up-empty>
				</template>
				<view v-if="loading" class="loadbox">
					<up-loading-icon text="加载中..." textSize="14"></up-loading-icon>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs } from 'vue'
	import { post } from '@/api/request.js'
	import { sp2 } from '@/components/img.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		setup() {
			const data = reactive({
				sp2: sp2,
				couponTitleList: [
					{ id: 1, title: '未使用' },
					{ id: 2, title: '已使用' },
					{ id: 3, title: '已过期' },
					{ id: 4, title: '已失效' },
					{ id: 5, title: '已转赠' }
				],
				status: 1,
				couponList: [],
				page: 1,
				lastPage: 0,
				loading: false,
				Color: uni.getStorageSync('theme_color'),
				bgColor: Style[uni.getStorageSync('theme_index') * 1].bgColor,
				priceColor: Style[uni.getStorageSync('theme_index') * 1].priceColor
			})

			//切换tab事件
			function getChang(item) {
				data.status = item.id
				data.couponList = []
				data.page = 1
				getCoupon()
			}
			//点击立即使用跳转到首页
			function gotoHome(item) {
				if((item.use_type === 20 || item.use_type === 31) && item.extend && item.extend.id) {
					uni.navigateTo({
						url: '/pages/groups/index?id=' + item.extend.id
					})
				} else if(item.use_type === 21 && item.type === 50) {
					uni.navigateTo({
						url: '/pages/mine/scoreShop/good?id=' + item.shop_coupon_id
					})
				} else if(item.type === 51) {
					uni.navigateTo({
						url: '/pages/vip/wheel/good?id=' + item.id
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}
			//获取优惠券数据
			const getCoupon = () => {
				data.loading = true
				uni.showLoading({
					mask: true,
					title: '加载中'
				})
				post('/api/v1/UserCoupon/getList', {
					status: data.status,
					page: data.page,
					pageSize: 20,
				}).then((res) => {
					data.couponList = data.couponList.concat(res.data)
					data.lastPage = res.meta.last_page
					data.loading = false
					uni.hideLoading()
				}).catch(() => {
					data.loading = false
				})
			}

			const scorllBottom = () => {
				if (data.page < data.lastPage) {
					data.page++
					getCoupon()
				}
			}

			return {
				...toRefs(data),
				getChang,
				gotoHome,
				getCoupon,
				scorllBottom
			}
		},
		onLoad() {
			this.getCoupon()
		}
	}
</script>

<style lang="scss" scoped>
	.whole{
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}
	.nav_tab {
		position: sticky;
		top: 0;
		width: 100%;
		z-index: 999;
		display: flex;
		justify-content: space-around;
		height: 118rpx;
		align-items: center;
		background-color: #ffffff;
		box-shadow: 0rpx 0rpx 50rpx rgba(0, 0, 0, 0.08);
		.titleTab{
			padding: 10rpx 24rpx;
			background: #f7f7f7;
			font-size: 28rpx;
			border-radius: 10rpx;
			color: #999999;
		}
		.nav_title{
			background-color: v-bind('bgColor');
			color: v-bind('Color');
		}
	}

	.coupon-list {
		position: relative;
		height: calc(100vh - 118rpx);
		.coupon-box {
			margin: 30rpx;
			background-color: #ffffff;
			position: relative;
			.cannot{
				width: 52px;
				height: 52px;
				background-repeat: no-repeat;
				background-size: 678px 612px;
				position: absolute;
				right: 0;
				top: 0;
				&.one{
					background-position: -480px -30px;
				}
				&.two{
					background-position: -404px -30px;
				}
				&.three{
					background-size: 100%;
					background-image: url('https://ct-upimg.yx090.com/ju8hn6/shop/image/2025/05/24/BynRrxe4AY1gSAYipl8Xd2oc6mOptTvClqZRPicv.png');
				}
			}
			.tag{
				position: absolute;
				top: 0;
				left: 0;
			}
			.oneBox{
				display: flex;
				align-items: center;
				padding: 40rpx 0;
				.coupon-left{
					width: 190rpx;
					height: 100%;
					border-right: 2rpx dashed #f0f0f0;
					text-align: center;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					.sign{
						font-size: 36rpx;
					}
					.money{
						font-size: 42rpx;
					}
					.title{
						font-size: 28rpx;
						padding: 10rpx 16rpx 0;
					}
				}
				.coupon-right{
					width: calc(100% - 190rpx);
					padding: 0 20rpx;
					.text{
						font-size: 32rpx;
						margin-bottom: 10px;
						font-weight: 500;
						padding-right: 20rpx;
						&.chang{
							font-size: 32rpx;
						}
					}
					.time{
						font-size: 24rpx;
						color: #98989f;
					}
					.tips{
						font-size: 24rpx;
						color: #999;
						margin-top: 8rpx;
					}
				}
			}
			.twoBox{
				display: flex;
				justify-content: flex-end;
				padding: 20rpx 24rpx;
				border-top: 1rpx solid #f2f2f2;
				.coupon-btn{
					width: 140rpx;
					height: 50rpx;
					background: v-bind('Color');
					font-size: 24rpx;
					border-radius: 10rpx;
					color: #ffffff;
					text-align: center;
					line-height: 50rpx;
					margin-left: 20rpx;
				}
			}
		}
	}

	.textColor {
		color: #333333;
	}

	.textColor1 {
		color: v-bind('priceColor');
	}

	.textColor2 {
		color: #98989f;
	}
	.loadbox{
		padding: 30rpx 0;
		text-align: center;
		font-size: 24rpx;
		color: #98989f;
	}
	.red_label{
		margin-top: 10rpx;
		.red_col{
			font-size: 22rpx;
			color: v-bind('Color');
			padding: 4rpx 10rpx;
			background: v-bind('bgColor');
			border: 1px solid v-bind('Color');
			display: inline-block;
			border-radius: 6rpx;
			&.red_col1{
				border-right: none;
			}
			&.red_col2{
				border-left: 1px dashed v-bind('Color');
			}
		}
	}
	.birth{
		background-image: linear-gradient(90deg, #FBBA65, #F44B4D);
		border-radius: 4px;
		align-items: center;
		display: inline-flex;
		color: #fff;
		font-size: 24rpx;
		justify-content: center;
		padding: 2px 5px;
		width: -webkit-max-content;
		width: -moz-max-content;
		width: max-content;
		line-height: 1;
	}
	.solid{
		margin: 0 10rpx 10rpx 0;
		&.red{
			color: v-bind('Color');
			border: 1rpx solid v-bind('Color');
		}
	}
</style>
