<template>
  <!-- #ifdef H5 -->
	<view>
		<zj-page-layout
			:hasFooter="true"
			:isScroll="true"
			:refresherTriggered="refresherTriggered"
			@refresherrefresh="refresherrefresh">
			<template slot="header">
				<view class="tab-container">
					<u-tabs
						:scrollable="false"
						:list="tabList"
						:current="tabCurrent"
						@click="changeTab"
						lineColor="#3D8FFD"
						:activeStyle="{
							color: '#3D8FFD'
						}"
						:inactiveStyle="{
							color: '#666666'
						}"
						itemStyle="padding-left: 0; padding-right: 0; height: 88rpx;">
					</u-tabs>
				</view>
			</template>

			<view class="list-container">
				<block v-for="(item, index) in dataList" :key='index'>
					<view class="item">
						<view class="bg">
							<image src="@/static/mine/coupon/bg_1.png" v-if="tabCurrent == 0"></image>
							<image src="@/static/mine/coupon/bg_0.png" v-if="tabCurrent != 0"></image>
						</view>
						<view class="content">
							<view class="left">
								<view class="price">{{item.couponValue}}<text>元</text></view>
								<view class="text" v-if="item.couponType == 'SATISFY'">满{{item.orderAmount}}可用</view>
							</view>
							<view class="right">
								<view class="main">
									<view class="row1 ellipsis-2" :class="tabCurrent == 0 ? '':'width'">{{item.couponName}}</view>
									<view class="row2">
										<view>使用时间：</view>
										<view>{{item.activeStartTime | dateToYYmmdd2}}-{{item.activeEndTime | dateToYYmmdd2}}</view>
									</view>
								</view>
								<view class="btn-group" v-if="tabCurrent == 0">
									<view class="button white" @tap="toUseCoupon">去使用</view>
									<view class="button red" @tap="shareCoupon(item.id)" v-if="userInfo.type == 'SERVICE' && item.transferType">
										<text class="iconfont icon-code"></text><text>分享</text>
									</view>
								</view>
								<view class="tag" v-if="tabCurrent != 0">
									<image src="@/static/mine/coupon/status_1.png" v-if="tabCurrent == 1"></image>
									<image src="@/static/mine/coupon/status_2.png" v-if="tabCurrent == 2"></image>
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>

			<Loading :loadStatus="loadStatus" :dataList="dataList" />

		</zj-page-layout>

		<u-popup
			:round="10"
			mode="center"
			:closeable="true"
			:show="isShareDialog"
			@close="isShareDialog = false">
			<view class="share-dialog">
				<view class="content">
					<image src="@/static/mine/coupon/share.png" class="bg"></image>
					<image :src="shareDetail.qrcode" class="code" @longpress="handleLongpress"></image>
					<view class="logo">
						<image :src="configInfo.minLogo2" mode="heightFix"></image>
					</view>
					<view class="tips" v-if="shareDetail.leftShareTimes > 0 && shareDetail.qrcode">长按二维码保存到手机</view>
					<view class="noData" v-if="shareDetail.leftShareTimes <= 0">
						<image src="@/static/mine/coupon/noData.png"></image>
						<view>已超过可分享次数</view>
						<view>该优惠券不可再分享</view>
					</view>
					<view class="text">可分享次数：{{shareDetail.leftShareTimes}}/{{shareDetail.shareTimes}}</view>
				</view>
			</view>
		</u-popup>
	</view>
  <!-- #endif -->
  <!-- #ifndef H5 -->
  <web-view :src="webViewHref(`/packageMine/pages/coupon/list`, pam,crossPagePam)" @message="crossPage.$listener"></web-view>
  <!-- #endif -->
</template>

<script>
  // #ifdef H5
	export default {
		data() {
			return {
				configInfo: {},
        userInfo: {},
				count: {}, // 优惠券数量统计
				tabList: [
					{name: '未使用', value: 0},
					{name: '已使用', value: 1},
					{name: '已过期', value: 2},
				],
				tabCurrent: 0, // 当前选择值
				dataList: [], // 优惠券列表
				pageNum: 1,
				loadStatus: 0,
				refresherTriggered: false, // 下拉刷新状态

				isShareDialog: false,
				shareDetail: {},
			}
		},

		async onLoad() {
      this.configInfo = await this.$getConfigInfo();
      this.userInfo = await this.$getUserInfo();
			this.getCount();
			this.getList();
		},

		methods: {
			getCount() {
				this.$api.get('/coupon/count', {
					userId: this.$store.state.user.userId
				}).then(res => {
					this.count = res.data;
				})
			},

			getList() {
				this.loadStatus = 1;
				this.$api.get('/coupon/list/page', {
          pageNum: this.pageNum,
          pageSize: 10,
          type: this.tabCurrent,
          userId: this.$store.state.user.userId
				}).then(res => {
					this.loadStatus = 0;
					let list = res.data.records;
					if (list.length < 10) {
					  this.loadStatus = 2;
					}
					this.dataList = this.dataList.concat(list);
				}).catch(() => {
					this.loadStatus = 2;
				}).finally(res => {
					this.refresherTriggered = false;
				})
			},

			// 滚动到底部
			scrolltolower(e) {
				if (this.loadStatus === 0) {
					this.pageNum++;
					this.getList();
				}
			},

			// 触发下拉刷新
			async refresherrefresh(e) {
				this.refresherTriggered = true;
				this.refreshList();
			},

			refreshList() {
				this.dataList = [];
				this.pageNum = 1;
				this.getList();
			},

			changeTab(e) {
				this.tabCurrent = e.value;
				this.refreshList();
			},

			// 去使用优惠券
			toUseCoupon() {
				this.$navToPage({
				  url: '/packageGoods/pages/index'
				});
			},

			// 分享优惠券
			shareCoupon(id) {
				this.$api.get('/coupon/transfer/qrcode', {
          userId: this.$store.state.user.userId,
          userCouponId: id
				}).then(res => {
					this.shareDetail = res.data;
					this.isShareDialog = true;
				})
			},

			handleLongpress() {
				uni.showActionSheet({
          itemList: ['保存二维码'],
          success: res => {
          if(res.tapIndex == 0) {
            uni.downloadFile({
              url: this.shareDetail.qrcode,
              success: res => {
                if (res.statusCode === 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => {
                      this.$successToast('保存成功');
                    }
                  });
                }
              }
            });
          }
          },
          fail: res => {
            console.log(res.errMsg);
          }
				});
			}
		}
	}
  // #endif
  // #ifndef H5
  export default {
    data() {
      return {
        pam: {},
      }
    },

    onLoad(pam) {
      this.pam = pam;
    },
  }
  // #endif
