<template>
	<view class="app">
		<view class="contents" v-if="list&&list.length>0">
			<view class="content" v-for="item in list">
				<!-- <image :src="item.src" mode=""></image> -->
				<u-image width="100%" height="355rpx" :src="item.src"></u-image>
				<view class="servename">
					{{item.prizeName?item.prizeName:''}}
				</view>
				<view class="PointsRecord">
					{{item.score?item.score:''}}积分
				</view>
				<view class="button" @tap="goexchange(item)">
					提交兑换申请
				</view>
			</view>
			<view style="width: 48%;" v-if="list.length%2!=0">
			</view>
		</view>
		<u-empty v-else mode="list" icon-size='220' text='暂无可兑换商品'></u-empty>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		prizelist,
		prizeExchangesave,
	} from '@/api/pagesB/PointsMall/index.js'
	import {
		getImgById
	} from '@/api/pagesB/getimg/index.js'
	export default {
		data() {
			return {
				query: {
					identity: uni.getStorageSync('userinfo').cardNo,
					pageNum: 1,
					pageSize: 10,
				},
				list: [],
				total: 0,
			};
		},
		onReady() { //更改导航栏文字
			if (uni.getStorageSync('region') == 2) {
				uni.setNavigationBarTitle({
					title: '东营健康银行'
				});
			} else {
				uni.setNavigationBarTitle({
					title: '积分兑换'
				});
			}
		},
		methods: {
			goexchange(item) {
				var obj = {
					identity: this.query.identity,
					prizeId: item.prizeId,
					source: 2
				}
				prizeExchangesave(obj).then(res => {
					if (res.code == 200) {
						uni.showModal({
							title: '兑换申请提示',
							content: '兑换申请已提交，机构正在审批中，如长时间未通过，请联系您的签约医生',
							showCancel: false,
							success: function(res) {}
						});
					} else {
						this.$refs.uToast.show({
							title: res.msg,
							type: 'error',
						})
					}
				})
			},
			info() {
				prizelist(this.query).then(res => {
					res.rows.forEach(e => {
						e.src = ''
					})
					this.list = [
						...this.list,
						...res.rows
					]
					this.list.forEach(async (e) => {
						if (e.src == '') {
							e.src = await this.getimg(e.prizeId);
						}
					});
					this.total = res.total
				})
			},
			async getimg(id) {
				try {
					const res = await getImgById(id);
					return res.data
				} catch (error) {
					return null;
				}
			},
		},
		onLoad() {
			this.info();
		},
		onReachBottom() { //上滑加载
			if (this.list.length >= this.total) {} else {
				this.query.pageNum++
				this.info();
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.query.pageNum = 1;
			this.info();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
	}
</script>

<style lang="scss">
	.app {
		::v-deep .u-empty {
			margin-top: 25vh !important;
		}

		.contents {
			padding-top: 10rpx;
			display: flex;
			width: 100%;
			flex-wrap: wrap;
			justify-content: space-evenly;
		}

		.content {
			height: 514rpx;
			background: #fff;
			width: 48%;
			position: relative;
			border-radius: 5rpx;
			margin: 0 0 20rpx 0;
			border: 1px solid #fff;

			.button {
				width: 150rpx;
				height: 50rpx;
				line-height: 50rpx;
				position: absolute;
				text-align: center;
				font-size: 22rpx;
				font-weight: 400;
				color: #fff;
				right: 3%;
				bottom: 5%;
				background: #26A888;
				border-radius: 5rpx;
			}

			.PointsRecord {
				padding-top: 20rpx;
				font-size: 22rpx;
				font-weight: 400;
				color: #26A888;
				padding-left: 3%;
			}

			.servename {
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 28rpx;
				font-weight: 400;
				color: #000000;
				padding-left: 3%;
			}

			::v-deep .u-image {
				width: 100%;
				height: 355rpx;
				border-radius: 5rpx;
			}
		}
	}
</style>