<template>
  <view class="studycard" :style="{ height: contentHeight }">
    <u-tabs
      :list="list"
      :is-scroll="false"
      :current="msg.UseState"
      @change="change"
      :active-color="mainColor"
    ></u-tabs>
    <u-empty v-if="g.length == 0" text="暂无相关学习卡" mode="coupon"></u-empty>
		
    <view
      v-if="g.length > 0"
      style="
        height: calc(100vh - 50px);
        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">{{x.StartDate.split(' ')[0].replace(/-/g,'.')+"-"+x.EndDate.split(' ')[0].replace(/-/g,'.')}}</view>
		      </view>
		      <view class="chosen" :style="{'color':mainColor,'border':'0.5px solid'+mainColor}" @click="gouseUrl(x)" v-if="msg.UseState==0">
		        去使用
		      </view>
			  <image v-if="msg.UseState==1" mode="aspectFill" class="imgs" src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/studycardsy.png" ></image>
			  <image v-if="msg.UseState==2" mode="aspectFill" class="imgs" src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/studycardgq.png"  ></image>
			  
		    </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 style="width: 100%;height: 50px;background: #FFFFFF;display: flex;align-items: center;justify-content: center;position: absolute;left: 0;bottom: 0;border-top: 1px solid #E1E1E1;" @click="goUrl">
			  <Text>去领取学习卡</Text>
	</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:{},
      list: [
        { name: "未使用" },
        { name: "已使用" },
        { name: "已过期" },
      ],
      mainColor: "",
      contentHeight: 0,
      
	  msg:{
		  pageIndex: 1,
		  pageSize:10,
		  UseState: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: {
    change(index) {
		
      this.msg.UseState = index;
      this.msg.pageIndex = 1;
      this.g = [];
      this.loading = true;
      this.init();
    },
    init() {
      
	  this.request2(
	  	{
	  		url: '/api/AppletEducation/GetMemberCouponPageList',
	  		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";
				}
	  		}
	  	}
	  );
    },
    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()
	},
	gouseUrl(g){
		let data = []
		if(g.UseType==1 || g.UseType==2){
			g.ProductList.forEach(x=>{
				data.push(x.ProductId)
			})
			let id = data.toString()
			uni.redirectTo({ url: '/pages/school/courseList?coupon_id='+ id+'&UseType='+g.UseType });
		}else{
			uni.redirectTo({ url: '/pages/school/courseList'});
		}
	},
	goUrl(){
		uni.navigateTo({
		  url: "/pages/school/personal/cardList"
		});
	}
  },
};
</script>
<style>
.studycard {
  width: 100%;
  height: 100%;
  background: #FFF;
  position: relative;
}

.studycard .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;
}
.studycard .details{
	padding: 15px;
	display: flex;
	flex-direction: column;
	
}
.studycard  .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);
}
.studycard .details .item .ticket {
	width: 104px;
	height: 78px;
}

.studycard .details .item .detail {
  width: 1px;
  flex: 1;
  background: #fff;
  padding: 10px;
  display: flex;
  align-items: center;
  position: relative;
}
.studycard .details .item .detail .coupond {
  flex: 1;
}
.studycard .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;
}
.studycard .details .item .detail .coupond .date {
  margin-top: 5px;
  font-size: 11px;
  color: #777;
}
.studycard .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;
}
.studycard .details .item .detail .chosen {
  width: 55px;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  border-radius: 4px;
}

.studycard .details .imgs{
	width: 55px; height: 52px;
	position: absolute;top: 0px;
	right: 0px;
}









</style>
