<template>
	<view :style="viewColor">
		<view class="navTabBox">
			<view class="longTab">
				<view class="tab-item" v-for="(item,index) in tabList" :key="index" :class="{'on':index == tabIndex-1}" @click="bindTab(index)"><text>{{item.title}}</text></view>
			</view>
		</view>
		<view class='coupon-list' v-if="couponsList.length">
			<view v-for='(item,index) in couponsList' :key="index">
				<view v-if="item.merchant && item.status==0" class="store-info acea-row row-between-wrapper">
					<view class="store_name acea-row row-between-wrapper">
						<image class="image" :src="item.merchant.mer_avatar"></image>
						<text class="name">{{item.merchant.mer_name}}</text>
					</view>
					<view v-if="hide_mer_status == 0 && item.status == 0" class="store" @click="goStore(item)">进店</view>
				</view>
				<view class='item acea-row row-center-wrapper'>
					<block v-if="item.status == 0">
						<view v-if="item.coupon.type == 13" class='money vip-coupon'>
              <view class="line1 coupon_value"><text class='num' style="font-size: 45rpx!important;">{{ item.brand ? item.brand.title : '逸路红' }}</text></view>
              <view class="pic-num">{{ item.shopMer ? '仅 ' + item.shopMer.mer_name + ' 可用' : '全门店可用' }}</view>
							<!--<view class="line1 coupon_value">￥<text class='num'>{{item.coupon_price}}</text></view>-->
							<!--<view class="pic-num">满{{ item.use_min_price }}元可用</view>-->
						</view>
						<view v-else class='money' :style="{ 'background-image': `url(${domain}/static/diy/couponBg${keyColor}.png)` }">
							<view class="line1 coupon_value">￥<text class='num'>{{item.coupon_price}}</text></view>
							<view class="pic-num">满{{ item.use_min_price }}元可用</view>
						</view>
					</block>
					<view v-else class='money moneyGray'>
            <block v-if="item.coupon.type == 13">
              <view class="line1 coupon_value"><text class='num'>{{ item.brand ? item.brand.title : '逸路红' }}</text></view>
              <view class="pic-num">已核销</view>
            </block>
            <block v-else>
              <view>￥<text class='num'>{{item.coupon_price}}</text></view>
              <view class="pic-num">满{{ item.use_min_price }}元可用</view>
            </block>
					</view>
					<view class='text'>
						<view class='condition line1'>
							<view class="line-title" :class="item.status === 0 ? 'bg-color-huic' : 'bg-color-check'" v-if="item.coupon && item.coupon.type === 0">店铺券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.coupon && item.coupon.type === 1">商品券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.coupon && item.coupon.type === 11">品类券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.coupon && item.coupon.type === 10">通用券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.coupon && item.coupon.type === 12">跨店券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.coupon && item.coupon.type === 13">核销券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else>商品券</view>
							<view class="line1 coupon-title">{{item.coupon_title}}</view>
						</view>
						<view class='data acea-row row-between-wrapper'>
							<view>{{item.start_time | timeYMD}}-{{item.end_time | timeYMD}}</view>
							<view v-if="item.status==0">
								<navigator :url="'/pages/columnGoods/goods_coupon_list/index?coupon_id='+item.coupon_id" class='bnt1 b-color' hover-class="none" v-if="item.coupon.type != 13">
								去使用
								</navigator>
								<view class='bnt1 b-color svip-btn' @click="getQRcode(item.write_code)" hover-class="none" v-else>
								去核销
								</view>
							</view>
							<view class='bnt1 gray' style="position: static;" v-if="item.status==1">已使用</view>
							<view class='bnt1 gray' style="position: static;" v-if="item.status==2">已过期</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class='noCommodity' v-if="!couponsList.length && loading==true">
			<view class='pictrue'>
				<image :src="`${domain}/static/images/noCoupon.png`"></image>
			</view>
		</view>
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
		<uni-popup ref="qrCodePopup" type="center">
		  <view class="qr-code-content">
		    <image class="image" :src="qrCode"></image>
		    <view class="close-qr-code" @click="closeQrCode()">关闭</view>
		  </view>
		</uni-popup>
	</view>
</template>

