<template>
  <view class="receivecard" :style="{ height: contentHeight }">
   
    <u-empty v-if="g.length == 0" text="暂无相关套餐卡" mode="coupon"></u-empty>
		
    <view
      v-if="g.length > 0"
      style="
        height: calc(100vh);
        width: calc(100vw);
        overflow: hidden;
		padding-bottom: 50px;
      "
    >
      <scroll-view
        :scroll-y="true"
        :enable-back-to-top="true"
        :enable-flex="true"
        @scrolltolower="lower"
        :style="{ height: '100%' }"
      >
        <view class="details">
        	<view class="cd-t-box" v-for="(x, i) in g" :key="i" @click="gocoupon(x)">
        		<view style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;">
        			<text style="font-size: 12px;color: rgba(239, 220, 203, 0.6);">{{x.Name}}</text>
        			<view class="receivebtn" @click.stop="receive(x)">{{x.IsReceive>0?'去使用':'立即领取'}}</view>
        		</view>
        		<text  class="cd-tt" >{{x.Describe}}</text>
        		<text style="font-size: 13px;">核销次数:{{x.TotalNum}}次</text>
        		<view class="setmeal-pl" style="left: -12px;"></view>
        		<view class="setmeal-pl" style="right: -12px;"> </view>
        	</view>
        	  
        </view>
     
		<u-loadmore
		  :status="status"
		  :load-text="loadText"
		  :font-size="24"
		  :margin-top="20"
		  :margin-bottom="20"
		  bg-color="#FFF"
		/>
      </scroll-view>
    </view>
	
    <!-- 加载中 -->
    <view class="loading" v-if="loading">
      <u-loading mode="flower" size="48">></u-loading>
      <Text style="color: #fff; margin-top: 10rpx;">加载中...</Text>
    </view>
    <!-- 取消提示 -->
    <u-toast ref="uToast" />
   <auth v-if="showAuth" @changeuserinfo="reloadUserinfo" @gbAuth= 'gbAuth'></auth>
  </view>
</template>

<script>
import auth from "@/components/auth/index.vue";
export default {
  components: {
    auth
  },
  data() {
    return {
      pageTitle: "套餐卡中心",
      showAuth:false,
	  u:{},
     
      mainColor: "",
      contentHeight: 0,
	  msg:{
		  pageIndex: 1,
		  pageSize:10,
		  SmallShopId:0,
		  StoreId:0,
	  },
      page_count: 1,
      g: [],
      loading: false,
	  status: "loadmore",
	  loadText: {
	    loadmore: "轻轻上拉，加载更多",
	    loading: "努力加载中",
	    nomore: "没有更多了",
	  },
    };
  },
  created() {
    this.contentHeight = this.$utils.calcContentHeight(-40) + "px";
    this.mainColor = this.$uiConfig.mainColor;
  },
  mounted() {
    let currentPages = getCurrentPages();
    let u = "/" + currentPages[currentPages.length - 1].route;
    let pages = wx.getStorageSync("basedata")
      ? wx.getStorageSync("basedata").bar_title
      : [];
    pages.forEach((x) => {
      if (x.value == u) {
        this.pageTitle = x.new_name ? x.new_name : x.name;
      }
    });
    uni.setNavigationBarTitle({
      title: this.pageTitle,
    });
  },
  onLoad: function (option) {    
    this.u = uni.getStorageSync("mall_UserInfo");
    if (!this.u) {
      this.u = {
    	nickName: "未登录",
    	avatarUrl: ""
      };
      this.showAuth = true;
    }	else{
    	this.init();
    }
  },
  methods: {
    
    init() {
      this.msg.SmallShopId = this.u.UserSmallShopId
	  this.msg.StoreId = uni.getStorageSync("storeId")?uni.getStorageSync("storeId").storeId:0;
	  this.request2(
	  	{
	  		url: '/api/AppletStores/GetAllCouponPageList',
	  		data: this.msg
	  	},
	  	res => {
	  		if(res.resultCode==1){
	  				this.loading = false;
	  				this.g = this.g.concat(res.data.pageData);
					 this.page_count = res.data.pageCount;
					 if (this.page_count == 1) {
					   this.status = "nomore";
				}
	  		}
	  	}
	  );
    },
	gocoupon(item){//优惠券详情
		uni.navigateTo({
			url: "/pages/reserve/personal/couponDetail?ID="+item.CouponId,
		})
	},
	receive(x,i){
		if(x.IsReceive==0){
			this.request2(
			{
				url: '/api/AppletStores/GrantCoupon',
				data: {
					CouponId:x.CouponId
				}
			},
			  
			  (res) => {
				  if(res.resultCode==1){
				  	
				  	uni.showToast({
				  		title: res.message,
				  		icon: "none"
				  	});
					this.g[i].IsReceive=1;
				  }
				 
			  } 
			);
		}else{
			let data = []
			let g =x
			if(g.UseType==1 || g.UseType==2){
				g.ProductList.forEach(x=>{
					data.push(x.ProductId)
				})
				let id = data.toString()
				uni.redirectTo({ url: '/pages/goods/list?coupon_id='+ id+'&UseType='+g.UseType });
			}else{
				uni.redirectTo({ url: '/pages/goods/list'});
			}
		}
	},
    lower(e) {
      if (this.msg.pageIndex < this.page_count) {
        this.msg.pageIndex++;
		this.status = "loading";
        this.init();
      } else {
        
		this.status = "nomore";
      }
    },
	reloadUserinfo() {
	  this.u = uni.getStorageSync("mall_UserInfo");
	  // this.showAuth=false;
	  this.init();
	},
	//关闭登录窗口
	gbAuth(){
		uni.navigateBack()
	},	
  },
};
</script>
<style>
.receivecard {
  width: 100%;
  height: 100%;
  background: #FFF;
  position: relative;
}

.receivecard .loading {
  width: 180rpx;
  height: 180rpx;
  background: #000000;
  opacity: 0.7;
  border-radius: 10rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: fixed;
  left: 50%;
  top: 30%;
  margin-left: -100rpx;
  z-index: 999;
}
.receivecard .details{
	padding: 15px;
	display: flex;
	flex-direction: column;
	position: relative;
	
}
.receivecard .cd-t-box{
		width: 100%;
		height: 115px;
		background-image: url('https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/cpoun.png');
		background-repeat: no-repeat;
		background-size: cover ;
		padding:15px 20px;
		color:#EFDCCB ;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		border-radius: 6px;
		position: relative;
		box-shadow:0 4px 10px 1px #D2D2D2;
		margin-bottom: 15px;
	}
	.receivecard .cd-tt{
		width: 100%;
		display: inline-block;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		font-size: 15px;
		
	}
	.receivecard .setmeal-pl{
		position: absolute;
		top:45.5px;
		width: 24px;
		height: 24px;
		border-radius: 12px;
		background: #FFF;
		
	}
	.receivecard .receivebtn{
		height: 25px;
		width: 60px;
		font-size: 11px;
		color: #221818;
		border-radius: 4px;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #FFF;
	}








</style>
