<template>
  <view class="cardList" :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="item" v-for="(x, i) in g" :key="i" >
		    <view class="ticket">
				<image mode="aspectFill" :src="x.CouponIco" style="width: 100%; height: 100%;border-top-left-radius: 5px;border-bottom-left-radius: 5px;" ></image>
		    </view>
		    <view class="detail">
		      <view class="coupond">
		        <view class="name">{{x.Name}}</view>
		    			  <view class="rule">{{x.UseTypeStr}}</view>
		        <view class="date" v-if="x.IndateType==1">领券后{{x.IndateDay}}天过期</view>
		        <view class="date" v-if="x.IndateType==2">{{x.StartDate.split(' ')[0].replace(/-/g,'.')+"-"+x.EndDate.split(' ')[0].replace(/-/g,'.')}}</view>
		      </view>
		      <view class="chosen" :style="{'background':mainColor}"  @click="receive(x,i)">
		        <Text>{{x.IsReceive==1?'已领取':'立即领取'}} </Text>
		      </view>
			  
		    </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,
	  },
      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.request2(
	  	{
	  		url: '/api/AppletEducation/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";
				}
	  		}
	  	}
	  );
    },
	receive(x,i){
		if(x.IsReceive==0){
			this.request2(
			{
				url: '/api/AppletEducation/GrantCoupon',
				data: {
					CouponId:x.CouponId
				}
			},
			  
			  (res) => {
				  if(res.resultCode==1){
				  	
				  	uni.showToast({
				  		title: res.message,
				  		icon: "none"
				  	});
					this.g[i].IsReceive=1;
				  }
				 
			  } 
			);
		}else{
			uni.showToast({
				title: "已领取过学习卡",
				icon: "none"
			});
		}
	},
    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>
.cardList {
  width: 100%;
  height: 100%;
  background: #FFF;
  position: relative;
}

.cardList .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;
}
.cardList .details{
	padding: 15px;
	display: flex;
	flex-direction: column;
	position: relative;
}
.cardList  .item {
  display: flex;
  margin-bottom: 10px;
  align-items: flex-start;
  background: #fff;
  border-radius: 5px;
  box-shadow:0 4px 6px 1px rgba(76, 76, 76, 0.1);
}
.cardList .details .item .ticket {
	width: 104px;
	height: 78px;
}

.cardList .details .item .detail {
  width: 1px;
  flex: 1;
  background: #fff;
  padding: 10px;
  display: flex;
  align-items: center;
}
.cardList .details .item .detail .coupond {
  flex: 1;
}
.cardList .details .item .detail .coupond .name {
  font-size: 13px;
  font-weight: 600;
  color: #000;
  overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
}
.cardList .details .item .detail .coupond .date {
  margin-top: 5px;
  font-size: 11px;
  color: #777;
}
.cardList .details .item .detail .coupond .rule{
    font-size: 11px;
    color:#777;
    margin-top: 5px;
	overflow: hidden;
	  text-overflow: ellipsis;
	  display: -webkit-box;
	  -webkit-line-clamp: 1;
	  -webkit-box-orient: vertical;
}
.cardList .details .item .detail .chosen {
  width: 60px;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  border-radius: 4px;
  color: #FFF;
  
}










</style>