<script>

	import spread from "../../../libs/spread";

  let app = getApp();
	import {
		getUserCoupons
	} from '@/api/api.js';

	import { createQRcode } from '@/api/user.js';

	import {
		mapGetters
	} from "vuex";
	import authorize from '@/components/Authorize';
	import { configMap } from '@/utils';
	import { HTTP_REQUEST_URL } from '@/config/app';
	export default {
		components: {
			authorize
		},
		data() {
			return {
				domain: HTTP_REQUEST_URL,
				tabIndex:1,
				couponsList: [],
				loading: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false ,//是否隐藏授权
				isUsed: 0,
				tabList:[
					{
						title:'未使用'
					},
					{
						title:'已使用/已过期'
					}
				],
				limit:15,
				page:1,
				isScroll:true,
				moneyBg: '/static/images/couponBg',
				qrCode: '',
			};
		},
		computed: configMap(['hide_mer_status'], mapGetters(['isLogin','uid','viewColor','keyColor'])),
		filters: {
		  timeYMD: function (value) {
			if(value){
				var newDate=/\d{4}-\d{1,2}-\d{1,2}/g.exec(value)
				return newDate[0]
			}
		  }
		},
		onLoad(options) {
			if (this.isLogin) {
				this.getUseCoupons();
			} else {
				this.isAuto = true;
				this.isShowAuth = true
			}

      // 关系处理
      if(options.spread) spread(options.spread, this.isLogin)
		},
		mounted: function() {
		},
		methods: {
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.isShowAuth = false;
				// this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			bindTab(index){
				this.tabIndex = index+1
				this.page =1
				this.isScroll = true
				this.couponsList = []
				this.getUseCoupons()
			},
			goStore(item){
				if(this.hide_mer_status != 1 && item.status==0){
					uni.navigateTo({
						url:`/pages/store/home/index?id=${item.mer_id}`
					})
				}
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				let that = this;
				if(!this.isScroll) return
				getUserCoupons({
					statusTag: this.tabIndex,
					page:this.page,
					limit:this.limit
				}).then(res => {
					that.loading = true;
					that.couponsList = that.couponsList.concat(res.data.list)
					that.isScroll = that.couponsList.length<res.data.count
					that.page++
				})
			},
			/**
			 *生成核销券的二维码
			 */
			getQRcode(write_code)
			{
				// let that = this;
				createQRcode({
					write_code: write_code
				}).then(res => {
					console.log(res.data.qr_code)
					this.qrCode = res.data.qr_code
					this.$refs.qrCodePopup.open('center')
				}).catch(err => {
					this.$util.Tips({title: err});
				});
			},
			// 关闭二维码弹框
			closeQrCode() {
				this.$refs.qrCodePopup.close();
			},
		},
		onReachBottom() {
			this.getUseCoupons()
		},
    // #ifdef MP
    // 分享给好友
    onShareAppMessage () {
      let pages = getCurrentPages();
      let page = pages[pages.length - 1]
      let shareData = {
        title: '我的优惠券',
        path: page.$page.fullPath || '/' + page.route,
      };
      // 判断：用户是否登录  已经登录则添加分享人信息，未登录则正常分享
      if (this.isLogin) shareData.path = shareData.path + '?spread=' + this.uid;
      // 返回最终的分享配置信息
      return shareData
    },
    // 分享到朋友圈
    onShareTimeline() {
      let shareData = {
        title: '我的优惠券',
        query: {},
      };
      // 判断：用户是否登录  已经登录则添加分享人信息，未登录则正常分享
      if (this.isLogin) shareData.query.spread = this.uid;
      // 返回最终的分享配置信息
      return shareData
    },
    // #endif
	}
</script>

<style scoped lang="scss">
	.navTabBox{
		background: #fff;
		height: 90rpx;
	}
	.longTab{
		display: flex;
		text-align: center;
	}
	.tab-item{
		width: 50%;
		display: inline-block;
		line-height: 90rpx;
		text{
			position: relative;
			display: inline-block;
		}
	}
	.tab-item.on{
		color: var(--view-theme);
		text{
			&:after{
				content: "";
				display: inline-block;
				width: 90%;
				height: 3rpx;
				background-color: var(--view-theme);
				position: absolute;
				left: 5%;
				bottom: 0;
			}
		}
	}
	.b-color {
		background-color: var(--view-theme);
	}
	.pic-num {
		color: #ffffff;
		font-size: 20rpx;
	}

	.coupon-list .item .text .condition {
		display: flex;
		align-items: center;
	}

	.condition .line-title {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 90rpx;
		height: 40rpx !important;
		padding: 0 10rpx;
		-webkit-box-sizing: border-box;
		box-sizing: border-box;
		background: var(--view-minorColor);
		border: 1px solid var(--view-theme);
		opacity: 1;
		border-radius: 22rpx;
		font-size: 20rpx !important;
		color: var(--view-theme);
		margin-right: 12rpx;
	}
	.condition .coupon-title{
		width: 280rpx;
	}
	.store-info{
		background: #fff;
		padding: 18rpx 20rpx;
		position: relative;
		&::before,&::after{
			content: "";
			display: inline-block;
			width: 24rpx;
			height: 24rpx;
			background: #f5f5f5;
			border-radius: 100%;
			position: absolute;
			bottom: -12rpx;
			z-index: 10;
		}
		&::before{
			left: -10rpx;
		}
		&::after{
			right: -10rpx;
		}
		.image{
			width: 40rpx;
			height: 40rpx;
			border-radius: 100%;
			margin-right: 9rpx;
		}
		.name{
			color: #282828;
		}
		.store{
			color: var(--view-theme);
			font-size: 20rpx;
			padding: 2rpx 15rpx;
			border: 1px solid var(--view-theme);
			border-radius: 16rpx;
		}
	}
	.svip-btn{
		background-color: #E8C077;
	}
	.noCommodity{
		width: 100%;
		.pictrue{
			margin: 0 auto;
		}
	}
	.coupon_value{
		max-width: 220rpx;
	}
  // 二维码弹框
  .qr-code-content{
    width: 80vw;
    .image{
      width: 80vw;
      height: 80vw;
    }
    .close-qr-code{
      position: fixed;
      top: 35rpx;
      right: 70rpx;
      color: #FFFFFF;
      border: 2rpx solid #FFFFFF;
      height: 40rpx;
      line-height: 36rpx;
      padding: 0 20rpx;
      border-radius: 50rpx;
    }
  }
</style>
