<template>
	<view class="coupon-swiper">
		<view class="top">
			<view class="title">全部优惠券</view>
			<view class="view-more" @click="toCouponPage">查看更多></view>
		</view>
		<u-tabs-swiper ref="coupon" :list="couponGroupList" name="gc_name" :is-scroll="true" active-color="#FF780F" :current="couponCurrent" font-size="24" :show-bar="false" @change="couponTabsChange" height="88" ></u-tabs-swiper>
		 <!-- :style="{ height: swiperHeight }" -->
		<swiper :current="swiperCouponCurrent" @animationfinish="couponAnimationFinish" :style="{height: swiperHeight}">
			<swiper-item class="swiper-coupon-item" v-for="(_, i) in couponGroupList" :key="i">
				<!-- 最多显示四个 -->
				<view v-for="(coupon, index) in couponList.slice(0, 4)" :key="index" class="coupon-item">
					<Coupon :couponInfo="coupon" :status='0' :type="0"></Coupon>
				</view>
				<u-empty text="暂无优惠券" mode="coupon" color="#000" v-if="!couponList.length"></u-empty>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
import Coupon from "@/components/mine/coupon/index";
export default {
	data() {
		return {
			swiperHeight: '',
			couponCurrent: 0,
			swiperCouponCurrent: 0,
			couponGroupList: [],
			couponList: [],
		}
	},
	components: {
		Coupon
	},
	watch: {
		couponCurrent(index) {
			const id = this.couponGroupList[index].gc_id;
			this.getCouponList(id);
		}
	},
	created() {
		this.getGoodsClass();
	},
	methods: {
		getGoodsClass() {
			this.$u.api.getGoodsClass().then(res => {
				if(res.errCode == 0) {
					this.couponGroupList = res.data;
					this.getCouponList(this.couponGroupList[0].gc_id);
				}
			})
		},
		getCouponList(gc_id) {
			this.$u.api.getCouponList({
				page: 0,
				gc_id: gc_id, 
			}).then(res => {
				if(res.errCode == 0) {
					this.couponList = res.data;
				} else {
					this.couponList = [];
				}
				// 设置 swiper 高度
				this.setViewHeight();
			})
		},
		couponTabsChange(index) {
			this.couponCurrent = index;
		},
		couponAnimationFinish(e) {
			const current = e.detail.current;
			this.swiperCouponCurrent = current;
			this.couponCurrent = current;
		},
		setViewHeight() {
			// 一个优惠券的高度 97px, 下距离 10px
			let num = 0;
			num = this.couponList.length 
				? this.couponList.length > 4 ? 4 : this.couponList.length
				: 1
			this.swiperHeight = (200 + 20) * num + 'rpx';
		},
		toCouponPage() {
			this.$u.route({
				url: '/pageE/mine/MemberServe',
				params: {
					current: 1
				}
			})
		},
	},
};
</script>
<style lang="scss" scoped>
.coupon-swiper {
	background-color: #ffffff;
	margin-bottom: 40rpx;
	.top {
		padding: 0 30rpx;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.title {
			font-size: 30rpx;
			font-weight: 500;
			color: rgba(51,51,51,1);
		}
		.view-more {
			font-size: 18rpx;
			color: rgba(153,153,153,1);
		}
	}
	.swiper-coupon-item {
		padding: 0 30rpx;
		box-sizing: border-box;
		background-color: #ffffff;
		.coupon-item {
            height: 200rpx;
			margin-bottom: 20rpx;
		}
	}
}
</style>