<template>
	<view class="whole">
		<view class="nav_tab">
			<block v-for="(item,index) in couponTitleList" :key="item.id">
				<view @click="getChang(item)" class="titleTab" :class="status == item.id ? 'nav_title' : ''">{{item.title}}</view>
			</block>
		</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="coupon-left" :class="status == 1 ? 'textColor1' : status == 2 || status == 3 ? 'textColor2' : ''">
							<text class="sign">￥<text class="money">{{item.amount}}</text></text>
							<view class="title" v-if="item.use_type === 30 || item.use_type === 31">满{{item.full_amount}}元可用</view>
							<view class="title" v-else>无门槛</view>
						</view>
						<view class="coupon-right">
							<view class="coupon-text">
								<view v-if="item.use_type == 20 || item.use_type === 31" class="text chang" :class="status == 1 ? 'textColor' : status == 2 || status == 3 ? 'textColor2' : ''">仅该团购可用：{{item.extend.title}}</view>
								<view v-else-if="item.use_type == 21 && item.type === 50" class="text" :class="status == 1 ? 'textColor' : status == 2 || status == 3 ? '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 class="text" :class="status == 1 ? 'textColor' : status == 2 || status == 3 ? 'textColor2' : ''">
									<text v-if="Number(item.amount) < 3">全场通用</text>
									<text v-else>部分团可用</text>
								</view>
								<text class="time">{{item.finish_time.substring(0, 16)}}前有效</text>
								<view class="tips">福利款、活动产品、跨境产品不能使用且不能叠加</view>
							</view>
							<view class="coupon-btn" v-if="status == 1" @click="gotoHome(item)">立即使用</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>
					</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 { Style } from '@/utils/list.js'
	import { sp2 } from '@/components/img.js'
	
	export default {
		setup() {
			const data = reactive({
				sp2: sp2,
				couponTitleList: [
					{ id: 1, title: '未使用' },
					{ id: 2, title: '已使用' },
					{ id: 3, 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 {
					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: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 999;
		display: flex;
		justify-content: space-around;
		height: 118rpx;
		align-items: center;
		background-color: #ffffff;
		font-size: 34rpx;
		box-shadow: 0rpx 0rpx 50rpx rgba(0, 0, 0, 0.08);

		.titleTab {
			padding: 20rpx 56rpx;
			box-sizing: border-box;
			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;
		top: 118rpx;
		height: calc(100vh - 118rpx);

		.coupon-box {
			margin: 30rpx;
			background-color: #ffffff;
			padding: 40rpx 0;
			display: flex;
			align-items: center;

			.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;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.coupon-text {
					width: calc(100% - 150rpx);
					.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;
					}
				}
				.coupon-btn {
					width: 140rpx;
					height: 50rpx;
					background: v-bind('Color');
					font-size: 24rpx;
					border-radius: 10rpx;
					color: #ffffff;
					text-align: center;
					line-height: 50rpx;
				}
				.cannot{
					width: 52px;
					height: 52px;
					background-repeat: no-repeat;
					background-size: 678px 612px;
					&.one{
						background-position: -480px -30px;
					}
					&.two{
						background-position: -404px -30px;
					}
				}
			}
		}
	}

	.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');
			}
		}
	}
</style>
