<template>
	<u-popup mode="bottom" border-radius="20" :popup="false" v-model="show" :maskCloseAble="true" length="auto"
	 :safeAreaInsetBottom="true" @close="popupClose" :z-index="9999" close-icon="cross" :closeable="true">
		<view class="coupon">
		 <scroll-view :scroll-y="true" style="max-height: 70vh;"> 
			<view class="title">优惠券</view>
			<view class="chosentips">请选择要使用的优惠券</view>
			<view class="details">
				<view class="item" v-for="(x, i) in ts" :key="i" :style="{'margin-bottom':ts.length==i+1?'50px':'10px'}">
					<view class="ticket">
						<view class="miane">
							<view class="money">
								<text class="small" style="font-family: microsoft yahei ui light;" v-if="x.couponsType==1">￥</text>
								<text style="font-size: 44px;font-family: nav-font;">{{x.denomination}}</text>
								<text class="small"  style="font-family: microsoft yahei ui light;" v-if="x.couponsType!=1">折</text>
							</view>
							<view class="tiaojian">满{{x.useCondition}}元可用</view>
						</view>
					</view>
					<view class="detail">
						<view class="coupond">
							<view class="name">{{x.couponName}}</view>
							<view class="date">{{ x.expirationDate }} 失效</view>
							<view class="err" v-if="x.err">
								{{x.err}}
							</view>
							<view style="color: #dfbe6e;font-size: 22rpx;"
							:style="{'padding-top':x.err?'':'30rpx'}">
							{{x.overlapUse==1?'可叠加使用':'不可叠加使用'}}
						</view>
							<!-- <view class="rule" v-if="x.lineTeamName!='' || x.lineName!=''">{{x.lineName}} {{x.lineTeamName}} 可用</view> -->
						</view>
						<view class="chosen" v-if="!x.err">
							<u-radio-group v-if="!x.overlapUse" v-model="value" @change="radioChange" >
								<u-radio shape="circle" :name="x.id" :icon-size="36" :active-color="mc"></u-radio>
							</u-radio-group>
							<u-radio-group v-else v-model="x.checkId" @change="(e)=>{multipleChoice(e,i)}" >
								<u-radio shape="circle" :name="x.id" :icon-size="36" :active-color="mc"></u-radio>
							</u-radio-group>
						</view>
					</view>
					
				</view>
			</view>
			</scroll-view>
			<view class="btton-box">
				<u-button size="80" :ripple="true" shape="circle" :custom-style="{
            backgroundColor: mc,
            height: '80rpx',
            color: '#FFF',
            fontSize: '14px',
            margin: '0 3vw',
            width: '90vw',
          }"
				 @click="popupClose">确定</u-button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		props: ["list", "current","currentPrice","order","multiple"],
		data() {
			return {
				mc: "",
				show: true,
				currentChosen: "",
				value:'',
				ts: [],
				stacking: 0,
				multipleChosen: [],
			};
		},
		mounted() {
			this.mc = this.$uiConfig.mainColor;
			if(this.current) {
				this.currentChosen = this.current;
				this.value = this.current;
			}
			if(this.multiple) this.multipleChosen = this.multiple
			this.ts = this.list
			this.ts.forEach(x => {
				x.start_time = x.effectDate.split(' ')[0].replace(/-/g, '.')
				x.end_time = x.expirationDate.split(' ')[0].replace(/-/g, '.')
				if(this.multipleChosen&&this.multipleChosen.length>0){
					this.multipleChosen.forEach(y => {
						if (x.id == y) {
							console.log(y,'====')
							x.checkId = y
							x.check = true
						}
					})
				}
				
			})
			this.checkOrderCoupon()
		},
		methods: {
			checkOrderCoupon(){
				if (this.currentPrice.teamType==1) {
					//totalSeat:总机位   isSubstitution:1 （可候补） leaderNum:领队
					
					const shengyu = this.currentPrice.totalSeat-this.currentPrice.leaderNum
					const remainNum = this.currentPrice.remainNum
					const orderNum = this.order.ManNum+this.order.ChirdNeedBedNum+this.order.ChirdNoBedNum
					let errorMsg = null
					if(shengyu!=remainNum) errorMsg = '当前定制团存在其他订单，无法使用专享优惠券'
					else if(orderNum<shengyu) errorMsg = '报名人数小于团队机位人数，无法使用专享优惠券'
					
					console.log(errorMsg,shengyu,remainNum,orderNum)
					
					this.ts.forEach(x => {
						if(x.couponsUseScope==10) x.err = errorMsg
					})
					
					console.log(this.ts)
				}
			},
			popupClose() {
				if(this.stacking==0){
					if (this.currentChosen != this.current) {
						this.$emit('close', this.currentChosen)
					} else {
						this.$emit('close', -1)
					}
				}else{
					if(this.multipleChosen.length>0){
						this.$emit('close', this.multipleChosen)
					}else{
						this.$emit('close', -1)
					}
				}
				
			},
			radioChange(e){
				this.stacking = 0
				this.multipleChosen = []
				this.ts.forEach(x => {
					x.checkId = 0
					x.check = false
				})
				if(this.currentChosen != e){
					this.currentChosen = e
				}else{
					this.currentChosen = 0;
					this.value = 0
				}
			},
			// 叠加使用优惠券
			multipleChoice(e,i){
				this.stacking = 1
				this.value = 0
				if(!this.ts[i].check) this.ts[i].check = true
				else this.ts[i].check = false

				if(!this.ts[i].check) {
					this.ts[i].checkId = 0
					this.multipleChosen = this.multipleChosen.filter(x=>{return x!=e})
				}else {
					this.ts[i].checkId = e
					let list = this.multipleChosen.findIndex(x=>x==this.ts[i].id)
					if(list==-1) this.multipleChosen.push(e)
				}
				console.log(e,'---',this.multipleChosen)
			}
		},
	};
