<template>
	<view class="container">
		<view class="towcontent">
			<view class="towcontentlistxitem flex-start">
				<view class="towcontentlistxitembox flex-colum"
					:class="tabIndex == item.type?'towcontentlistxitemboxopacity':''"
					v-for="(item,index) in tabList" :key="index" @click="towcontentclick(item)">
					<text class="title">{{item.name}}</text>
					<image v-if="tabIndex == item.type"
						src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/index/today/dg.png" mode="widthFix">
					</image>
				</view>
			</view>
		</view>
		
		<view class="containertop" :class="{select: type == 'confirm_order_coupon' || type == 'confirm_order_product' || type == 'orderInfo_coupon' || type == 'orderInfo_product'}">
			<view class="containertopbox">
				<view class="containertopboxitem flex-start" v-for="(item,index) in list" :key="index">
					<view class="containertopboxitemleft flex-colum"
						:class="{'containertopboxitemleft_vip': item.type == 2,'containertopboxitemlefts': status != 1,}" >
						<view class="containertopboxitemleft_one"
							:class="status == 1?'':'containertopboxitemleft_ones'">
							<block v-if="item.type == 2">
								<text>{{item.num || 0}}</text>
								<text style="font-size: 28rpx;margin-left: 6rpx;">张</text>
							</block>
							<block v-else >
								<text style="font-size: 28rpx;margin-right: 6rpx;">￥</text>
								<text>{{item.discountAmount || 0}}</text>
							</block>
							
						</view>
						<view class="containertopboxitemleft_tow"
							:class="{
								'containertopboxitemleft_tows': status != 1,
								'containertopboxitemleft_nameVip': item.type == 2,
							}">
							{{ item.type == 2 ? item.name : '优惠券(元)'}}
						</view>
					</view>
				
					<view class="containertopboxitemright">
						<view class="containertopboxitemright_one">
							<view class="flex-start">
								<image class="icon"  v-if="item.type == 2 " src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/user/coupon_icon.png" mode="aspectFill">
								<text class="title" v-if="item.type == 2 ">{{ item.shopName }}</text>
								<text class="title" v-if="item.type == 1 ">{{ item.name }}</text>
							</view>
							<text>无门槛使用</text>
						</view>
						<view class="containertopboxitemright_tow">
							<view> {{ item.type == 2 ? '会员商品券' : '通用红包券'}}</view>
							<view
								@click="navigatorGo(item)"
								:class="status == 1?'containertopboxitemright_four containertopboxitemright_btn':'containertopboxitemright_fours containertopboxitemright_btn'">
								{{status == 1 ? '去使用':'已使用'}}
							</view>
						</view>
						<view class="containertopboxitemright_there" v-if="item.type == 1 ">
							有效期至：{{$u.timeFormat(item.endTime, 'yyyy/mm/dd') || '0'}}
						</view>
					
					</view>
				</view>
			
				<image style="width: 402rpx;height: 442rpx;margin:240rpx auto 32rpx;" src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/nomore.png"
					v-if="list.length <= 0" mode="aspectFill"></image>
				<u-loadmore :status="form.status" iconSize='24' fontSize='24' height='40' />
			</view>
		</view>
		<view class="btnBox" v-if="type == 'confirm_order_coupon' || type == 'confirm_order_product' || type == 'orderInfo_coupon' || type == 'orderInfo_product'">
			<view class="btn" @click="cancelCoupon" >暂不使用券</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabList: [
					
					{
						name: '未使用',
						type: '1'
					},
					{
						name: '已使用',
						type: '2'
					},
					{
						name: '已过期',
						type: '-1'
					}
				],
				tabIndex: 1,
				list: [],
				status: 1,
				type: "",
				payAmount: 0,
				shopId: null,
				shoppingCart: [],
				productList: [],
				
			};
		},
		onLoad(options) {
			console.log(options)
			if (options.type) { this.type = options.type }
			if (options.payAmount) { this.payAmount = options.payAmount }
			if (options.shopId) { this.shopId = options.shopId }
			if (options.shoppingCart) { this.shoppingCart = JSON.parse(decodeURIComponent(options.shoppingCart)) }
			if (options.productList) { this.productList = JSON.parse(decodeURIComponent(options.productList)) }
		},
		onShow() {
			this.getCouponList()
		},
		methods: {
			
			/**
			 * 切换类型
			 * @param {Object} item
			 */
			towcontentclick(item) {
				this.tabIndex = item.type;
				this.status = item.type;
				this.getCouponList()
			},
			
			/**
			 * 获取优惠券数据
			 */
			async getCouponList() {
				try {
					let params = {
						userId: uni.cache.get('userInfo').id,
						status: this.status
					}
					if ( this.type == 'confirm_order_coupon' || this.type == 'confirm_order_product' || this.type == 'orderInfo_coupon' || this.type == 'orderInfo_product' ) {
						params.orderId = -1;
					}
					if ( this.shopId ) {
						params.shopId = this.shopId;
					}
					console.log("params",params)
					let res = await this.api.conponList(params)
					if (res.code == 0) {
						console.log(res)
						let list = [];
						if ( this.type == 'confirm_order_coupon' || this.type == 'orderInfo_coupon') {
							list = res.data.filter(v => v.type == 1);
						}else if ( this.type == 'confirm_order_product' || this.type == 'orderInfo_product') {
							list = res.data.filter(v => v.type == 2);
						} else {
							list = res.data;
						}
						this.list = list;
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			
			/**
			 * 去使用优惠券
			 */
			navigatorGo ( item ) {
				
				uni.cache.set('shopId',item.shopId)
				if ( this.type == 'confirm_order_coupon' || this.type == 'confirm_order_product' || this.type == 'orderInfo_coupon' || this.type == 'orderInfo_product' ) {
					if ( item.type == 1 ) {
						if( this.payAmount < item.fullAmount) {
							uni.showToast({
								title: "当前订单金额不足使用金额",
								icon: "none",
							})
							return;
						}
						if( this.payAmount < item.discountAmount) {
							uni.showToast({
								title: "当前订单金额不足抵扣金额",
								icon: "none",
							})
							return;
						}
						uni.$emit('couponItem', JSON.stringify(item))
						let selectCouponData = uni.cache.get('selectCouponData') || [];
						let couponList = selectCouponData.filter(v => v.type == 1);
						if ( couponList.length > 0) {
							couponList.map(items => {
								if ( item.type == 1 ) {
									for(var v in items){
										items[v] = item[v]
									}
								}
							})
						} else {
							selectCouponData.push(item)
						}
						uni.cache.set('selectCouponData',selectCouponData)
						uni.navigateBack();
					} else {
						let productNum = 0;//商品购物车数量
						let productTicketNum = 0;//商品券使用数量
						let product = this.shoppingCart.filter(v => v.productId == item.proId);
						let productTicket = this.productList.filter(v => v.productId == item.proId);
						if ( product.length <= 0 ) {
							uni.showToast({
								title: "购物车没有"+item.name,
								icon: "none",
							})
							return;
						}
						if (this.productList.filter(v => v.userCouponId == item.id).length > 0) {
							uni.showToast({
								title: `当前优惠券已选择`,
								icon: "none",
							})
							return;
						}
						// 计算当前商品购物车数量
						product.forEach(item => {
							productNum += item.number;
						})
						// 计算当前商品券使用数量
						productTicket.forEach(item => {
							productTicketNum += item.num;
						})
						if ( (productNum - productTicketNum) == 0 ) {
							uni.showToast({
								title: `当前商品已经全部抵扣`,
								icon: "none",
							})
							return;
						}
						let isNum = 0;
						let isProductItem;
						
						// 确认订单
						if ( this.type == 'confirm_order_product' ) {
							product.forEach(item => {
								for (let i = 0; i < item.number; i++){
									isNum++
									if ( isNum == productTicketNum+1 ) {
										isProductItem = item;
									}
								}
							})
							
							if ( this.payAmount < (isProductItem.memberPrice > 0 ? isProductItem.memberPrice : isProductItem.salePrice)) {
								uni.showToast({
									title: `当前金额不足商品抵扣`,
									icon: "none",
								})
								return;
							}
						}
						
						// 订单详情
						if ( this.type == 'orderInfo_product' ) {
							product.forEach(item => {
								for (let i = 0; i < item.num; i++){
									isNum++
									if ( isNum == productTicketNum+1 ) {
										isProductItem = item;
									}
								}
							})
							
							if ( this.payAmount < (isProductItem.memberPrice > 0 ? isProductItem.memberPrice : isProductItem.price)) {
								uni.showToast({
									title: `当前金额不足商品抵扣`,
									icon: "none",
								})
								return;
							}
						}
						
						if ( !isProductItem ) {
							uni.showToast({
								title: `当前订单无可抵扣商品`,
								icon: "none",
							})
							return;
						}
						// let productNum = 0;
						// product.map(item => {
						// 	productNum += item.number
						// })
						// if ( productNum < item.num) {
						// 	uni.showToast({
						// 		title: `购物车${item.name}数量不足`,
						// 		icon: "none",
						// 	})
						// 	return;
						// }
						// if (product[0].number item.num) {
							
						// }
						uni.$emit('couponItem', JSON.stringify(item))
						let selectCouponData = uni.cache.get('selectCouponData') || [];
						selectCouponData.push(item)
						uni.cache.set('selectCouponData',selectCouponData)
						uni.navigateBack();
					}
					
					
				} else {
					uni.pro.navigateTo('/pages/order_food/order_food', {
						shopId: item.shopId
					})
				}
				
			},
			
			/**
			 * 取消使用优惠券/商品券
			 */
			cancelCoupon () {
				let selectCouponData = uni.cache.get('selectCouponData')
				if ( this.type == 'confirm_order_coupon' || this.type == 'orderInfo_coupon' ) {
					let couponList = selectCouponData.filter(v => v.type == 1);
					let productList = selectCouponData.filter(v => v.type == 2);
					if ( couponList.length > 0 ) {
						uni.cache.set('selectCouponData',productList)
					}
				} else if ( this.type == 'confirm_order_product' || this.type == 'orderInfo_product' ) {
					let couponList = selectCouponData.filter(v => v.type == 1);
					let productList = selectCouponData.filter(v => v.type == 2);
					if ( productList.length > 0 ) {
						uni.cache.set('selectCouponData',couponList)
					}
				}
				uni.navigateBack();
			},
		}
	};
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.container {
		.towcontent {
			padding: 0 28rpx;
			border-top: 16rpx solid #f7f7f7;
			
			.towcontentlistxitem {
				width: 100%;
				margin-top: 32rpx;

				.towcontentlistxitembox {
					width: 33.33%;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;

					image {
						margin-top: 6rpx;
						width: 38.83rpx;
						height: 8.62rpx;
					}
				}

				.towcontentlistxitemboxopacity {
					font-weight: bold;
					text{
						color: #E3AD7F;
					}
					image {
						width: 38.83rpx;
						height: 8.62rpx;
					}
				}


			}
		}

		.containertop {
			padding: 40rpx 32rpx;
			.containertopbox {
				margin-top: 8rpx;
				.containertopboxitem::after {
					position: absolute;
					bottom: 14rpx;
					left: 14rpx;
					content: '';
					display: inline-block;
					background: url(https://czg-qr-order.oss-cn-beijing.aliyuncs.com/coupontop.png) no-repeat;
					width: 72.83rpx;
					height: 77.14rpx;
					background-size: cover;
				}

				.containertopboxitem {
					margin-bottom: 32rpx;
					width: 100%;
					position: relative;
					border-radius: 18rpx;
					box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(0, 0, 0, 0.16);
					overflow: hidden;
					.containertopboxitemleft {
						position: relative;
						width: 182rpx;
						height: 192rpx;
						background: #E3AD7F;
						border-radius: 18rpx 0rpx 0rpx 18rpx;
						padding: 0 20rpx;
						

						// ::before {
						// 	content: '';
						// 	position: absolute;
						// 	bottom: 0rpx;
						// 	left: 166rpx;
						// 	background: #fff;
						// 	display: inline-block;
						// 	width: 32rpx;
						// 	height: 16rpx;
						// 	line-height: 32rpx;
						// 	border-radius: 32rpx 32rpx 0 0;
						// 	box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
						// 	z-index: 999;

						// }
						
						.containertopboxitemleft_one {
							text {
								font-family: Source Han Sans CN, Source Han Sans CN;
								font-weight: 500;
								font-size: 60rpx;
								color: #FFFFFF;
							}
						}

						.containertopboxitemleft_ones {
							text {
								color: #999999;
							}
						}

						.containertopboxitemleft_tow {
							font-family: Source Han Sans CN, Source Han Sans CN;
							font-weight: 500;
							font-size: 24rpx;
							color: #FFFFFF;
						}
						.containertopboxitemleft_nameVip{
							color: #967152;
						}

						.containertopboxitemleft_tows {
							color: #999999;
						}
					}
					.containertopboxitemleft::after {
						content: '';
						position: absolute;
						top: -20rpx;
						left: 166rpx;
						background: #fff;
						display: inline-block;
						width: 40rpx;
						height: 40rpx;
						border-radius: 50%;
						box-shadow: inset 0rpx 1rpx 9rpx 21rpx rgba(0, 0, 0, 0.04);
						z-index: 999;
					}
					.containertopboxitemleft::before {
						content: '';
					    position: absolute;
					    bottom: -20rpx;
					    left: 166rpx;
					    background: #fff;
					    display: inline-block;
					    width: 40rpx;
					    height: 40rpx;
					    border-radius: 50%;
					    box-shadow: inset 0rpx 0rpx 15rpx 1rpx rgba(0, 0, 0, 0.1);
						z-index: 999;
					}
					.containertopboxitemleft_vip{
						background-color: #E1D4B2;
					}	
					.containertopboxitemlefts {
						background: #F7F7F7;
					}

					.containertopboxitemright {
						position: relative;
						padding: 0 32rpx;
						flex: auto;
						height: 192rpx;
						background: #FFFFFF;
						border-radius: 0rpx 18rpx 18rpx 0rpx;

						.containertopboxitemright_one {
							font-family: Source Han Sans CN, Source Han Sans CN;
							padding: 16rpx 0 20rpx 0;
							border-bottom: 1rpx dashed #707070;
							display: flex;
							justify-content: space-between;
							align-items: center;
							.icon{
								width: 26rpx;
								height: 26rpx;
								margin-right: 14rpx;
							}
							text{
								font-weight: bold;
								font-size: 24rpx;
								color: #666666;
							}
							.title{
								color: #333;
							}
						}

						.containertopboxitemright_tow {
							margin-top: 24rpx;
							font-family: Source Han Sans CN, Source Han Sans CN;
							font-weight: bold;
							font-size: 32rpx;
							color: #333333;
							display: flex;
							justify-content: space-between;
							align-items: center;
						}

						.containertopboxitemright_there {
							font-family: Source Han Sans CN, Source Han Sans CN;
							font-weight: 500;
							font-size: 24rpx;
							color: #999999;
						}
						
						.containertopboxitemright_btn{
							font-family: Source Han Sans CN, Source Han Sans CN;
							font-weight: 500;
							font-size: 24rpx;
							padding: 8rpx 24rpx;
							border-radius: 24rpx 24rpx 24rpx 24rpx;
						}

						.containertopboxitemright_four {
							color: #FFFFFF;
							background: #967152;
						}

						.containertopboxitemright_fours {
							color: #999999;
							background: #F7F7F7;
						}
					}
				}

			}
		}
		.select{
			padding-bottom: 180rpx;
		}
		.btnBox{
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 999;
			background-color: #fff;
			padding: 30rpx 30rpx 50rpx 30rpx;
			.btn{
				width: 100%;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				font-weight: bold;
				font-size: 36rpx;
				color: #FFFFFF;
				border-radius: 12rpx;
				background: #E3AD7F;
			}
		}

	}
</style>