<template>
	<view>
		<view class="card_box">
			<view class="card_head_box">
				<view class="card_head_item" v-for="(item,index) in 8" :key="index"></view>
			</view>
			<view class="card">
				<!-- 订单头部 -->
				
				<view class="card_item" v-for="(item,index) in listinfo.detailList" :key="index">
					<view class="head">
						<view class="head_left">
							<text class="placeNum">第{{item.placeNum}}次下单</text>
							<text class="placeTime">{{item.placeTime}}</text>
						</view>
						
					</view>
					<!-- 订单详情 -->
					<view class="shop-info">
						<view class="item" v-for="(items,indexs) in item.info" :key="indexs">
							<view class="cover">
								<u-image width="152" height="152" radius="16" :src="items.productImg" v-if="items.productId!=-999"></u-image>
								<u-image width="152" height="152" radius="16" :src="'https://czg-qr-order.oss-cn-beijing.aliyuncs.com/confirmOrder/table.png'" mode="heightFix" v-else></u-image>
							</view>
							<view class="info">
								<text class="productName">{{items.productName}}</text>
								<text class="productSkuName" v-if="items.productSkuName">{{items.productSkuName}}</text>
							</view>
								
							<view class="price">
								<view class="priceAmount">￥{{items.priceAmount}}</view>
								<view class="num">x{{items.num}}</view>
							</view>
						</view>
					</view>
					
					<view class="status" v-if="listinfo.status == 'unpaid' ||  listinfo.status == 'paying'">未付款</view>
					<view class="totalAmount">
						<view class="label">小计￥</view>
						<view class="price"> {{item.totalAmount}} </view>
					</view>
					
					<view class="semicircle_icon" v-if="index > 0">
						<view class="semicircle_left_icon"></view>
						<view class="semicircle_right_icon"></view>
					</view>
				</view>
				<view class="cell-item" v-if="listinfo.orderInfo.seatCount > 0">
					<view class="label">餐位费</view>
					<view class="val">
						<view>X{{listinfo.orderInfo.seatCount}}</view>
						<view style="font-size: 28rpx;">￥</view>
						<view>{{listinfo.orderInfo.seatAmount}}</view>
					</view>
				</view>
				<view v-for="(item,indexe) in favorable" :key="indexe">
					<view class="favorable" :class="{column:item.value.length>0&&item.type=='product'}" @click="goUrl(item)">
						<view class="favorable_left"> 
							<!-- <image class="icon" :src="item.url" mode="aspectFill"/> -->
							<text class="name"> {{ item.name }} </text>
						</view>
						<view class="favorable_right" v-if="item.type=='coupon'">
							<text class="favorable_right_text">{{item.value}}</text> 
							<u-icon name="arrow-right" color="#575B66" size="28"></u-icon>
						</view>
						<view class="favorable_right" :class="{column:item.value.length>0}" v-if="item.type=='product'">
							<view class="favorable_right_text"  v-for="(items,indexs) in item.value" :key="indexs">
								<text>{{items.name}}</text>
								<text>X{{items.num}}</text>
								<text>-￥{{items.discountAmount || 0}}</text>
							</view> 
							<u-icon v-if="item.value.length <= 0" name="arrow-right" color="#575B66" size="28"></u-icon>
						</view>
						<view class="favorable_right" v-if="item.type=='points'" @click.stop="pointsChange">
							<text class="favorable_right_text" style="color: #666;margin-right: 16rpx;" v-if="calcUsablePointsData.usable">
								使用 {{ calcUsablePointsData.pointsNum}} 积分抵扣￥{{calcUsablePointsData.pointsNum/calcUsablePointsData.equivalentPoints}}
							</text> 
							<text class="favorable_right_text" style="color: #666;margin-right: 16rpx;color: #DE4D3A;" v-else>
								{{calcUsablePointsData.unusableReason}}
							</text> 
							
							<u-checkbox-group iconPlacement="right" @change="pointsChange">
							    <u-checkbox v-model="isPointsChecked" :disabled="freeCheck||!calcUsablePointsData.usable" :checked="isPointsChecked" activeColor="#E8AD7B" shape="circle" icon-size="36" size="36">
								</u-checkbox>
							</u-checkbox-group>
						</view>
					</view>
					
				</view>
				<view class="total-wrap">
					<view>总计￥</view>
					<view class="price"> {{listinfo.payAmount}} </view>
				</view>
			
			</view>
		</view>
		<view class="orderInfo">
			<view class="row" @click="copyHandle(listinfo.orderNo)">
				<text class="t">订单编号：</text>
				<text class="info">{{listinfo.orderNo}}（点击复制）</text>
			</view>
			<view class="row">
				<text class="t">下单时间：</text>
				<text class="info">{{$u.timeFormat(listinfo.time, 'yyyy-mm-dd hh:MM:ss')}}</text>
			</view>
			<view class="row">
				<text class="t">下单门店：</text>
				<text class="info">{{listinfo.name}}</text>
			</view>
			<view class="row">
				<text class="t">订单类型：</text>
				<text class="info" v-if="listinfo.sendType == 'post'">快递</text>
				<text class="info" v-if="listinfo.sendType == 'takeaway'">外卖</text>
				<text class="info" v-if="listinfo.sendType == 'takeself'">自提</text>
				<text class="info" v-if="listinfo.sendType == 'table'">堂食</text>
			</view>
			<view class="row">
				<text class="t">备注：</text>
				<text class="info">{{ listinfo.orderInfo.remark || ""}}</text>
			</view>
			<view class="row">
				<text class="t">就餐人数：</text>
				<text class="info">{{ listinfo.orderInfo.seatCount || ""}}</text>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
	
		data() {
			return {
				favorable: [
					{ name: "优惠券", type: "coupon",value: "", url: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/confirmOrder/coupon.png"},
					{ name: "商品券", type: "product",value: [], url: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/confirmOrder/coupon.png"},
					// { name: "团购优惠", type: "group",value: "", url: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/confirmOrder/groupOffer.png"},
					{ name: "积分抵扣", type: "points",value: "", url: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/confirmOrder/points.png" }
				],
				calcUsablePointsData: null,
				isPointsChecked: false,
				freeCheck: false,
				freeDisabled: false,
				userCouponInfos: [],
			}
		},
		props:{
			listinfo:{
				type: Object
			},
			amountVIP:{
				type: Object
			},
		},
		watch :{
			listinfo:{
				immediate: true,
				handler (newVal) {
					console.log(newVal)
					this.$forceUpdate()
				}
			},
		},
		mounted() {
		},
		methods: {
			childOnShow() {
				console.log(222)
			    uni.$on('couponItem',this.changeCoupon)
			    this.getCalcUsablePoints()
			},
			/**
			 * 监听优惠券/商品券选择
			 * @param {Object} data
			 */
			changeCoupon ( data ) {
				let _this = this;
				// uni.$off('couponItem')
				let couponData = JSON.parse(data)
				console.log('监听到事件来自返回的参数：' , couponData);
				if ( couponData.type == 1 ) { //满减优惠券
					let couponList = _this.userCouponInfos.filter(v => v.type == 1);
					//不包含优惠券的金额
					let currentPayAmount = (couponList.length > 0 ? Number(_this.listinfo.payAmount) + couponList[0].discountAmount : _this.listinfo.payAmount)
					// 判断优惠金额不能大于支付金额
					if ( couponData.discountAmount < currentPayAmount) {
						_this.listinfo.payAmount = ( currentPayAmount - couponData.discountAmount).toFixed(2)
						_this.favorable[0].value = ('-￥'+couponData.discountAmount)
						if ( couponList.length > 0) {
							_this.userCouponInfos.map(item => {
								if ( item.type == 1 ) {
									item.userCouponId = couponData.id
									item.type = couponData.type
									item.discountAmount = couponData.discountAmount
									item.num = couponData.type == 1 ? 1 : couponData.num
								}
							})
						} else {
							_this.favorable[0].value = ('-￥'+couponData.discountAmount)
							_this.userCouponInfos.push({
								userCouponId: couponData.id,
								type: couponData.type,
								discountAmount: couponData.discountAmount,
								num: couponData.type == 1 ? 1 : couponData.num,
							})
						}
						console.log(currentPayAmount)
						console.log(_this.listinfo.payAmount)
						if ( _this.isPointsChecked ) {
							_this.pointsChange()
							_this.getCalcUsablePoints()
						}
						this.$emit("setPayAmount",_this.listinfo.payAmount)
					} else {  
						uni.showToast({
							title: "优惠券满减金额大于订单金额不可使用",
							icon: "none",
						})
					}
				} else {  //商品券
					
					// 筛选选中商品券商品列表
					let couponList = _this.listinfo.details.filter(v => v.productId == couponData.proId);
					//金额从小到大排序
					let minCouponList = couponList.sort((a, b) => (a.memberPrice > 0 ? a.memberPrice : a.salePrice) - (b.memberPrice > 0 ? b.memberPrice : b.salePrice))
					let productTicketList = _this.userCouponInfos.filter(v => v.productId == couponData.proId);
					//商品券抵扣的数量
					let productNum = couponData.num;
					let productPayAmount = 0;
					let productTicketNum = 0;
					let productNumber = 0;
					productTicketList.map((item,index) => {
						productTicketNum += item.num
					})
					console.log(productTicketList)
					console.log(minCouponList)
					minCouponList.map((item,index) => {
						// productNumber += item.num 
						if ( productNum > 0) {
							item.num = (item.num - productTicketNum) <= 0 ? 0 : (item.num - productTicketNum)
							productTicketNum = (item.num - productTicketNum) <= 0 ? (productTicketNum - item.num) : item.num - productTicketNum
							console.log(productTicketNum)
						}
						if ( productNum > 0 && item.num > 0) {
							let num = productNum >= item.num ? item.num : productNum
							productNum = (productNum - item.num) >= 0 ? (productNum - item.num) : 0;
							productPayAmount = productPayAmount + (num*(item.memberPrice > 0 ? item.memberPrice : item.price))
							// let num = productNum >= item.num ? item.num : productNum
							// productNum = (productNum - item.num) >= 0 ? (productNum - item.num) : 0;
							
							// productNum = num <= productNum ? num : productNum;
						}
					})
					_this.userCouponInfos.push({
						userCouponId: couponData.id,
						type: couponData.type,
						productId: couponData.proId,
						discountAmount: productPayAmount,
						num: productNum == 0 ? couponData.num : (couponData.num - productNum),
					})
					_this.favorable[1].value.push({
						userCouponId: couponData.id,
						name: couponData.name,
						type: couponData.type,
						productId: couponData.proId,
						discountAmount: productPayAmount,
						num: productNum == 0 ? couponData.num : (couponData.num - productNum),
					})
					console.log(productNum)
					console.log(productNum == 0 ? couponData.num : productNum)
					if ( _this.isPointsChecked) {
						_this.pointsChange()
					}
					this.setPayAmount()
				}
				uni.$off('couponItem')
			},
			setPayAmount ( cartLists ) {
				let tableFee;
				// this.storeInfo.registerType == 'munchies'
				
				
				if ( this.userCouponInfos.length > 0 ) {
					console.log(this.userCouponInfos)
					this.userCouponInfos.forEach(item => {
						if ( item.type == 1 ) {
							if ( item.discountAmount && item.discountAmount > 0 ) {
								this.listinfo.payAmount = (this.listinfo.payAmount - item.discountAmount).toFixed(2)
							}
						} else {
							if ( item.discountAmount && item.discountAmount > 0 ) {
								this.listinfo.payAmount = (this.listinfo.payAmount - item.discountAmount).toFixed(2)
							}
						}
					})
				}
				
				
				this.isPointsChecked = false;
				this.getCalcUsablePoints()
				this.$emit("setPayAmount",this.listinfo.payAmount)
			},
			/**
			 * 积分状态监听
			 */
			pointsChange (){
				if ( this.freeCheck || !this.calcUsablePointsData.usable) {
					return;
				}
				this.isPointsChecked = !this.isPointsChecked
				if ( this.isPointsChecked ) {
					this.freeDisabled = true
					
					this.listinfo.payAmount = (this.listinfo.payAmount-(this.calcUsablePointsData.pointsNum/this.calcUsablePointsData.equivalentPoints)).toFixed(2);
				} else {
					this.freeDisabled = false
					this.listinfo.payAmount = (Number(this.listinfo.payAmount)+(this.calcUsablePointsData.pointsNum/this.calcUsablePointsData.equivalentPoints)).toFixed(2);
				}
				console.log(this.listinfo.payAmount)
				this.$emit("setPayAmount",this.listinfo.payAmount)
			},
			goUrl(item){
				switch (item.type){
					case 'coupon':
						let payAmount = this.listinfo.payAmount;
						let couponList = this.userCouponInfos.filter(v => v.type == 1);
						if ( couponList.length > 0 ) { 
							payAmount = payAmount + couponList.discountAmount;
						}
						uni.pro.navigateTo('/pages/user/coupon', {
							type: "confirm_order_coupon",
							payAmount: payAmount,
							shoppingCart: JSON.stringify(this.listinfo.details)
						})
						break;
					case 'product':
						let productList = this.userCouponInfos.filter(v => v.type == 2);
						uni.pro.navigateTo('/pages/user/coupon', {
							type: "confirm_order_product",
							payAmount: this.listinfo.payAmount,
							shoppingCart: JSON.stringify(this.listinfo.details),
							productList: JSON.stringify(productList)
						})
						break;	
						
					
				}
			},
			
			/**
			 * 获取积分相关信息
			 */
			async getCalcUsablePoints() {
				let params = {
					memberId: this.listinfo.orderInfo.memberId,
					orderAmount: this.listinfo.payAmount,
				}
				let res = await this.api.calcUsablePoints(params)
				this.calcUsablePointsData = res.data;
				if ( this.calcUsablePointsData.usable && this.listinfo.payAmount >= this.calcUsablePointsData.minPaymentAmount) {
					this.calcUsablePointsData.pointsNum = this.calcUsablePointsData.accountPoints >= this.calcUsablePointsData.maxUsablePoints ? this.calcUsablePointsData.maxUsablePoints : this.calcUsablePointsData.accountPoints
				}
			},
			/**
			 * 复制订单号
			 * @param {Object} e
			 */
			copyHandle(e) {
				uni.setClipboardData({
					data: e,
					success() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						});
					}
				});
			},
			
			
		}
	}
