<template name="cm-parameter">
	<!-- 相关规格 -->
	<view>
		<tui-bottom-popup :radius="true" :show="popupShow" @close="hidePopup">
			<view class="tui-popup-box clearfix">
				<view class="tui-shopping-main" :style="{ paddingBottom: isIphoneX ? '68rpx' : '34rpx' }">
					<view class="tui-sku-title">
						<view class="tui-sku-image"> <image :src="skuProduct.image" mode=""></image> </view>
						<view class="tui-sku-price">
							<view class="sku-price-viw">
								<view
									class="sku-price-text"
									:class="
										PromotionsFormat(skuProduct.promotions) || skuProduct.svipProductFlag == 1
											? 'none'
											: ''
									"
								>
									￥{{
										(PromotionsFormat(skuProduct.promotions) || skuProduct.svipProductFlag == 1
											? skuProduct.originalPrice
											: skuProduct.price) | NumFormat
									}}
								</view>
							</view>
							<view class="sku-price-vip">
								<view class="floor-item-act">
									<view class="coupon-tags" v-if="skuProduct.couponsLogo">优惠券</view>
									<template v-if="skuProduct.actStatus == 1 && skuProduct.promotions">
										<view v-if="PromotionsFormat(skuProduct.promotions)" class="floor-tags">
											{{ skuProduct.promotions.name }}
											<text
												v-if="skuProduct.promotions != null && skuProduct.promotions.type != 3"
											>
												:￥{{
													skuProduct.promotions == null
														? '0.00'
														: skuProduct.promotions.touchPrice | NumFormat
												}}
											</text>
										</view>
										<view v-else-if="skuProduct.promotions.type != 3" class="floor-tags">
											{{ skuProduct.promotions.name }}
										</view>
									</template>
									<template v-if="skuProduct.actStatus == null && skuProduct.ladderFlag == 1">
										<view class="floor-tags">阶梯价格</view>
									</template>
									<template v-if="skuProduct.svipProductFlag == 1">
										<view class="svip-tags">
											<view class="tags">SVIP</view>
											<view class="price">{{ skuProduct.svipPriceTag }}</view>
										</view>
									</template>
								</view>
							</view>
						</view>
					</view>
					<view class="tui-sku-unit">
						<view class="sku-unit-h1">规格：</view>
						<view class="sku-unit-li">
							<view
								class="unit-li"
								v-for="(sku, index) in skuList"
								@click="handleChoisSku(sku, index)"
								:key="index"
								:class="skuIndex === index ? 'active' : ''"
							>
								{{ sku.unit }} <text class="tips" v-if="sku.stock === 0">缺货</text>
							</view>
						</view>
					</view>
					<view class="sku-unit-ladel" v-if="isShowLadder" @click.stop="showLaddePopup">
						此规格商品存在阶梯价格，点击查看 >>>
					</view>
				</view>
				<view class="tui-right-flex tui-popup-btn" :style="{ paddingBottom: isIphoneX ? '68rpx' : '34rpx' }">
					<view class="tui-modal-flex">
						<button
							class="tui-modal-button confirm"
							:class="[isBtnDisabled ? 'disabled' : '']"
							@click="handleConfirm"
						>
							确定
						</button>
					</view>
				</view>
			</view>
		</tui-bottom-popup>
		<!-- 阶梯价弹窗 -->
		<cmLadderPopup v-if="laddePopupShow" :show="laddePopupShow" :list="ladderPriceList"></cmLadderPopup>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import cmLadderPopup from '@/components/cm-module/productDetails/cm-ladder-popup.vue'
