<template>
	<view>
		<customNav :shownav="true" :title="$t('index.hongbaokq')" titledetl=""></customNav>
		<view class="content contentInRowS" style="margin-top: 36rpx;height: 80rpx;">
			<view style="padding-left: 10rpx;" :class="cellindex==0?'cellsele':'cellNom'" @tap="cellSelect(0)">{{$t("hongbaokq.dailingqu")}}</view>
			<view :class="cellindex==1?'cellsele':'cellNom'" @tap="cellSelect(1)">{{$t("hongbaokq.daishiyong")}}</view>
			<view style="padding-right: 10rpx;" :class="cellindex==2?'cellsele':'cellNom'" @tap="cellSelect(2)">{{$t("hongbaokq.yiguoqi")}}</view>
		</view>
		<view v-if="cellindex==0">
			<view v-for="(item,index) in kaquanList"  class="content" style="margin-top: 20rpx;">
				<view class="contentColumnC" style="padding-top: 16rpx;">
					<view class="contentInRowC">
						<text :class="item.type==0?'pingtName':'shangjiaName'">{{item.name}}</text>
					</view>
					<text style="color:#00A6FF;font-size: 26rpx;">{{$t('hongbaokq.youxiaoqi')}}{{item.validity}}{{$t('hongbaokq.tian')}}</text>
				</view>
				<view class="contentColumn">
					<text v-if="item.tiaojian==null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{$t('hongbaokq.wumenkan')}}</text>
					<text v-if="item.tiaojian!=null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{mankeyong(item.tiaojian)}}</text>
					<text class="" style="font-size:28rpx;margin-left: 10rpx;">{{getNote(item)}}</text>
				</view>
				<view class="contentColumnC" style="padding: 20rpx;" >
					<text :class="item.type==0?'pingtBt':'shjbt'" @tap="lijilingqu(index)">{{$t('hongbaokq.lingqu')}}</text>
				</view>
			</view>
		</view>
		<view v-if="cellindex==1">
			<view v-for="(item,index) in kaquanList" class="content" style="margin-top: 20rpx;">
				<view class="contentColumnC" style="padding-top: 16rpx;">
					<view class="contentInRowC">
						<text :class="item.type==0?'pingtName':'shangjiaName'">{{item.name}}</text>
					</view>
					<text style="color:black;font-size: 26rpx;">{{$t('hongbaokq.youxiaoqi')}}{{changeDataframe(item.validity)}}</text>
				</view>
				<view class="contentColumn">
					<text v-if="item.tiaojian==null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{$t('hongbaokq.wumenkan')}}</text>
					<text v-if="item.tiaojian!=null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{mankeyong(item.tiaojian)}}</text>
					<text class="" style="font-size:28rpx;margin-left: 10rpx;">{{getNote(item)}}</text>
				</view>
				<view class="contentColumnC" style="padding: 20rpx;" >
					<text :class="item.type==0?'pingtBt':'shjbt'" @tap="lijishiyong">{{$t('hongbaokq.shiyong')}}</text>
				</view>
			</view>
		</view>
		<view v-if="cellindex==2">
			<view v-for="(item,index) in kaquanList" class="content" style="margin-top: 20rpx;">
				<view class="contentColumnC" style="padding-top: 16rpx;">
					<view class="contentInRowC">
						<text :class="item.type==0?'pingtName':'shangjiaName'">{{item.name}}</text>
					</view>
					<text style="color:#00A6FF;font-size: 26rpx;">{{$t('hongbaokq.youxiaoqi')}}{{changeDataframe(item.validity)}}</text>
				</view>
				<view class="contentColumn" style="padding: 10rpx;">
					<text v-if="item.tiaojian==null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{$t('hongbaokq.wumenkan')}}</text>
					<text v-if="item.tiaojian!=null" class="" style="font-size:28rpx;margin-left: 10rpx;">{{mankeyong(item.tiaojian)}}</text>
					<text class="" style="font-size:28rpx;margin-left: 10rpx;">{{getNote(item)}}</text>
				</view>
			</view>
		</view>
		<view style="height: 100rpx;"></view>
	</view>
	
</template>

