<template>
	<view >
		<ujp-navbar title="我的优惠券">
			<view class="exchange oldTextjp2" oldstyle="font-size: 16px;" @click="toConversion">兑换</view></ujp-navbar>
		
		<u-alert-tips size="12" :show-icon="true"  type="warning"
		 description="同类型优惠券只显示一张"></u-alert-tips>
		
		
		<view class="list-item"  
		
		v-for="(item,index) in myCouponList"
		:style="item.chooseStatus == '0' ? 'opacity: 0.5':'' "
		 @click="ckselectObj(item)"
		 :key="item.id">
			
			<view 
			:style="item.chooseStatus == '0' ? 'background-color:#9F9F9F':'' "
			
			class="voucher" >优惠券</view>
		
			
			
			<view class="checked" v-if="selectObj.id==item.id">
				<img src="@/assets/static/img/选中.png" alt="">
			</view>
			
			 
			<view class="item-left">
				<view class="title oldTextjp" oldstyle="font-size: 18px;">
					{{item.couponName}}
				</view>
				<view class="useful-life oldTextjp2" oldstyle="font-size: 16px;">
					有效期至{{item.endDate&&item.endDate.slice(0,10)}}{{" "}}
					<span class="item-left-explain" @click.stop="gotoUrl('pages/user/coupon/couponDetails?id=' + item.id)">使用说明</span>
				</view>
			</view>
			<view class="item-right">
				
				
					<view class="price2"
					 :style="item.chooseStatus == '0' ? 'color:#666':'' "
					 
					 v-if="item.classify == '2'"><span style="font-size: 12px;" v-if="item.value">服务费</span>
					 <template v-if="item.value==0">
					 免服务费
					 </template>
					 <template v-else>
					 	{{item.value}}<span style="font-size: 12px;">折</span>
					 </template>
					 
					 </view>
				 <view class="price"
				  :style="item.chooseStatus == '0' ? 'color:#666':'' "
				 
				 v-else >{{item.value.toFixed(2)}}<span style="font-size: 12px;">元</span></view>
				 
				 
				 
				  <view class="contidion oldTextjp2" oldstyle="font-size: 16px;">
				  	{{item.threshold == 0 ? '无门槛' : '满'+item.threshold.toFixed(2)+'元可用'}}
				  </view>
			</view>
		</view>
		<u-divider  v-if="myCouponList.length > 0" style="margin-top: 10px;background-color: #F2F4F4;">已经到底了</u-divider>
		<view   v-if="myCouponList.length > 0"  style="padding-bottom: 80px;" ></view>
		<view class="carNone" v-if="myCouponList.length == 0">
			<img src="@/assets/static/img/暂无数据-缺省页.png" alt="">
			<p>暂无优惠券</p>
		</view>
		
	<view class="bottom">
		<view class="text">
			
			<view class="num"  v-show="selectObj.id">
				 
				<view class="price" v-if="selectObj.classify == '2'">
					<template v-if="selectObj.value==0">
						服务费全免
					</template>
					<template v-else>
						服务费{{selectObj.value}}折
					</template>
				</view>
				<view class="price" v-else  >抵扣{{selectObj.value}}元</view>
								 
			</view>
		</view>
		<u-button 
		:style="selectObj.id  ?'' : 'background-color:#9F9F9F'"
		@click="submitBtn"
		class="confirm" >
			<span>确定</span>
		</u-button>
	</view>
	
	
	
	</view>
</template>

