<template>
	<view class="whole">
		<view class="coupon-list sonin">
			<view v-for="(it, index) in couponList" :key="it.id" class="item">
				<view class="tit">{{it.time}}</view>
				<view class="coupon-box" v-for="(item, i) in it.list" :key="i" :class="item.status == 1 ? '' : 'textColor1'">
					<view class="coupon-left">
						<text class="sign">￥<text class="money">{{item.amount}}</text></text>
						<view class="title" v-if="item.full_amount * 1 > 0">满{{item.full_amount}}元可用</view>
						<view class="title" v-else>无门槛</view>
					</view>
					<view class="coupon-right">
						<view class="coupon-text">
							<view class="text textColor" v-if="item.extend">{{item.extend.title}}</view>
							<view v-else class="text textColor">
								<text v-if="Number(item.amount) < 3">全场通用</text>
								<text v-else>部分团可用</text>
							</view>
							<text class="time">{{item.created_at.substring(0, 10).replace(/-/g, "/")}}-{{item.finish_time.substring(0, 10).replace(/-/g, "/")}}</text>
						</view>
						<view class="coupon-btn" v-if="item.status == 1" @click="gotoHome(item)">
							<text>去使用</text>
							<up-icon name="arrow-right" :color="Color" />
						</view>
						<view v-if="item.status == 2" class="cannot one" :style="{backgroundImage: 'url(' + sp2 + ')'}"></view>
						<view v-if="item.status == 3" class="cannot two" :style="{backgroundImage: 'url(' + sp2 + ')'}"></view>
					</view>
				</view>
				<view style="height: 1px;"></view>
			</view>
			<template v-if="!loading && !couponList.length">
				<up-empty mode="data" icon="https://ct-upimg.yx090.com/g.ii090/images/sprite/empty/data.png" text="暂无数据"></up-empty>
			</template>
			<view v-if="loading" class="loadbox">
				<up-loading-icon text="加载中..." textSize="14"></up-loading-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs } from 'vue'
	import { get, 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,
				couponList: [],
				loading: false,
				lastPage: 0,
				page: 1,
				Color: uni.getStorageSync('theme_color'),
				bgColor: Style[uni.getStorageSync('theme_index') * 1].bgColor,
				priceColor: Style[uni.getStorageSync('theme_index') * 1].priceColor
			})
			
			// 获取优惠券数据
			const getCoupon = () => {
				data.loading = true
				get('/api/v1/shopScore/coupon/exchange/log', {page: data.page, pageSize: 10}).then((res) => {
					data.lastPage = Math.ceil(res.total / 10)
					if(res.total) {
						for (let key in res.data) {
							if(data.couponList.length !== 0 && data.couponList[data.couponList.length - 1].time == key) {
								data.couponList[data.couponList.length - 1].list = data.couponList[data.couponList.length - 1].list.concat(res.data[key])
							} else {
								data.couponList.push({
									time: key,
									list: res.data[key]
								})
							}
						}
					}
					data.loading = false
				}).catch(() => {
					data.loading = false
				})
			}
			
			// 立即使用
			function gotoHome(item) {
				if(item.use_type === 21) {
					uni.navigateTo({
						url: '/pages/mine/scoreShop/good?id=' + item.shop_coupon_id
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}

			return {
				...toRefs(data),
				gotoHome,
				getCoupon
			}
		},
		onLoad() {
			this.getCoupon()
		},
		onReachBottom() {
			if (this.page < this.lastPage) {
				this.page ++
				this.getCoupon()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.whole{
		width: 100%;
		background-color: #fff;
	}
	.coupon-list {
		.item{
			margin-bottom: 20rpx;
			.tit{
				color: #333333;
				font-size: 28rpx;
				padding: 10rpx 30rpx;
			}
		}
		.coupon-box {
			margin: 0 30rpx 30rpx;
			background-color: #FEF7F1;
			height: 200rpx;
			display: flex;
			align-items: center;
			&.textColor1{
				background-color: #F4F5F7;
			}
			.coupon-left {
				width: 190rpx;
				height: 100%;
				text-align: center;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: v-bind('priceColor');
				.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;
				border-left: 2rpx dashed #e5e5e5;
				.coupon-text {
					.text {
						font-size: 32rpx;
						margin-bottom: 10px;
						font-weight: 500;
						padding-right: 20rpx;
						&.chang{
							font-size: 32rpx;
						}
					}

					.time {
						font-size: 26rpx;
						color: #98989f;
					}
				}

				.coupon-btn {
					height: 60rpx;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					color: v-bind('Color');
				}
				.cannot{
					width: 52px;
					height: 52px;
					background-repeat: no-repeat;
					background-size: 678px 612px;
					&.one{
						background-position: -480px -30px;
					}
					&.two{
						background-position: -404px -30px;
					}
				}
			}
		}
	}
	.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>