<script>
	import api from "@/pages/api/api.js"
	export default {
		data() {
			return {
				cellindex:0,
				kaquanList:[],
				page:1
			}
		},
		onShow() {
			this.cellindex=0;
			this.getdailingHongbaokq(0);
		},
		methods: {
			cellSelect(index){
				this.page=1;
				if(index!=this.cellindex){
					this.kaquanList = [];
				}
				this.cellindex = index;
				if(index==0){
					this.getdailingHongbaokq(index);
				}
				else if(index==1){
					this.getHongbaokq(index);
				}
				else if(index==2){
					this.getHongbaokq(index);
				}
			},
			
			changeDataframe(date){
				if(date.length==10){
					var st2 = date.substr(8,2);
					st2=st2+'-'+date.substr(5,2);
					st2=st2+'-'+date.substr(0,4);
					return st2;
				}
				return date;
			},
			getNote(obj){
				var str='';
				if(obj.yhfangs==1&&obj.yhnum>1){//优惠方式：1 运费，2 订单 ,3 商品
					str= this.$t('hongbaokq.yunfeiyh');
				}
				if(obj.yhfangs==1&&obj.yhnum<1&&obj.yhnum>0){//优惠方式：1 运费，2 订单 ,3 商品
					str= this.$t('hongbaokq.yunfeizk');
				}
				if(obj.yhfangs==1&&obj.yhnum==0){//优惠方式：1 运费，2 订单 ,3 商品
					str= this.$t('hongbaokq.yunfeiquanm');
				}
				if(obj.yhfangs==2&&obj.yhnum>1){
					str= this.$t('hongbaokq.dingdanyh');
				}
				if(obj.yhfangs==2&&obj.yhnum<1){
					str= this.$t('hongbaokq.dingdanzk');
				}
				if(obj.yhfangs==3&&obj.yhnum>1){
					str= this.$t('hongbaokq.shangpinyh');
				}
				if(obj.yhfangs==3&&obj.yhnum<1){
					str= this.$t('hongbaokq.shangpinzk');
				}
				if(obj.yhnum!=0){
					str=str+' '+obj.yhnum +this.$t("locale.huobidw");
				}
				return str;
			},
			
			mankeyong(jiner){
				var str = this.$t('hongbaokq.mankeyong')
				str = str.replace('x',jiner);
				return str;
			},
			
			lijilingqu(index){
				if(this.kaquanList.length>=index){
					var obj = this.kaquanList[index];
					this.lingqukaquan(obj.id);
				}
			},
			
			lijishiyong(){
				uni.navigateTo({
					url:'/pages/OrderList/buy/gouwuche'
				})
			},
			
			getdailingHongbaokq(index){
				if(index==0){
					api('userUnclaimedList',{
						pageNum:''+this.page,
						pageSize:'20',
					},res=>{
						uni.stopPullDownRefresh();
						console.log('-----------',res)
						if(res.data.code==200){
							if(this.page>1){
								if(res.data.rows&&res.data.rows.length>0){
									var temp = this.kaquanList;
									for(var i=0;i<res.data.rows.length;i++){
										temp.push(res.data.rows[i]);
									}
									this.kaquanList=temp;
								}
							}
							else{
								this.kaquanList=res.data.rows;
							}
						}
					},failc=>{
						uni.stopPullDownRefresh();
						console.log('getadvertis----',failc)
					})
				}
			},
			getHongbaokq(index){
				
				var listType='';
				if(index==1){
					listType='1';
				}
				else if(index==2){
					listType='2';
				}
				api('userQuanyilist',{
					pageNum:this.page,
					pageSize:'20',
					listType:listType
				},res=>{
					uni.stopPullDownRefresh();
					console.log('-----------',res)
					if(res.data.code==200){
						if(this.page>1){
							if(res.data.data.records&&res.data.data.records.length>0){
								var temp = this.kaquanList;
								for(var i=0;i<res.data.data.records.length;i++){
									temp.push(res.data.data.records[i]);
								}
								this.kaquanList=temp;
							}
						}
						else{
							this.kaquanList=res.data.data.records;
						}
					}
				},failc=>{
					uni.stopPullDownRefresh();
					//console.log('getadvertis----',failc)
				})
			},
			
			lingqukaquan(quanyiId){
				api('userQuanyireceive',{
					quanyiId:quanyiId,
					num:1
				},r=>{
					console.log('-----------',r)
					if(r.data.code==200){
						this.page=1;
						this.getdailingHongbaokq(0);
						uni.showToast({
						    title:this.$t('hongbaokq.lingqucg'),
						    icon: 'none',
						    duration: 2500
						})
					}
					else{
						uni.showToast({
						    title:this.$t('hongbaokq.lingqushib'),
						    icon: 'none',
						    duration: 2500
						})
					}
				},failc=>{
					uni.showToast({
					    title:this.$t('hongbaokq.lingqushib'),
					    icon: 'none',
					    duration: 2500
					})
					//console.log('getadvertis----',failc)
				})
			},
			
		},
		/**
		 * 下拉刷新回调函数
		 */
		onPullDownRefresh() {
			console.log('onPullDownRefresh')
			this.page=1;
			if(this.cellindex==0){
				this.getdailingHongbaokq(this.cellindex);
			}
			else if(this.cellindex==1){
				this.getHongbaokq(this.cellindex);
			}
			else if(this.cellindex==2){
				this.getHongbaokq(this.cellindex);
			}
		},
		/**
		 * 上拉加载回调函数
		 */
		onReachBottom() {
			console.log('onReachBottom')
			this.page=this.page+1;
			if(this.cellindex==0){
				this.getdailingHongbaokq(this.cellindex);
			}
			else if(this.cellindex==1){
				this.getHongbaokq(this.cellindex);
			}
			else if(this.cellindex==2){
				this.getHongbaokq(this.cellindex);
			}
		},
	}
</script>

<style lang="scss">

page{
	background-color: #F2F3F7;
}

.cellsele{
	color: #00A6FF;
	font-size: 30rpx;
}

.cellNom{
	color: black;
	font-size: 26rpx;
}

.pingtName{
	color: #00A6FF;
	font-size: 32rpx;
}
.shangjiaName{
	color: green;
	font-size: 32rpx;
}

.pingtBt{
  	width: 50%;
  	height: 50rpx;
  	line-height: 60rpx;
  	border-radius: 20rpx;
  	border: solid #00A6FF;
  	border-width: 0px 0px 6rpx 0px;
  	text-align: center;
  	justify-content: center;
  	background-color: #00A6FF;
  	color: white;
  	box-shadow: 0rpx 0rpx 10rpx 0rpx #00A6FF;
  }
  
  .shjbt{
    	width: 50%;
    	height: 50rpx;
    	line-height: 60rpx;
    	border-radius: 20rpx;
    	border: solid green;
    	border-width: 0px 0px 6rpx 0px;
    	text-align: center;
    	justify-content: center;
    	background-color: green;
    	color: white;
    	box-shadow: 0rpx 0rpx 10rpx 0rpx green;
    }

</style>