export default {
	name: 'cm-unit-popup',
	components: {
		cmLadderPopup
	},
	props: {
		skuProduct: {
			type: Object
		},
		popupShow: {
			type: Boolean,
			default: false
		},
		type: {
			type: Number,
			default: 1
		}
	},
	data() {
		return {
			laddePopupShow: false,
			isShowLadder: false,
			skuIndex: 0,
			ladderPriceList: [],
			skuList: [],
			isBtnDisabled:false,
			handleUnit:'',
			handleSkuId:0,
			handleMinNumber:1
		}
	},
	filters: {
		NumFormat(value) {
			//处理金额
			return Number(value).toFixed(2)
		}
	},
	created() {
		this.initData()
	},
	computed: {
		...mapState(['hasLogin'])
	},
	methods: {
		initData() {
			console.log('skuProduct',this.skuProduct)
			this.skuList = this.skuProduct.skus
			this.handleUnit = this.skuList[0].unit
			this.handleSkuId = this.skuList[0].skuId
			this.handleMinNumber = this.skuList[0].minBuyNumber
			this.isBtnDisabled = this.skuList[0].stock === 0
			if (this.skuList[0].ladderPriceList) {
				this.isShowLadder = true
			}
		},
		PromotionsFormat(promo) {
			//促销活动类型数据处理
			if (promo != null) {
				if (promo.type == 1 && promo.mode == 1) {
					return true
				} else {
					return false
				}
			}
			return false
		},
		handleConfirm() {
			const data = { unit: this.handleUnit,skuId : this.handleSkuId,minBuyNumber:this.handleMinNumber}
			this.$emit('handleUnitConfirm', data)
			this.$parent.popupShow = false
		},
		handleChoisSku(sku, index) {
			// 选择SKU
			this.skuIndex = index
			this.handleUnit = sku.unit
			this.handleSkuId = sku.skuId
			this.ladderPriceList = sku.ladderPriceList
			this.isShowLadder = sku.ladderPriceList ? true : false
			this.isBtnDisabled = sku.stock === 0
			this.$emit('skuClick', sku)
		},
		showLaddePopup() {
			// 点击显示阶梯价
			this.laddePopupShow = true
		},
		hidePopup() {
			//隐藏
			this.$parent.popupShow = false
		}
	}
}
</script>

<style lang="scss">
.tui-popup-box {
	padding: 40rpx 24rpx 0 24rpx;
}
.tui-shopping-main {
	width: 100%;
	.tui-sku-title {
		width: 100%;
		height: 136rpx;
		float: left;
		margin-bottom: 30rpx;
		.tui-sku-image {
			width: 138rpx;
			height: 138rpx;
			float: left;
			border-radius: 8rpx;
			margin-right: 30rpx;
			box-sizing: border-box;
			border: 1px dashed #e1e1e1;
			image {
				width: 134rpx;
				height: 134rpx;
				border-radius: 10rpx;
			}
		}
		.tui-sku-price {
			height: 136rpx;
			float: left;
			.sku-price-viw {
				width: 100%;
				height: 40rpx;
				margin-bottom: 24rpx;
				.sku-price-text {
					font-size: 28rpx;
					line-height: 40rpx;
					color: #f94b4b;
					font-weight: bold;
					.sku-price-l {
						float: left;
						font-weight: normal;
					}
					&.none {
						text-decoration: line-through;
						color: #999999;
						font-weight: normal;
					}
				}
			}
			.sku-price-vip {
				width: 100%;
				height: 40rpx;
			}
		}
	}
	.tui-sku-unit {
		width: 100%;
		height: auto;
		float: left;
		padding-bottom: 30rpx;
		.sku-unit-h1 {
			font-size: 28rpx;
			line-height: 40rpx;
			color: #333333;
			font-weight: bold;
		}
		.sku-unit-li {
			width: 100%;
			height: auto;
			.unit-li {
				padding: 0 24rpx;
				line-height: 48rpx;
				text-align: center;
				font-size: 24rpx;
				color: #666666;
				background: #f5f5f5;
				float: left;
				margin-right: 16rpx;
				margin-top: 12rpx;
				margin-bottom: 12rpx;
				border-radius: 24rpx;
				position: relative;
				box-sizing: border-box;
				border: 1px solid #f5f5f5;
				&.active {
					border-color: $color-system;
					background: #fff1eb;
					color: $color-system;
					.tips {
						background: #e15616;
					}
				}
				.tips {
					padding: 0 10rpx;
					line-height: 32rpx;
					text-align: center;
					font-size: 22rpx;
					color: #ffffff;
					background: #cccccc;
					float: left;
					border-radius: 16rpx;
					position: absolute;
					right: -12rpx;
					top: -15rpx;
				}
			}
		}
	}
	.sku-unit-ladel {
		width: 100%;
		height: 64rpx;
		line-height: 64rpx;
		float: left;
		margin-top: 20rpx;
		text-align: center;
		font-size: 24rpx;
		background: #fffaf8;
		color: #e15616;
		margin-bottom: 30rpx;
	}
}
.tui-modal-flex {
	width: 100%;
	height: 84rpx;
	margin-top: 40rpx;
	display: flex;
	.tui-modal-button {
		flex: 1;
		line-height: 84rpx;
		font-size: $font-size-28;
		text-align: center;
		border-radius: 42rpx;
		padding: 0;
		margin: 0 15rpx;
		box-sizing: border-box;
		&.cancel {
			background: #ffe6dc;
			color: #e15616;
		}
		&.confirm {
			background: $btn-confirm;
			color: #ffffff;
			&.disabled {
				background: linear-gradient(135deg, rgba(242, 143, 49, 0.5) 0%, rgba(225, 86, 22, 0.5) 100%);
			}
		}
	}
}
</style>
