<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 column" style="height: 70vh;">
			<view class="title">优惠券</view>
			<view class="chosentips">请选择要使用的优惠券</view>
			<scroll-view :scroll-y="true" class="col" style="height: 1px;"> 
			
				<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","price","currentPrice","order"],
		data() {
			return {
				mc: "",
				show: true,
				currentChosen: [],
				value:'',
				ts: [],
				stacking: 0,
				TotalDiscounts: 0,
				money: 0,
			};
		},
		watch: {
			price: {
				deep: true,
				immediate: true,
				handler: function(newVal, oldVal) {
					this.ts.forEach((x)=>{
						console.log(!x.err || x.err=='订单金额不满足')
						if(!x.err || x.err=='订单金额不满足'){
							x.err = x.useCondition>this.price?'订单金额不满足':null
						}
					})
					this.OverlayCalculation()
				},
			},
		},
		mounted() {
			this.mc = this.$uiConfig.mainColor;
			this.currentChosen = this.current;
			// this.value = this.current[0]
			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.currentChosen&&this.currentChosen.length>0){
					this.currentChosen.forEach(y => {
						if (x.id == y) {
							x.checkId = y
							x.check = true
							if(!x.overlapUse){
								this.value = y
							}else{
								this.value = 0
								this.stacking = 1
							}
						}
					})
				}
				
			})
			this.OverlayCalculation()
			this.checkOrderCoupon()
		},
		methods: {
			checkOrderCoupon(){
				if (this.currentPrice && 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 = '报名人数小于团队机位人数，无法使用专享优惠券'
					this.ts.forEach(x => {
						if(x.couponsUseScope==10) x.err = errorMsg
					})
					this.ts.sort((a,b) => {
						const aerr = (a.err?a.err:'').length
						const berr = (b.err?b.err:'').length
						return aerr-berr
					})
				}
				this.ts.forEach((x)=>{
					if(!x.err && x.useCondition>this.price){
						x.err = '订单金额不满足'
					}
				})
			},
			popupClose() {
				if(this.currentChosen.length>0){
					this.$emit('close', this.currentChosen)
				}else{
					this.$emit('close', -1)
				}
				// console.log(this.currentChosen,'-----')
			},
			// 不可叠加使用优惠券
			radioChange(e){
				if(this.stacking) {
					this.currentChosen = []
					this.stacking = 0
				}
				this.ts.forEach(x => {
					x.checkId = 0
					x.check = false
				})
				if(!this.currentChosen||this.currentChosen.length==0||(this.currentChosen.length==1&&this.currentChosen[0] != e)){
					this.currentChosen = [e]
				}else{
					this.currentChosen = [];
					this.value = 0
				}
				this.OverlayCalculation(1)
			},
			// 叠加使用优惠券
			multipleChoice(e,i){
				if(!this.ts[i].check) this.ts[i].check = true
				else this.ts[i].check = false
				if(!this.stacking) {
					this.currentChosen = []
					this.stacking = 1
					this.value = 0
				}
				if(!this.ts[i].check) {
					this.ts[i].checkId = 0
					this.currentChosen = this.currentChosen.filter(x=>{return x!=e})
				}else {
					this.ts[i].checkId = e
					let list = this.currentChosen.findIndex(x=>x==this.ts[i].id)
					if(list==-1) this.currentChosen.push(e)
				}
				this.OverlayCalculation()
			},
			OverlayCalculation(type){
				this.TotalDiscounts = 0
				this.money = 0
				if(this.currentChosen.length==0||type){
					this.ts.forEach(x => {
						if(x.overlapUse&&(x.err||type)){
							x.err = null
						}
					})
					return
				}
				let alreadyStacked = this.ts.filter(x => {
					return x.check
				})
				alreadyStacked.forEach(x => {
					this.TotalDiscounts+=x.denomination
					this.money = this.price-this.TotalDiscounts
				})
				this.ts.forEach(x => {
					if(x.overlapUse){
						if(!x.check&&x.denomination>this.money){
							x.err = '订单金额不满足'
						}else {
							x.err = null
						}
					}
				})
				this.ts.sort((a,b) => {
					const aerr = (a.err?a.err:'').length
					const berr = (b.err?b.err:'').length
					return aerr-berr
				})
			}
		},
	};
</script>

<style>
	@import url("../../asset/css/flex.css");
	.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;
	}

	.coupon .btton-box {
		height: 50px;
		padding: 5px 10px;
		border-top: 1px solid #f1f2f4;
		align-items: center;
		background: #fff;
	}
</style>