</script>

<style lang="scss">
	.tab-container {
		background: #FFFFFF;
	}
	.list-container {
		padding: 20rpx 20rpx 40rpx;
		.item {
			position: relative;
			margin-bottom: 20rpx;
			.bg {
				image {
					width: 710rpx;
					height: 160rpx;
					display: block;
				}
			}
			.content {
				position: absolute;
				left: 0;
				top: 0;
				width: 710rpx;
				height: 160rpx;
				display: flex;
				align-items: center;
				.left {
					width: 240rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					.price {
						font-size: 60rpx;
						color: #FFFFFF;
						text {
							font-size: 28rpx;
							margin-top: 20rpx;
						}
					}
					.text {
						color: #FFFFFF;
						font-size: 28rpx;
					}
				}
				.right {
					display: flex;
					justify-content: space-between;
					padding: 15rpx 20rpx;
					width: 470rpx;
					height: 160rpx;
					box-sizing: border-box;
					.main {
						width: 300rpx;
						height: 130rpx;
						box-sizing: border-box;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.row1 {
							font-size: 28rpx;
							line-height: 32rpx;
							&.width{
								width: 330rpx;
							}
						}
						.row2 {
							font-size: 24rpx;
							color: #999999;
							line-height: 28rpx;
						}
					}
					.btn-group {
						display: flex;
						flex-direction: column;
						justify-content: center;
						padding-right: 8rpx;
						.button {
							width: 100rpx;
							height: 40rpx;
							border-radius: 40rpx;
							font-size: 24rpx;
							flex-shrink: 0;
							display: flex;
							align-items: center;
							justify-content: center;
							.iconfont {
								font-size: 24rpx;
								margin-right: 4rpx;
							}
							&.white {
								color: #FF3F42;
								border: 1px solid #FF3F42;
							}
							&.red {
								color: #FFFFFF;
								border: 1px solid #FE781F;
								background: #FE781F;
							}
							&:nth-child(2) {
								margin-top: 20rpx;
							}
						}
					}
					.tag {
						image {
							width: 80rpx;
							height: 80rpx;
							display: block;
						}
					}
				}
			}
		}
	}
	.bottom-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		height: 100rpx;
		.button {
			width: 100%;
			height: 68rpx;
			line-height: 68rpx;
			border-radius: 68rpx;
			text-align: center;
			font-size: 28rpx;
			color: #666666;
			border: 1px solid #B0B0B0;
		}
	}
	.share-dialog {
		.content {
			position: relative;
			.bg {
				width: 640rpx;
				height: 1000rpx;
				display: block;
			}
			.close {
				position: absolute;
				right: 20rpx;
				top: 20rpx;
				width: 30rpx;
				height: 30rpx;
				display: block;
			}
			.code {
				position: absolute;
				top: 300rpx;
				left: 120rpx;
				width: 400rpx;
				height: 400rpx;
				display: block;
				z-index: 9;
			}
			.logo {
				width: 100%;
				position: absolute;
				bottom: 60rpx;
				left: 0;
				display: flex;
				justify-content: center;
				image {
					height: 60rpx;
					display: block;
				}
			}
			.tips {
				position: absolute;
				left: 0;
				top: 700rpx;
				line-height: 50rpx;
				font-size: 24rpx;
				color: #f5f5f5;
				width: 640rpx;
				text-align: center;
			}
			.text {
				position: absolute;
				left: 0;
				bottom: 0;
				line-height: 60rpx;
				font-size: 24rpx;
				color: #666666;
				width: 640rpx;
				text-align: center;
			}
			.noData {
				position: absolute;
				top: 300rpx;
				left: 120rpx;
				width: 400rpx;
				height: 400rpx;
				display: block;
				z-index: 10;
				background: #FFFFFF;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				image {
					width: 197rpx;
					height: 146rpx;
					display: block;
					margin-bottom: 20rpx;
				}
				view {
					font-size: 28rpx;
					color: #333333;
					line-height: 48rpx;
				}
			}
		}

	}
</style>