</script>

<style>
	.coupon {
		max-height: 70vh;
		background: #f5f5f5;
		/* padding-bottom: 50px; */
		text-align: left !important;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		/* overflow: scroll; */
		overflow: hidden;
	}

	.coupon .title {
		font-size: 22px;
		color: #000;
		padding: 20px;
		background: #fff;
	}

	.coupon .chosentips {
		padding: 12px 20px;
		border-bottom: 1px solid #f5f5f5;
		border-top: 1px solid #f5f5f5;
		color: #777;
		background: #fff;
		font-size: 12px;
	}

	.coupon .details {
		padding: 10px;
		display: flex;
		flex-direction: column;
	}

	.coupon .details .item {
		display: flex;
		margin-bottom: 10px;
		align-items: flex-start;
		background: #fff;
	}

	.coupon .details .item .ticket {
		position: relative;
		background-image: linear-gradient(to bottom right,
			rgb(95, 154, 220),
			rgb(112, 125, 195));
		width: 125px;
		height: 100px;
		display: flex;
		align-items: center;
		color: #fff;
	}

	.coupon .details .item .ticket .miane .money {
		font-family: "oswald";
		font-weight: bold;
		size: 64px;
		width: 125px;
		text-align: center;
	}

	.coupon .details .item .ticket .miane .money .small {
		font-size: 20px;
	}

	.coupon .details .item .ticket .miane .tiaojian {
		font-size: 14px;
		width: 125px;
		text-align: center;
	}

	.coupon .details .item .detail {
		width: 1px;
		flex: 1;
		background: #fff;
		padding: 10px;
		display: flex;
		align-items: flex-end;
	}

	.coupon .details .item .detail .coupond {
		flex: 1;
	}

	.coupon .details .item .detail .coupond .name {
		font-size: 13px;
		font-weight: 600;
		color: #000;
	}
	
	.coupon .details .item .detail .coupond .err {
		margin-top: 5px;
		font-size: 11px;
		color: #FF3166;
	}

	.coupon .details .item .detail .coupond .date {
		margin-top: 5px;
		font-size: 11px;
		color: #777;
	}

	.coupon .details .item .detail .coupond .rule {
		font-size: 11px;
		color: #777;
		margin-top: 5px;
	}

	.coupon .details .item .detail .chosen {
		width: 20px;
	}

	.coupon .details .item .ticket:before {
		content: "";
		display: block;
		width: 12px;
		height: 100%;
		background-size: 12px 12px;
		/* 一个repeat的大小 */
		background-repeat: repeat-y;
		background-image: radial-gradient(#f5f5f5 4px, transparent 4px);
		position: absolute;
		top: 0;
	}

	.coupon .details .item .ticket:before {
		left: -6px;
	}

	.pesongBox .btton-box {
		height: 50px;
		display: flex;
		padding: 5px 10px;
		border-top: 1px solid #f5f5f5;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		align-items: center;
		background: #fff;
	}
</style>