</script>

<style lang="scss">
	.card_box{
		background-color: #fff;
		// box-shadow: 0rpx 8rpx 12rpx 2rpx rgba(87,86,86,0.35);
		position: relative;
		width: 100%;
		height: 100%;
		// box-shadow: 0rpx 4rpx 12rpx 2rpx rgba(87,86,86,0.35);
		border-radius: 18rpx;
		padding-bottom: 32rpx;
		.card_head_box{
			width: 100%;
			display: flex;
			justify-content: space-between;
			position: absolute;
			top: -22.5rpx;
			padding: 0 44rpx;
			.card_head_item{
				width: 45rpx;
				height: 45rpx;
				background-color: #f9f9f9;
				border-radius: 50%;
				// box-shadow: inset 0rpx -13rpx 18rpx -16rpx rgba(87, 86, 86, 0.35);
			}
		
		}
		
		.card {
			// background-color: #fff;
			border-radius: 20upx;
			margin-bottom: 28upx;
			position: relative;
			.card_item{
				display: flex;
				flex-direction: column;
				position: relative;
				padding: 0 34rpx;
				.head {
					display: flex;
					justify-content: space-between;
					padding: 32rpx 0;
					.head_left{
						display: flex;
						align-items: center;
						.placeNum{
							font-weight: bold;
							font-size: 32rpx;
							color: #333333;
							margin-right: 32rpx;
							flex-shrink: 0;
						}
						.placeTime{
							font-weight: 400;
							font-size: 32rpx;
							color: #666666;
							flex-shrink: 0;
						}
					}
					
				}
				
				.shop-info {
					border-bottom: 2rpx dashed #e5e5e5;
					.item:nth-child(1) {
						margin-top: 0;
					}
				
					.item {
						margin-bottom: 32rpx;
						display: flex;
						.cover{
							
						}
						.info {
							flex: 1;
							display: flex;
							flex-direction: column;
							padding-left: 16upx;
							.productName{
								font-weight: bold;
								font-size: 28rpx;
								color: #333333;
								margin-top: 20rpx;
							}
							.productSkuName{
								font-weight: 400;
								font-size: 24rpx;
								color: #999999;
								  display: -webkit-box;
								  -webkit-box-orient: vertical;
								  -webkit-line-clamp: 3;
								  overflow: hidden;
								  text-overflow: ellipsis;
							}
							.n {
								font-size: 24upx;
								color: #999;
							}
						}
						.price{
							display: flex;
							flex-direction: column;
							align-items: flex-end;
							padding-left: 68rpx;
							.priceAmount{
								font-weight: 500;
								font-size: 28rpx;
								color: #333333;
								margin-top: 26rpx;
							}
							.num{
								font-weight: 400;
								font-size: 24rpx;
								color: #999999;
								margin-top: 22rpx;
							}
						}
					}
				}
				.status{
					align-self: flex-end;
					width: 92rpx;
					height: 40rpx;
					line-height: 40rpx;
					text-align: center;
					background: #999999;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					font-weight: 400;
					font-size: 22rpx;
					color: #FFFFFF;
					margin-top: 32rpx;
				}
				.totalAmount {
					width: 100%;
					display: flex;
					justify-content: flex-end;
					align-items: flex-end;
					padding: 32rpx 0;
					border-bottom: 2rpx dashed #e5e5e5;
					.label{
						font-weight: bold;
						font-size: 28rpx;
						color: #333333;
					}
					.price{
						font-weight: bold;
						font-size: 36rpx;
						color: #333333;
					}
				}
				.semicircle_icon{
					width: 100%;
					position: absolute;
					top: -22.5rpx;
					left: 0;
					display: flex;
					justify-content: space-between;
					.semicircle_left_icon{
						width: 45rpx;
						height: 45rpx;
						margin-left: -22.5rpx;
						background-color: #f9f9f9;
						border-radius: 50%;
						// box-shadow: inset -20rpx 0rpx 22rpx -20rpx rgba(87, 86, 86, 0.35);
						
					}
					.semicircle_right_icon{
						width: 45rpx;
						height: 45rpx;
						margin-right: -22.5rpx;
						background-color: #f9f9f9;
						border-radius: 50%;
						// box-shadow: inset 13rpx 0rpx 16rpx -9rpx rgba(87, 86, 86, 0.35);
					
					}
				}
			}
			.cell-item{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 32rpx 34rpx 0 34rpx;
				.label{
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
					padding-bottom: 32rpx;
				}
				.val{
					display: flex;
					align-items: flex-end;
					padding-bottom: 32rpx;
					border-bottom: 2rpx dashed #e5e5e5;
					view:nth-child(1){
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
						margin-right: 42rpx;
					}
					view:nth-child(2){
						display: flex;
						align-items: flex-end;
						font-weight: bold;
						font-size: 36rpx;
						color: #333333;
					}
				}
			}
			
			.favorable{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 30rpx;
				box-sizing: border-box;
				padding-bottom: 26rpx;
				padding-top: 26rpx;
				border-bottom: 2rpx solid #E5E5E5;
				.favorable_left{
					display: flex;
					align-items: center;
					.icon{
						width: 40rpx;
						height: 40rpx;
						margin-right: 16rpx;
					}
					.name{
						font-size: 28rpx;
						font-weight: 400rpx;
						color: #333;
					}
				}
				.favorable_right{
					display: flex;
					align-items: center;
					.favorable_right_text{
						font-size: 24rpx;
						color: #999;
						font-weight: 400rpx;
					}
				
				}
				.favorable_right.column{
					display: flex;
					flex-direction: column;
					.favorable_right_text{
						margin-bottom: 10rpx;
						text:nth-child(1){
							font-size: 26rpx;
							color: #999;
						}
						text:nth-child(2){
							font-size: 26rpx;
							color: #999;
							margin: 0 30rpx;
						}
						text:nth-child(3){
							font-size: 26rpx;
							color: #333;
							
						}
					}
				}
			}
			.favorable.column{
				align-items: flex-start;
			}
				
			.total-wrap {
				width: 100%;
				display: flex;
				justify-content: flex-end;
				align-items: flex-end;
				padding: 0 34rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #333333;
				margin-top: 64rpx;
				.price{
					font-weight: bold;
					font-size: 36rpx;
				}
			}
			
			
		
			.order_footer{
				display: flex;
				flex-direction: column;
				padding: 34rpx;
				position: relative;
				
				
				
			}
		
			
		}
	}
	.orderInfo{
		background: #FFFFFF;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		margin-top: 32rpx;
		padding: 32rpx 34rpx;
		// box-shadow: 0rpx 4rpx 12rpx 2rpx rgba(87,86,86,0.35);
		.row {
			display: flex;
			justify-content: space-between;
			margin-bottom: 32rpx;
					
			.t {
				font-weight: bold;
				font-size: 28rpx;
				color: #333333;
				flex-shrink: 0;
			}
					
			.info {
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
			}
		}
		.row:last-child{
			margin-bottom: none;
		}
		
	}
	
	
	.fixedview {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		
		.flex-between {
			width: 100%;
			padding: 36rpx 54rpx 102rpx 54rpx;
			background: #FFFFFF;
	
			.fixedview_one {
				display: flex;
				align-items: flex-end;
				.fixedview_oneone {
					font-weight: 400;
					font-size: 28rpx;
					color: #666666;
				}
	
				.fixedview_onetow {
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
	
					text {
						font-size: 24rpx;
					}
				}
			}
	
			.fixedview_tow {
				width: 140rpx;
				height: 56rpx;
				line-height: 56rpx;
				text-align: center;
				background: #E3AD7F;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}
	}
</style>
