<template>
	<page-meta :page-style="themeColor"></page-meta>
	<view class="bundling_goods_BOX">
		<!-- 商品媒体信息 -->
		<view class="goods-media">
			<!-- 商品图片 -->
			<view class="goods-img" :class="{ show: switchMedia == 'img' }">
				<swiper class="swiper" @change="swiperChange" :interval="swiperInterval" :autoplay="swiperAutoplay"
					autoplay="false" interval="4000" circular="true">
					<swiper-item v-for="(item, index) in bundlingIMG" :key="index" :item-id="'goods_id_' + index">
						<view class="item" @click="previewMedia(index)">
							<image :src="$util.img(item)" mode="scaleToFill" />
						</view>
					</swiper-item>
				</swiper>
				<view class="img-indicator-dots">
					<text>{{ swiperCurrent }}</text>
					<text v-if="bundlingIMG">/{{ bundlingIMG.length }}</text>
				</view>
			</view>
		</view>

		<view class="bundlingCard">
			<view class="detailCard_nav" v-if="userInfo.show_price&&token">
				<view class="bl_price">
					套餐价 &nbsp;¥
					<text class="price">{{ combo.bl_price }}</text>
				</view>
				<view class="goods_money">原价 &nbsp;¥{{ combo.goods_money }}</view>
			</view>
			<view class="detailCard_con">
				<view class="combo_nav">
					<view class="bl_name">{{ combo.bl_name }}</view>
					<div style="display: flex;align-items: ceter;" v-if="userInfo.show_price&&token">
						<view slot="minus" class="minus" @click.stop="numChange(num, 'minus')">
							<u-icon name="minus-circle-fill" size="20" color="var(--base-color)"></u-icon>
						</view>
						<text slot="input" style="width: 32px;text-align: center;" class="input-number">
							{{ num || 0 }}
						</text>
						<view slot="plus" class="minus" @click.stop="numChange(num, 'add')">
							<u-icon name="plus-circle-fill" size="20" color="var(--base-color)"></u-icon>
						</view>
					</div>
				</view>

				<view class="combo_detail">
					<view>套餐包含商品数：{{ combo.bundling_goods_count }}件</view>
					<view>运费：{{ combo.shipping_fee_type==1  ? '包邮' : '自费' }}</view>
				</view>
			</view>
			<view class="detailCard_footer"></view>
		</view>

		<view class="combo-package" :class="isIphoneX ? 'combo-iphonex' : ''">
			<view class="combo-package-content">
				<view class="combo-package-name color-title"><!-- {{ combo.bl_name }} --></view>

				<view v-for="(item, index) in combo.bundling_goods" :key="index" class="goods-info"
					@click="toGoodsDetail(item)">
					<view class="goods-img">
						<view class="img-wrap">
							<image :src="$util.img(item.sku_image, { size: 'mid' })" @error="imageError(index)"
								mode="aspectFit" />
						</view>
					</view>
					<view class="data-info">
						<view class="goods-name">{{ item.sku_name }}</view>
						<view class="cradBOX">
							<view class="left">库存:{{ item.stock }}</view>
						</view>
						<view class="price-wrap" v-if="userInfo.show_price&&token">
							<text class="unit price-style small">
								{{ $lang('common.currencySymbol') }}
							</text>
							<text class="price price-style large">
								{{
									parseFloat(item.price)
										.toFixed(2)
										.split('.')[0]
								}}
							</text>
							<text class="unit price-style small">
								.{{
									parseFloat(item.price)
										.toFixed(2)
										.split('.')[1]
								}}
							</text>
							<text class="num">x{{ num }}</text>
						</view>
						<view class="stock-tips color-base-text" v-if="item.stock < num">
							库存不足
							<!-- ，剩余：{{ item.stock }}
							<block v-if="item.unit">{{ item.unit }}</block>
							<block v-else>件</block> -->
						</view>
					</view>
				</view>
			</view>

			<view class="footer" :class="isIphoneX ? 'padding-bottom' : ''" v-if="userInfo.show_price&&token">
				<view class="price-wrap">
					<text class="label">套餐价：</text>
					<text class="unit price-color">{{ $lang('common.currencySymbol') }}</text>
					<text class="price price-color">
						{{
							parseFloat(packagePrice)
								.toFixed(2)
								.split('.')[0]
						}}
					</text>
					<text class="unit price-color">
						.{{
							parseFloat(packagePrice)
								.toFixed(2)
								.split('.')[1]
						}}
					</text>

					<!-- <text class="info-num">为您节省：{{ $lang('common.currencySymbol') }} {{ saveThePrice }}</text> -->
				</view>
				<button class="footer-btn" v-if="isDisabled" type="primary" @click="comboBuy()" size="mini">
					立即购买
				</button>
				<button class="footer-btn" v-else disabled>立即购买</button>
			</view>
		</view>
		<view style="height: 120rpx;width:100%;"></view>

		<loading-cover ref="loadingCover"></loading-cover>
		<ns-login ref="login"></ns-login>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex';
	export default {
		data() {
			return {
				num: 1, //购买数量
				blId: 0, //商品id
				combo: [],
				bundlingIMG: [],
				packagePrice: [], //套餐价
				saveThePrice: 0, //节省价格
				isDisabled: false, //按钮失效
				isIphoneX: false, //判断是否是iphoneX以上的设备

				// 套餐轮播图
				swiperInterval: 1,
				swiperAutoplay: true,
				swiperCurrent: 1,
				switchMedia: 'img',
				token: uni.getStorageSync("token")
			};
		},
		onLoad(e) {
			this.blId = e.bl_id || 0;
			this.isIphoneX = this.$util.uniappIsIPhoneX();
		},
		onShow() {
			setTimeout(() => {
				if (this.addonIsExist && !this.addonIsExist.bundling) {
					this.$util.showToast({
						title: '商家未开启组合套餐',
						mask: true,
						duration: 2000
					});
					setTimeout(() => {
						this.$util.redirectTo('/pages/index/index');
					}, 2000);
					return;
				}
			}, 1000);

			this.getDetail();
		},
		onHide() {
			this.btnSwitch = true;
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		methods: {
			// 获取套餐详情
			getDetail() {
				this.bundlingIMG = [];
				this.$api.sendRequest({
					url: '/bundling/api/bundling/detail',
					data: {
						bl_id: this.blId
					},
					success: res => {
						console.log(res);
						if (res.data) {
							this.combo = res.data;
							console.log(this.combo);
							res.data.bundling_goods.map(item => {
								this.bundlingIMG.push(item.sku_image);
							});
							this.numberChange();
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			toGoodsDetail(e) {
				this.$util.redirectTo('/page_goods/detail/detail', { goods_id: e.goods_id });
			},
			numberChange(flag, callback) {
				setTimeout(() => {
					var disabledCount = 0;

					// 防止空
					if (flag && this.num.length == 0) {
						this.num = 1;
						disabledCount++;
					}

					// 防止输入0和负数、非法输入
					if (flag && (this.num <= 0 || isNaN(this.num))) {
						this.number = 1;
						disabledCount++;
					}

					if (flag) this.num = parseInt(this.num);

					var price = 0;
					for (var i = 0; i < this.combo.bundling_goods.length; i++) {
						price += parseFloat(this.combo.bundling_goods[i].price);
						//检测库存
						if (this.combo.bundling_goods[i].stock < this.num) disabledCount++;
					}
					this.isDisabled = !(disabledCount > 0);

					this.saveThePrice = ((price - this.combo.bl_price) * this.num).toFixed(2);
					this.packagePrice = (this.combo.bl_price * this.num).toFixed(2);
					if (callback) callback();
				}, 0);
			},
			// 套餐立即购买点击
			async comboBuy() {
				uni.removeStorageSync('buyer_message');
				if (!this.isDisabled) return;
				//纠正数量
				this.numberChange(true, () => {
					if (!uni.getStorageSync('token')) {
						this.$refs.login.open('/pages_promotion/bundling/detail?bl_id=' + this.blId);
						return;
					}

					if (this.btnSwitch == false) return;

					this.btnSwitch = false;

					var data = {
						bl_id: this.blId,
						num: this.num
					};

					uni.setStorage({
						key: 'comboOrderCreateData',
						data: data,
						success: () => {
							this.$util.redirectTo('/pages_promotion/bundling/payment');
							this.btnSwitch = true;
						}
					});
				});
			},
			imageError(index) {
				this.combo.bundling_goods[index].sku_image = this.$util.getDefaultImage().goods;
				this.$forceUpdate();
			},
			swiperChange(e) {
				this.swiperCurrent = e.detail.current + 1;
			},
			// 预览图片
			previewMedia(index) {
				var paths = [];
				for (let i = 0; i < this.bundlingIMG.length; i++) {
					paths.push(
						this.$util.img(this.bundlingIMG[i])
					);
				}
				uni.previewImage({
					current: index,
					urls: paths
					// longPressActions: {
					// 	itemList: ['发送给朋友', '保存图片', '关注'],
					// 	success: function(data) {
					// 		console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
					// 	},
					// 	fail: function(err) {
					// 		console.log(err.errMsg);
					// 	}
					// }
				});
			},

			numChange(num, type) {
				let setp = 1;
				if (this.num > 1) {
					this.num ? this.num : (this.num = 0);
					type == 'add' ? (this.num += setp) : (this.num -= setp);
					this.numberChange();
				} else if ((this.num = 1)) {
					type == 'add' ? (this.num += setp) : (this.num = 1);
					this.numberChange();
				}
				this.$forceUpdate();
			}
		},
		onShareAppMessage(res) {
			var title = '购买套餐，优惠多多哦';
			var path = '/pages_promotion/combo/detail?bl_id=' + this.blId;
			return {
				title: title,
				path: path,
				success: res => {},
				fail: res => {}
			};
		}
	};
</script>

<style lang="scss">
	.bundling_goods_BOX {

		// 商品媒体信息
		.goods-media {
			width: 90vw;
			height: 90vw;
			margin: 30rpx auto;
			position: relative;

			.goods-img,
			.goods-video {
				width: 100%;
				height: 100%;
				position: relative;

				video {
					width: 100%;
					height: 100%;
				}

				.img-indicator-dots {
					text-align: center;
					width: 80rpx;
					height: 42rpx;
					line-height: 42rpx;
					position: absolute;
					bottom: 30rpx;
					right: 30rpx;
					background: #000000;
					border-radius: 25rpx;
					opacity: 0.3;

					text {
						color: #fff;
					}
				}
			}

			.swiper {
				width: 100%;
				height: 100%;

				.item {
					width: 100%;
					height: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.bundlingCard {
			width: 95%;
			margin: 0 auto;
			height: 260rpx;
			background: #ffffff;
			border-radius: $border-radius;

			.detailCard_nav {
				background: var(--goods-btn-color);
				width: 100%;
				height: 100rpx;
				border-radius: $border-radius $border-radius 0 0;
				display: flex;

				.bl_price {
					height: 100rpx;
					line-height: 100rpx;
					padding-left: 30rpx;
					font-size: 24rpx;
					color: #ffffff;

					.price {
						padding-left: 0rpx;
						font-size: 48rpx;
					}
				}

				.goods_money {
					height: 100rpx;
					line-height: 110rpx;
					padding-left: 30rpx;
					font-size: 24rpx;
					color: #ffffff;
					text-decoration: line-through;
				}
			}

			.detailCard_con {
				padding: 0 30rpx;

				.combo_nav {
					margin: 20rpx 0;
					display: flex;
					justify-content: space-between;

					.bl_name {
						font-size: 32rpx;
						font-weight: 600;
					}

					.input-number {
						line-height: 30rpx;
					}
				}

				.combo_detail {
					display: flex;
					justify-content: space-between;

					view {
						font-size: 28rpx;
						color: #999999;
					}
				}
			}

			.detailCard_footer {}
		}

		.combo-package {
			border-radius: $border-radius;
			background: #ffffff;
			width: 95%;
			margin: 30rpx auto;

			&.combo-iphonex {
				margin-bottom: 230rpx;
			}

			.combo-package-content {
				// padding-bottom: 40rpx;

				.combo-package-name {
					padding: 20rpx 30rpx;
				}

				.goods-info {
					overflow: hidden;
					padding: $padding 30rpx;
					border-radius: $border-radius;

					.goods-img {
						display: inline-block;
						width: 30%;
						text-align: center;
						line-height: 100%;
						float: left;

						.img-wrap {
							display: inline-block;
							width: 180rpx;
							height: 180rpx;
							text-align: center;
							border-radius: $border-radius;

							image {
								height: 100%;
								width: 100%;
								vertical-align: middle;
							}
						}
					}

					.data-info {
						display: inline-block;
						width: 66%;
						float: left;
						position: relative;
						margin-left: 20rpx;
						height: 180rpx;

						.goods-name {
							overflow: hidden;
							text-overflow: ellipsis;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							display: -webkit-box;
							line-height: 150%;
							// margin-bottom: 20rpx;
						}

						.cradBOX {
							display: flex;
							justify-content: space-between;

							view {
								font-size: $font-size-tag;
								color: #999;
							}
						}

						.stock-tips {
							position: absolute;
							width: 100%;
							bottom: 50rpx;
							height: 40rpx;
							line-height: 40rpx;
							font-size: $font-size-tag;
						}

						.price-wrap {
							height: 50rpx;
							line-height: 50rpx;
							position: absolute;
							width: 100%;
							bottom: 0;

							.unit {
								font-weight: bold;
								font-size: $font-size-tag;
								margin-right: 4rpx;
							}

							.price {
								font-weight: bold;
								font-size: $font-size-toolbar;
							}

							.num {
								float: right;
							}
						}
					}
				}
			}

			.footer {
				width: 100%;
				background: #fff;
				display: flex;
				justify-content: space-between;
				padding: 20rpx 24rpx;
				box-sizing: border-box;
				position: fixed;
				bottom: 0;
				left: 0;

				.price-wrap {
					flex: 1;
					text-align: right;
					vertical-align: middle;
					line-height: 70rpx;
					margin-right: 30rpx;

					.label {
						font-size: $font-size-base;
					}

					.unit {
						font-weight: bold;
						font-size: $font-size-tag;
						margin-right: 4rpx;
					}

					.price {
						font-weight: bold;
						font-size: $font-size-toolbar;
					}

					.info-num {
						font-size: $font-size-activity-tag;
						color: $color-tip;
						line-height: 1;
						margin-top: 12rpx;
					}
				}

				.footer-btn {
					display: flex;
					align-items: center;
					justify-content: center;
					height: 70rpx;
					line-height: 70rpx;
				}
			}

			.padding-bottom {
				padding-bottom: 80rpx;
			}
		}
	}
</style>