<script>
	//import * as userApi from '@/apis/user.js'
	import * as userApi from '@/apis/chargeProcess.js'
	
	export default{
		data(){
			return{
				selectObj:{},
				amount:0,
				uuid:0,
				myCouponList: [],
				pageIndex: 1,
				recordsTotal: 0,
				stationId:"",
				userCard:null,
			}
		},
		onReachBottom() {
			if (this.myCouponList.length < this.recordsTotal) {			
				this.myLoadmore();
			}
		},
		onLoad(op) {
			var plus=this.carhelp.getPersonInfoPlus()
			if(plus){
				this.userCard=plus.userCard;
			}
			if(op.uuid){
				this.amount=op.amount
				this.uuid=op.uuid
				this.stationId=op.stationId;
			}
		
		},
		onShow() {
				this.getCouponList();
		},
		computed:{
			userCardBool(){
				if(this.userCard){
					var date=new Date().getTime()
					var reg=new RegExp('-','gi')
					var str=this.userCard.endTime;
					//console.log(str.replace(reg,'/'))
					var date2=new Date(str.replace(reg,'/')).getTime();
					if(date<date2){
						return true;
					}
				}
				return false
			}
		},
		methods:{
			submitBtn(){
				if(this.selectObj.id){
					this.carhelp.set("chooseCoupons",{
						uuid:this.uuid,
						selectObj:this.selectObj
					})
					uni.navigateBack({
						
					})
				}
			},
			ckselectObj(item){
				if(item.chooseStatus==1){
					this.selectObj=item;
				}else{
					uni.showToast({
						title:"不满足使用条件"
					})
				}
				
			},
			toConversion() {
				uni.navigateTo({
					url: '/pages/user/coupon/conversion'
				})
			},
			getCouponList(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.myCouponList = [];
					this.pageIndex = 1;
				}
				var obj={
					amount:this.amount==0?'':this.amount,
					stationId:this.stationId
				}
				if(this.userCardBool){
					obj.userCardId=this.userCard.id
				}
	
				userApi.useCoupon(obj).then((res) => {
					uni.hideLoading();
					
					this.myCouponList =res.data.userCouponList
					this.recordsTotal = res.data.recordsTotal;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getCouponList()
			},
		}
	}
</script>

<style lang="scss">
	.carNone{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		img{
			width: 100%;
			height: 100%;
		}
		p{
			margin-top: -60px;
		}
	}
	.exchange{
		margin-left: 85.3%;
		color: rgba(119, 119, 119, 100);
		font-size: 12px
	}
	.list-item{
		width: 91.4%;
		display: flex;
		justify-content: space-between;
		background-color: #fff;
		margin: 12px auto 0;
		border-radius: 8px;
		position: relative;
		.voucher{
			width: 60px;
			height: 20px;
			line-height: 20px;
			border-radius: 8px 0px 8px 0px;
			background-color: rgba(0, 185, 98, 100);
			color: rgba(255, 255, 255, 100);
			font-size: 12px;
			text-align: center;
			position: absolute;
			top: 0;
			left: 0;
		}
		.checked{
			width: 28px;
			height: 28px;
			position: absolute;
			top: 0;
			right: 0;
			img{
				width: 100%;
				height: 100%;
			}
		}
		.item-left{
			padding: 24px 0 20px 10px;
			.title{
				line-height: 25px;
				color: rgba(16, 16, 16, 100);
				font-size: 18px;
				font-weight: 600;
			}
			.useful-life{
				line-height: 22px;
				color: rgba(51, 51, 51, 100);
				font-size: 14px;
				margin-top: 1px;
			}
			.item-left-explain {
				color: rgba(0, 185, 98, 100);
				font-size: 14px;
			}
		}
		.item-right{
			padding: 19px 10px 18px 0;
			
			.price{
				// height: 38px;
				color: rgba(0, 185, 98, 100);
				font-size: 32px;
				text-align: right;
			}
			.price2{
				// height: 38px;
				color: rgba(0, 185, 98, 100);
				font-size: 20px;
				text-align: right;
				white-space: pre;
			}
			.contidion{
				// height: 22px;
				color: rgba(51, 51, 51, 100);
				font-size: 16px;
				text-align: right;
				margin-top: 4px;
			}
		}
	}

  .have-used{
	  
	  width: 54px;
	  height: 54px;
	  position: absolute;
	  top: -12px;
	 right: 0;
	  img{
		  width: 100%;
		  height: 100%;
	  }
  }
.bottom{
	position: fixed;
	bottom: 0;
	right: 0;
	width: 100%;
	height: 64px;
	line-height: 64px;
	background-color: #fff;
	padding: 0 16px;
	display: flex;
	justify-content: space-between;
	.text{
		height: 20px;
		color: rgba(16, 16, 16, 100);
		font-size: 18px;
		display: flex;
		.num{
			color: orangered;
			font-size: 18px
		}
	}
	.confirm{
		width: 100px;
		height: 40px;
		border-radius: 50px;
		background-color: rgba(0, 185, 98, 100);
		color: rgba(255, 255, 255, 100);
		font-size: 18px;
		text-align: center;
		margin: auto 10px ;
	}
}

</style>