<template>
	<u-popup :show="popupShow" round="17" mode="bottom" @close="close" :closeable="true" @touchmove.prevent.stop>
		<view class="choiceShow-wrap">
			<!-- 商品信息 -->
			<view class="flex goods">
				<view class="goods-left">
					<view class="goods-left__image flex">
						<image class="image-goods" :src="payInfo.picUrl" mode="aspectFit"></image>
					</view>
					<view class="goods-left-content flex">
						<!-- #ifdef MP-ALIPAY -->
						<view class="goods-left-content__titletwo">{{ detailInfo.title }}</view>
						<!-- #endif -->
						<!-- #ifndef MP-ALIPAY -->
						<view class="goods-left-content__title ells-one">{{ detailInfo.title }}</view>
						<!-- #endif -->
						<view class="goods-left-content__price">￥{{ $numberFormat(payInfo.price) }}</view>
					</view>
				</view>
				<view class="goods-right">库存：{{ payInfo.quantity }}</view>
			</view>
			<!-- sku -->
			<view class="sku" v-for="(item, index) in skuList" :key="index">
				<view class="sku-title">{{ item.name }}</view>
				<view class="flex sku-list">
					<view
						class="sku-list-item"
						:class="{'action': item.actionIndex == indexs}"
						v-for="(ele, indexs) in item.value" :key="indexs" @click="getSku(ele, item, indexs)">
						{{ ele }}
					</view>
				</view>
			</view>
			<!-- 数量 -->
			<view class="flex quantity">
				<view class="quantity-title">商品数量</view>
				<view class="quantity-title">
					<uni-number-box v-model="orderNum" :min="1"
						@change="valChange($event, payInfo)"></uni-number-box>
				</view>
			</view>
			<!-- 按钮 -->
			<view class="flex btn">
				<view class="flex btn-left">
					<view class="title">应付：</view>
					<view class="price">￥{{ $numberFormat(payInfo.value) }}</view>
				</view>
				<view class="btn-right">
					<view class="confirm" @click="confirmPrize">立即购买</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import env from '@/config/env.js'
	import $http from '@/utils/request.js'
	export default {
		name: "purchase-popup",
		props: {
			popupShow: {
				type: [Boolean],
				default: false
			},
			skuListInit: {
				type: Array,
				default: () => []
			},
			skuListPopup: {
				type: Array,
				default: () => []
			},
			detailInfo: {
				type: [Object],
				default: {}
			},
			popupInfo: {
				type: [Object],
				default: {}
			}
		},
		data() {
			return {
				orderNum: 1,
				payInfo: {},
				skuList: []
			};
		},
		
		created() {
			this.payInfo = this.popupInfo
			this.skuList = this.skuListPopup
		},
		
		methods: {
			valChange(e, item) {
				this.$set(item, 'value', e * item.price)
				if (this.payInfo.quantity < e) {
					uni.$u.toast('库存不足');
				}
			},
			
			getSku(e, item, indexs) {
				this.$set(item, 'txt', `${item.name}:${e}`)
				this.$set(item, 'actionIndex', indexs)
				this.orderNum = 1
				let actionSku = this.skuList.map(item => {
					return item.txt
				}).join(';')
				let sku = this.skuListInit.find(item => {
					return item.properties == actionSku
				})
				this.payInfo = {
					...sku,
					price: sku.value,
					picUrl: env.filePublic + sku.picUrl
				}
			},
			
			confirmPrize() {
				let flag = false
				// let data = {
				// 	goodsId: this.payInfo.goodsId,
				// 	skuId: this.payInfo.skuId,
				// 	num: this.orderNum,
				// 	resource:'PAYMENT',
				// }
				// console.log(data);
				if (flag) return
				if (this.payInfo.quantity == 0) {
					uni.$u.toast('库存不足');
					return
				}
				if (this.payInfo.quantity < this.orderNum) {
					uni.$u.toast('库存不足');
					return
				}
				uni.showLoading({
					title: '购买中'
				});
				flag = true
				
				uni.hideLoading();
				uni.navigateTo({
					url: `/packageGoods/order/settlement?goodsId=${ this.payInfo.goodsId }&skuId=${ this.payInfo.skuId?this.payInfo.skuId:0 }&num=${ this.orderNum }`
				})
			},
			
			close() {
				this.$emit('close')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.choiceShow-wrap {
		min-height: 400rpx;
		padding: 34rpx;
		
		// 商品
		.goods {
			justify-content: space-between;
			margin-bottom: 66rpx;
			
			&-left {
				flex: 1;
				display: flex;
				
				&__image {
					width: 154rpx;
					height: 154rpx;
					border: 1px solid #EEEEEE;
					border-radius: 18rpx;
					margin-right: 24rpx;
					
					.image-goods {
						width: 154rpx;
						height: 154rpx;
					}
				}
				
				&-content {
					height: 154rpx;
					flex-direction: column;
					align-items: flex-start;
					justify-content: space-between;
					padding: 34rpx 0;
					
					.image-goods {
						width: 154rpx;
						height: 154rpx;
						border-radius: 18rpx;
						border: 1px solid #EEEEEE;
						margin-right: 24rpx;
					}
					
					&__title {
						height: 30rpx;
						overflow: hidden;
						font-size: 30rpx;
						line-height: 30rpx;
						font-weight: bold;
					}
					
					&__titletwo {
						width: 350rpx;
						height: 30rpx;
						font-size: 30rpx;
						line-height: 35rpx;
						font-weight: bold;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					
					&__price {
						color: #FA822C;
						font-size: 26rpx;
					}
				}
			}
			
			&-right {
				color: #666;
			}
		}
		
		// sku
		.sku {
			&-title {
				font-size: 30rpx;
				line-height: 30rpx;
				font-weight: bold;
				margin-bottom: 34rpx;
			}
	
			&-list {
				justify-content: flex-start;
				flex-wrap: wrap;
	
				&-item {
					padding: 20rpx 66rpx;
					border-radius: 36rpx;
					margin-right: 18rpx;
					background: #F5F6F8;
					margin-bottom: 34rpx;
				}
	
				.action {
					background-color: rgba(250, 130, 44, .25);
					color: #FA822C;
				}
			}
		}
		
		// 数量
		.quantity {
			justify-content: space-between;
			margin: 34rpx 0;
			
			&-title {
				font-size: 30rpx;
				line-height: 30rpx;
				font-weight: bold;
			}
		}
	
		// 按钮
		.btn {
			justify-content: space-between;
			padding-top: 36rpx;
			border-top: 1px solid #eee;
	
			&-left {
				.price {
					font-size: 26rpx;
					color: #FA822C;
				}
			}
	
			&-right {
				.confirm {
					width: 414rpx;
					height: 88rpx;
					line-height: 88rpx;
					background: #FA822C;
					border-radius: 44rpx;
					font-size: 36rpx;
					color: #fff;
					text-align: center;
				}
			}
		}
	}
</style>