<template>
	<view class="store">
		<view class="store_bg">
			<image src="../../static/store_bg.png" class="bg_img"></image>
			<view class="bg_con">
				<text>我的积分</text>
				<view>{{integral?integral:0}}</view>
			</view>
		</view>
		<view class="exchange">
			<view class="record flexB">
				<view @click="skipDetail(0)">
					<image src="../../static/gift_detail.png" class="gift_icon"></image>
					<text>礼品详情</text>
				</view>
				<view @click="skipDetail(1)">
					<image src="../../static/record.png" class="record_icon"></image>
					<text>兑换记录</text>
				</view>
			</view>
			<view class="exchange_con flexT" v-for="item in giftList" :key="item.id">
				<!-- <view class="change_icon">已兑换{{item.exchange_num}}</view> -->
				<image :src="item.imgurl" class="exchange_img" mode="aspectFill" @click="previewImg(item.imgurl)">
				</image>
				<view style="flex:1">
					<view class="exchange_name">{{ item.name }}</view>
					<view class="limit_box flexC">限量{{item.gift_num}}</view>
					<view class="flexB" style="width: 100%;">
						<view class="exchange_inte">
							<text>{{ item.integral }}</text>
							<text>积分</text>
						</view>
						<view class="exchange_btn flexC"
							@click="item.gift_num-item.exchange_num==0?noChange():skipDetail(2,item)"
							:class="item.gift_num-item.exchange_num==0?'disable_change':''">
							{{item.gift_num-item.exchange_num==0?'已兑完':'立即兑换'}}
						</view>
					</view>
				</view>
			</view>
			<view class="noData" v-if="giftList.length==0">
				<image src="../../../static/imgs/default/no_card.png" mode=""></image>
				<view>--暂无可兑换礼品--</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getInteGifts
	} from '@/apis/shop.js'
	export default {
		data() {
			return {
				giftList: [],
				integral: 0,
				is_exc: '' //是否能兑换 0不能 1能
			};
		},
		onShow() {
			this.getList()
		},
		methods: {
			previewImg(img) {
				uni.previewImage({
					current: `${img}`,
					urls: [`${img}`],
					success: res => {
						console.log(res);
					},
					fail: err => {
						console.log(err)
					}
				})
			},
			//已兑完
			noChange() {
				uni.showModal({
					content: '礼品已兑完，下次早点来~',
					showCancel: false
				})
			},
			//获取礼品列表
			getList() {
				getInteGifts().then(res => {
					if (res.code == 200) {
						const {
							integral,
							gifts,
							is_exc
						} = res.data;
						this.integral = integral;
						this.giftList = is_exc == 1 ? gifts : [];
						this.is_exc = is_exc;
					} else {
						uni.showModal({
							content: res.msg || '获取数据失败',
							showCancel: false
						})
					}
				})
			},
			skipDetail(type, item) {
				let url = '';
				let gift = encodeURIComponent(JSON.stringify(item))
				switch (type) {
					case 0:
						url = '../../../pages/shop-gift-detail/shop-gift-detail';
						break;
					case 1:
						url = '../../../pages/shop-gift-record/shop-gift-record';
						break;
					case 2:
						url = '../../../pages/shop-gift-exchange/shop-gift-exchange?gift=' + gift
						break;
				}
				uni.navigateTo({
					url
				})
			},
		}
	};
</script>

<style lang="scss" scoped>
	.store {
		width: 100%;
		min-height: 100vh;
		background: #f9f9fb;

		.store_bg {
			width: 100%;
			height: 414rpx;
			position: relative;

			.bg_img {
				width: 100%;
				height: 100%;
			}

			.bg_con {
				position: absolute;
				top: 80rpx;
				left: 315rpx;
				text-align: center;

				text,
				view {
					color: #fff;
				}

				text {
					font-size: 32rpx;
				}

				view {
					font-size: 56rpx;
					margin-top: 20rpx;
				}
			}



		}

		.exchange {
			margin-top: -100rpx;
			position: relative;

			.record {
				width: 690rpx;
				margin: 0 auto;

				>view {
					width: 330rpx;
					height: 168rpx;
					background: #fff;
					border-radius: 18rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-bottom: 30rpx;

					text {
						font-size: 32rpx;
						font-weight: bold;
						margin-left: 10rpx;
					}
				}

				.gift_icon {
					width: 94rpx;
					height: 98rpx;
				}

				.record_icon {
					width: 85rpx;
					height: 93rpx;
				}

			}

			.exchange_con {
				width: 690rpx;
				margin: 0 auto 30rpx;
				padding: 20rpx;
				box-sizing: border-box;
				background: #fff;
				border-radius: 24rpx;
				position: relative;

				.change_icon {
					position: absolute;
					top: 0;
					right: 0;
					width: 154rpx;
					text-align: center;
					height: 44rpx;
					line-height: 44rpx;
					background: linear-gradient(226deg, #FFE9C2 0%, #FFCD81 100%);
					opacity: 1;
					border-radius: 0 24rpx 0 24rpx;
					color: #CE7E06;
					font-weight: bold;
				}

				.exchange_img {
					width: 260rpx;
					height: 260rpx;
					border-radius: 16rpx;
					flex-shrink: 0;
					margin-right: 20rpx;
				}

				.exchange_name {
					font-size: 30rpx;
					font-weight: bold;
					margin-top: 20rpx;
				}

				.limit_box {
					width: 170rpx;
					background: #FFF4F3;
					border-radius: 8rpx;
					height: 48rpx;
					color: $base-color;
					margin: 35rpx 0;
				}

				.exchange_inte {
					text {
						color: $base-color;
					}

					text:first-child {
						font-size: 36rpx;
					}

					text:last-child {
						font-size: 28rpx;
					}
				}

				.exchange_btn {
					width: 160rpx;
					height: 64rpx;
					background: $base-line-bg;
					border-radius: 34rpx;
					color: #fff;
					font-size: 28rpx;
				}

				.disable_change {
					background: #F8F8F8;
					color: #CCCCCC;
				}
			}

		}
	}
</style>
