<template>
	<view class="record">
		<view class="tips">
			<view class="tips-item" :class="{ 'active': typeIndex === index }" v-for="(item, index) in type" @click="handleIndex(item, index)">{{ item }}</view>
		</view>
		<view class="list-container" v-if="typeIndex == 0">
			<view v-for="(item, index) in couponList" :key="index"
				:class="['coupon-box']">
				<view class="coupon-card">
					<view class="left-content">
						<view class="coupon-title">
							<view>{{ item.couponName }}</view>
							<!-- <view v-if="item.discountType == 3">满{{ item.thresholdAmount }}减{{ item.discountValue }}</view>
							<view v-else>满{{ item.thresholdAmount }}可用</view> -->
						</view>
						<view class="coupon-time">
							<view v-if="item.validityType == 2">领券{{item.validDays }}天后失效</view>
							<view v-else-if="item.validityType == 3">长期有效</view>
							<view v-else>有效期至{{ item.expirationTimeString }}</view>
						</view>
						<view class="coupon-msg">
							<view v-if="item.couponType== 1">满减券*1</view>
							<view v-if="item.couponType== 2">折扣券*1</view>
							<view v-if="item.couponType== 3">兑换券*1</view>
							<view class="icon-circle" @click="item.showMsg = !item.showMsg">
								详细信息
								<view :class="{ 'circle': item.showMsg, 'default': !item.showMsg }"></view>
							</view>
						</view>
					</view>

					<view class="right-action">
						<view class="amount-box" v-if="item.couponType == 1">
							<text class="unit">¥</text>
							<text class="val">{{ item.discountValue }}</text>
						</view>
						<view class="amount-box" v-else-if="item.couponType == 2">
							<text class="val">{{ item.ruleDiscountRate }}</text>
							<text class="unit">折</text>
						</view>
						<view class="amount-box" v-else>
							<text class="val">兑换</text>
						</view>

						<view class="action-btn active">已使用</view>
					</view>
				</view>
				<view class="coupon-message" v-show="item.showMsg">
					<view class="message-item">
						<view>券类型：{{ item.couponType== '1'?'兑换券':item.couponType== '2'?'折扣券':item.couponType== '3'?'满减券':'' }}</view>
						<view v-if="item.locality">限定地区：仅{{ item.locality }}地区使用</view>
						<view v-if="item.relatedName">限定项目：{{ item.relatedName }}</view>
						<view>限定手机：仅限登录手机号为{{ getPhone() }}使用</view>
					</view>
				</view>
			</view>

			<u-empty v-if="couponList.length === 0" mode="coupon" marginTop="100"></u-empty>
		</view>

		<view class="list-container" v-if="typeIndex == 1">
			<view v-for="(item, index) in couponList" :key="index"
				:class="['coupon-box']">
				<view class="coupon-card">
					<view class="left-content">
						<view class="coupon-title">
							<view>{{ item.couponName }}</view>
							<!-- <view v-if="item.discountType == 3">满{{ item.thresholdAmount }}减{{ item.discountValue }}</view>
							<view v-else>满{{ item.thresholdAmount }}可用</view> -->
						</view>
						<view class="coupon-time">
							<view v-if="item.validityType == 2">领券{{item.validDays }}天后失效</view>
							<view v-else-if="item.validityType == 3">长期有效</view>
							<view v-else>有效期至{{ item.expirationTimeString }}</view>
						</view>
						<view class="coupon-msg">
							<view v-if="item.couponType== 1">满减券*1</view>
							<view v-if="item.couponType== 2">折扣券*1</view>
							<view v-if="item.couponType== 3">兑换券*1</view>
							<view class="icon-circle" @click="item.showMsg = !item.showMsg">
								详细信息
								<view :class="{ 'circle': item.showMsg, 'default': !item.showMsg }"></view>
							</view>
						</view>
					</view>

					<view class="right-action-lose">
						<view class="amount-box" v-if="item.couponType == 1">
							<text class="unit">¥</text>
							<text class="val">{{ item.discountValue }}</text>
						</view>
						<view class="amount-box" v-else-if="item.couponType == 2">
							<text class="val">{{ item.ruleDiscountRate }}</text>
							<text class="unit">折</text>
						</view>
						<view class="amount-box" v-else>
							<text class="val">兑换</text>
						</view>

						<view class="action-btn active">已失效</view>
					</view>
				</view>
				<view class="coupon-message" v-show="item.showMsg">
					<view class="message-item">
						<view>券类型：{{ item.couponType== '1'?'兑换券':item.couponType== '2'?'折扣券':item.couponType== '3'?'满减券':'' }}</view>
						<view v-if="item.locality">限定地区：仅{{ item.locality }}地区使用</view>
						<view v-if="item.relatedName">限定项目：{{ item.relatedName }}</view>
						<view>限定手机：仅限登录手机号为{{ getPhone() }}使用</view>
					</view>
				</view>
			</view>

			<u-empty v-if="couponList.length === 0" mode="coupon" marginTop="100"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		getCoupon
	} from '@/api/index';
	export default{
		data() {
			return {
				type: ['已使用','已失效'],
				typeIndex: 0,
				list: [],
				couponList: [],
			}
		},
		onShow() {
			this.getData(0);
		},
		methods:{
			getPhone() {
				const phone = uni.getStorageSync('phone')
				const phoneStr = `${phone.slice(0,3)}****${phone.slice(7,11)}`
				return phoneStr
			},
			getData(type) {
				let params = {
					receiveOpenId: uni.getStorageSync('wx_copenid')
				}
				getCoupon(params).then(res => {
					if (res.data.code == 200) {
						if(res.data.data && res.data.data.length > 0) {
							this.list = res.data.data.map(item => {
								return {
									...item,
									showMsg: false
								}
							})
							this.changeType(type)
						}
					}
				});
			},
			changeType(type) {
				if(!type) {
					this.couponList = this.list.filter(item => item.couponStatus == 2)
				}
				if(type==1) {
					this.couponList = this.list.filter(item => item.couponStatus == 3)
				}
			},
			handleIndex(item, index) {
				this.typeIndex = index
				this.changeType(index)
			}
		}
	}
