<template>
	<view class="content">
		<view class="" style="height: 20rpx;"></view>
		<empty v-if="list.length === 0 && loaded"></empty>
		<view class="green-card-wrap" v-for="(gitem, gindex) in list" :key="gindex">
			<image :src="'../../static/img/green' + ((gitem.id + 1) % 5) + '.png'" mode="" class="card-bg"></image>
			<view class="card-info flex">
				<image :src="'../../static/img/ginfo' + ((gitem.id + 1) % 5) + '.png'" mode="" class="info-img"></image>
				<view class="info-detail">
					<view class="card-name">我的洒水壶</view>
					<!-- 1买 2赠 -->
					<view class="card-from">来源：{{ gitem.type == 1 ? '水滴兑换' : '赠送获得' }}</view>
					<view class="card-time">获得时间:{{ gitem.create_time }}</view>
				</view>
				<view class="card-btn-wrap flex" v-if="gitem.status == 0 && gitem.type == 1"><view class="card-btn" @click="openZs(gitem)">转赠</view></view>
			</view>
		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
		<uni-popup ref="popup" type="center">
			<view class="zs-wrap">
				<view class="zs-tit">赠送洒水壶</view>
				<view class="zs-uid flex">
					<view class="to-uid">赠送对象UID：</view>
					<input type="number" v-model="toUid" placeholder="请输入赠送对象UID" />
				</view>
				<view class="zs-btns">
					<view class="zs-btn" @click="closeZs">取消</view>
					<view class="zs-btn" style="color: #ff567f;" @click="giveGreen()">赠送</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import { getGreenCardList, giveGreen } from '@/api/user.js';
import empty from '@/components/empty';
export default {
	components: {
		empty
	},
	data() {
		return {
			zsItem: {},
			toUid: '',
			loaded: false,
			list: [],
			page: 1,
			limit: 10,
			loadingType: 'more'
		};
	},
	onLoad() {},
	onShow() {
		this.getGreenCardList();
	},
	onReachBottom() {
		this.getGreenCardList();
	},
	onReady() {},
	methods: {
		getGreenCardList() {
			let obj = this;
			if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
				return;
			}
			obj.loadingType = 'loading';
			getGreenCardList().then(res => {
				console.log(res);

				obj.list = obj.list.concat(res.data.data);
				obj.page++;
				if (obj.limi == res.data.data.length) {
					obj.loadingType = 'more';
				} else {
					obj.loadingType = 'noMore';
				}
				obj.loaded = true;
			});
		},
		giveGreen() {
			let obj = this;
			console.log(obj.zsItem);
			if (obj.toUid == '') {
				return obj.$api.msg('请输入赠送对象UID');
			}
			giveGreen({
				id: obj.zsItem.id,
				uid: obj.toUid
			}).then(res => {
				this.closeZs();
				uni.showToast({
					title: '赠送成功',
					duration: 2000,
					position: 'top'
				});
				let s = obj.list.indexOf(obj.zsItem);
				obj.list.splice(s, 1);
			});
		},
		closeZs() {
			this.$refs.popup.close();
		},
		openZs(item) {
			this.zsItem = item;
			this.$refs.popup.open();
		}
	}
};
</script>

<style lang="scss">
.green-card-wrap {
	width: 700rpx;
	height: 184rpx;
	margin: 0 auto 20rpx;
	position: relative;

	.card-bg {
		width: 700rpx;
		height: 184rpx;
	}

	.card-info {
		position: absolute;
		top: 0;
		left: 0;
		width: 700rpx;
		height: 184rpx;
		padding: 0 35rpx;

		.info-img {
			width: 112rpx;
			height: 112rpx;
		}

		.info-detail {
			flex-grow: 1;
			padding-left: 30rpx;
			color: #ffffff;
			font-size: 24rpx;
			font-weight: 500;

			.card-name {
				font-size: 36rpx;
				font-weight: bold;
			}

			.card-from {
			}

			.card-time {
			}
		}

		.card-btn-wrap {
			width: 132rpx;
			height: 100%;

			.card-btn {
				width: 132rpx;
				height: 53rpx;
				background: #ffffff;
				border-radius: 27rpx;
				font-size: 26rpx;
				font-weight: 500;
				text-align: center;
				line-height: 53rpx;
				color: #ff567f;
			}
		}
	}
}

.zs-wrap {
	width: 600rpx;
	height: 300rpx;
	background-color: #fff;
	border-radius: 20rpx;
	text-align: center;
	position: relative;

	.zs-tit {
		font-size: 32rpx;
		font-weight: bold;
		padding: 40rpx 0 50rpx;
	}

	.zs-uid {
		font-size: 28rpx;
		padding: 0 40rpx;

		.to-uid {
			flex-shrink: 0;
		}
	}

	.zs-btns {
		width: 100%;
		display: flex;
		position: absolute;
		bottom: 0;
		height: 80rpx;
		font-size: 32rpx;
		line-height: 80rpx;

		.zs-btn {
			width: 50%;
		}
	}
}
</style>
