<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="60" ></u-tabs-swiper> -->
		<u-tabs :list="couponGroupList" name="gc_name" :is-scroll="true" :current="couponCurrent" @change="couponTabsChange" active-color="#FF780F" :show-bar="false" height="60" font-size="24"  inactive-color="#333333"></u-tabs>
		 <!-- :style="{ height: swiperHeight }" -->
		<swiper class="box" :current="swiperCouponCurrent" @animationfinish="couponAnimationFinish" :style="{height: swiperHeight}">
			<swiper-item class="swiper-coupon-item" v-for="(_, i) in couponGroupList" :key="i">
				<!-- 最多显示四个 -->
				<view v-if="couponList[i]">
					<view v-for="(coupon, index) in couponList[i].slice(0, 4)" :key="index" class="coupon-item">
						<Coupon :couponInfo="coupon" :status='0' :type="0"></Coupon>
					</view>
				</view>
				<u-empty text="暂无优惠券" mode="coupon" color="#000" v-if="!couponList[i] || !couponList[i].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[this.couponCurrent] = res.data;
				} else {
					this.couponList[this.couponCurrent] = [];
				}
				// 设置 swiper 高度
				this.setViewHeight();
				this.$forceUpdate();
			})
		},
		couponTabsChange(index) {
			this.couponCurrent = index;
			this.swiperCouponCurrent = this.couponCurrent;
		},
		couponAnimationFinish(e) {
			const current = e.detail.current;
			this.swiperCouponCurrent = current;
			this.couponCurrent = current;
		},
		setViewHeight() {
			// 一个优惠券的高度 97px, 下距离 10px
			let num = 0;
			num = this.couponList[this.couponCurrent].length 
				? this.couponList[this.couponCurrent].length > 4 ? 4 : this.couponList[this.couponCurrent].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: 80rpx;
		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: 20rpx;
			color: rgba(153,153,153,1);
		}
	}
    .box {
        padding-top: 20rpx;
        .swiper-coupon-item {
            padding: 0 30rpx;
            box-sizing: border-box;
            background-color: #ffffff;
            .coupon-item {
                height: 200rpx;
                margin-bottom: 20rpx;
            }
        }
    }
}
</style>