</script>

<style lang="scss">
	.record {
		width: 100%;
		height: 100vh;
		overflow: hidden;
		background-color: #F6F7F9;
		.tips{
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			background-color: #ffffff;
			border-radius: 0 0 24rpx 24rpx;
			.tips-item{
				line-height: 90rpx;
				border-bottom: 4rpx solid #ffffff;
			}
			.active{
				color: #03C8BE;
				border-bottom: 4rpx solid #03C8BE;
			}
		}
		.list-container {
			padding: 20rpx 24rpx;
			height: calc(100vh - 90rpx);
			overflow-y: auto;
			.coupon-box{
				margin-bottom: 24rpx;
				position: relative;
				border-radius: 16rpx;
				background-color: #fff;
				opacity: .6;
			}
			.coupon-card {
				display: flex;
				flex-wrap: wrap;
				overflow: hidden;

				.left-content {
					flex: 1;
					padding: 30rpx;
					border-right: 2rpx dashed #eee;

					.coupon-title {
						font-size: 30rpx;
						font-weight: bold;
						color: #333;
						margin-bottom: 10rpx;
					}

					.coupon-time {
						font-size: 22rpx;
						color: #999;
						margin-bottom: 20rpx;
					}
					.coupon-msg{
						color: #333;
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-size: 24rpx;
						.icon-circle{
							color: #999;
							display: flex;
							align-items: center;
							justify-content: space-between;
							.circle{
								width: 0;
								height: 0;
								border-left: 10rpx solid transparent;
								border-right: 10rpx solid transparent;
								border-bottom: 16rpx solid #999;
								transform: rotate(180deg);
							}
							.default{
								width: 0;
								height: 0;
								border-left: 10rpx solid transparent;
								border-right: 10rpx solid transparent;
								border-bottom: 16rpx solid #999;
							}
						}
					}
				}

				.right-action {
					width: 220rpx;
					padding: 20rpx;
					display: flex;
					align-items: center;
					flex-direction: column;
					justify-content: center;
					background-color: #E8FBFA;

					.amount-box {
						color: #03C8BE;
						margin-bottom: 16rpx;

						.unit {
							font-size: 24rpx;
							margin-right: 4rpx;
						}

						.val {
							font-size: 50rpx;
							font-weight: bold;
						}
					}

					.action-btn {
						width: 140rpx;
						height: 54rpx;
						border-radius: 40rpx;
						font-size: 24rpx;
						display: flex;
						align-items: center;
						justify-content: center;

						&.active {
							background-color: #03C8BE;
							color: #fff;
						}

						&.disabled {
							background-color: transparent;
							border: 2rpx solid #03C8BE;
							color: #03C8BE;
						}
					}
				}
				.right-action-lose {
					width: 220rpx;
					padding: 20rpx;
					display: flex;
					align-items: center;
					flex-direction: column;
					justify-content: center;
					background-color: #F4F9F9;

					.amount-box {
						color: #BBBBBB;
						margin-bottom: 16rpx;

						.unit {
							font-size: 24rpx;
							margin-right: 4rpx;
						}

						.val {
							font-size: 50rpx;
							font-weight: bold;
						}
					}

					.action-btn {
						width: 140rpx;
						height: 54rpx;
						border-radius: 40rpx;
						font-size: 24rpx;
						display: flex;
						align-items: center;
						justify-content: center;

						&.active {
							border: 2rpx solid #BBBBBB;
							background-color: #ffffff;
							color: #BBBBBB;
						}

						&.disabled {
							background-color: transparent;
							border: 2rpx solid #BBBBBB;
							color: #BBBBBB;
						}
					}
				}
			}
			.coupon-message{
				border-top: 1rpx solid #EEEEEE;
				padding: 30rpx;
				color: #999;
				font-size: 24rpx;
			}
		}
	}
</style>
