<template>
	<view :class="Visible ? 'product-popup open' : 'product-popup close'" @touchmove.stop.prevent=""
		@click="closePopup">
		<view class="popup-bg"></view>
		<view class="main" v-on:click.stop>
			<view class="header">
				<image :src="selectModel ? selectModel.image : ''" mode="aspectFit" class="avt"></image>
				<view class="price-color f24 ">
					{{currencyIcon()}}
					<text class="f36 fb">{{ formatNum(selectModel.price) }}</text>
				</view>
				<view class="stock">
					{{ formatNum(selectModel.achieve) }}
				</view>
				<view class="close-btn" @click="closePopup"><text class="icon iconfont icon-guanbi"></text></view>
			</view>
			<view class="body">
				<!--规格-->
				<view v-if="productModel">
					<scroll-view scroll-y="true" class="specs mt20" style="max-height: 600rpx;">
						<view class="f28">{{$t("w_0107")}}</view>
						<view class="specs mt20" v-for="(item, index) in productModel.specData" :key="index"
							@click="selectFunc(item.pkId)">
							<!-- <view class="specs-hd p-20-0">
								<text class="f28 gray3">{{ item.productName }} *{{ item.quantity }}</text>
							</view> -->
							<view class="specs-list">
								<view class="table-item" :class="{active:active_id == item.pkId}">
									<image class="table-item-img" :src="item.image" mode=""></image>
									<view class="table-item-name flex-1 text-ellipsis">{{ item.specValueNames }}</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
			<view class="btns"><button class="confirm-btn" @click="confirmFunc(form)">{{$t("N_I_192")}}</button></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/*是否可见*/
				Visible: false,
				/*表单对象*/
				form: {
					detail: {},
					show_sku: {
						sku_image: ''
					}
				},
				/*当前商品总库存*/
				stock: 0,
				/*选择提示*/
				selectSpec: '',
				/*是否打开过多规格选择框*/
				isOpenSpec: false,
				type: '',
				specAttr: '',
				selectModel: {}
			};
		},
		props: ['isPopup', 'productModel', 'active_id'],
		watch: {
			isPopup: function(n, o) {
				let self = this;
				if (n != o) {
					self.Visible = n;
					if (!this.isOpenSpec) {
						self.isOpenSpec = true;
						console.log(self.productModel)
						self.initShowSku();
					}
				}
			},
			active_id: function(n, o) {
				let self = this;
				if (n != o && self.productModel && self.productModel.specData) {
					self.productModel.specData.forEach((item, index) => {
						if (item.pkId == n) {
							self.selectModel = item
						}
					})
					console.log(self.selectModel)
				}
			}
		},
		methods: {
			selectFunc(e) {
				this.$emit('changeFunc', e);
			},
			/*初始化*/
			initShowSku() {
				let self = this;
				self.isOpenSpec = true;
				self.productModel.specData.forEach((item, index) => {
					if (item.pkId == self.active_id) {
						self.selectModel = item
					}
				})
			},

			/*关闭弹窗*/
			closePopup() {
				this.$emit('close', this.productModel.specData);
			},

			/*确认提交*/
			confirmFunc() {
				if (!this.active_id) {
					uni.showToast({
						title: this.$t("w_0126"),
						icon: 'none',
						duration: 2000
					});
					return;
				}
				this.$emit('close', this.productModel.specData);
				this.$emit('addCart', this.productModel.specData);
			},

			/*加入购物车*/
			addCart() {
				let self = this;
				let total_num = self.form.show_sku.sum;
				let product_num = this.form.show_sku.sum;
				let productStockId = this.form.show_sku.productStockId;
				if (self.form.detail.spec_type == 20 && productStockId == 0) {
					uni.showToast({
						title: this.$t("w_0126"),
						icon: 'none',
						duration: 2000
					});
					return false;
				}
				let params = Object.assign({}, self.form.detail, {
					shoppingid: '',
					sszq: '',
					num: self.form.show_sku.sum,
					productStockId: self.form.show_sku.productStockId,
					xh: 1
				});
				self.h_post('api/addShopping', params, function(res) {
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					self.$emit('close', null, res.data.cart_total_num);
				});
			},

			/*创建订单*/
			createdOrder() {
				let self = this;
				let product_num = this.form.show_sku.sum;
				let productStockId = this.form.show_sku.productStockId;
				this.form.show_sku.quantity = product_num;
				this.form.show_sku.product_attr = this.specAttr || '';
				this.form.show_sku.product_image = this.form.show_sku.sku_image || '';
				uni.setStorageSync('to_buy', [this.form.show_sku]);
				let jsonarray = [{
					productId: this.form.detail.pkid,
					productStockId: productStockId,
					quantity: product_num
				}];
				uni.setStorageSync('to_buy_confirm', jsonarray);
				this.gotoPage('/pages/order/confirm-order?order_type=buy&specialAreaId=' + this.form.specialAreaId);
			}
		}
	};
</script>

<style lang="scss">
	.product-popup {}

	.product-popup .popup-bg {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.6);
		z-index: 99;
	}

	.product-popup .main {
		position: fixed;
		width: 100%;
		bottom: 0;
		min-height: 200rpx;
		// max-height: 1050rpx;
		background-color: #fff;
		transform: translate3d(0, 980rpx, 0);
		transition: transform 0.2s cubic-bezier(0, 0, 0.25, 1);
		// bottom: env(safe-area-inset-bottom);
		z-index: 99;
		border-radius: 12rpx;
	}

	.product-popup.open .main {
		transform: translate3d(0, 0, 0);
		z-index: 99;
	}

	.product-popup.close .popup-bg {
		display: none;
	}

	.product-popup.close .main {
		display: none;
		z-index: -1;
	}

	.product-popup .header {
		height: 200rpx;
		padding: 40rpx 0 10rpx 250rpx;
		position: relative;
		border-bottom: 1px solid #eeeeee;
	}

	.product-popup .header .avt {
		position: absolute;
		top: 40rpx;
		left: 30rpx;
		width: 180rpx;
		height: 180rpx;
		border: 2px solid #ffffff;
		background: #ffffff;
		border-radius: 12rpx;
	}

	.product-popup .header .stock {
		font-size: 26rpx;
		color: #999999;
	}

	.product-popup .close-btn {
		position: absolute;
		width: 40rpx;
		height: 40rpx;
		top: 40rpx;
		right: 30rpx;
	}

	.product-popup .price {
		color: #333333;
		font-weight: bold;
		font-size: 24rpx;
	}

	.product-popup .price .num {
		padding: 0 4rpx;
		font-size: 40rpx;
	}

	.product-popup .old-price {
		margin-left: 10rpx;
		font-size: 26rpx;
		color: #999999;
		text-decoration: line-through;
	}

	.product-popup .body {
		padding: 20rpx 30rpx 39rpx 30rpx;
		// max-height: 600rpx;
		overflow-y: auto;
	}

	.product-popup .level-box {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.product-popup .level-box .key {
		font-size: 24rpx;
		color: #999999;
	}

	.product-popup .level-box .icon-box {
		width: 48rpx;
		height: 40rpx;
		background: #eee;
	}

	.product-popup .num-wrap .iconfont {
		color: #666;
	}

	.product-popup .num-wrap.no-stock .iconfont {
		color: #cccccc;
	}

	.product-popup .level-box .text-wrap {
		margin: 0 4rpx;
		height: 60rpx;
		border: none;
		background: #ffffff;
	}

	.product-popup .level-box .text-wrap input {
		padding: 0 10rpx;
		height: 60rpx;
		line-height: 60rpx;
		width: 80rpx;
		font-size: 32rpx;
		text-align: center;
	}

	.specs .specs-list {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;
	}

	.specs .specs-list button {
		margin-right: 10rpx;
		margin-bottom: 10rpx;
		font-size: 24rpx;
	}

	.specs .specs-list button:after,
	.product-popup .btns button,
	.product-popup .btns button:after {
		height: 72rpx;
		line-height: 72rpx;
		border: 0;
		border-radius: 0;
		margin-bottom: 55rpx;
	}

	.product-popup .btns .confirm-btn {
		width: 710rpx;
		height: 80rpx;
		background-color: #fb3024;
		border-radius: 40rpx;
		margin: 0 auto;
		margin-bottom: 55rpx;
		color: #ffffff;
		line-height: 80rpx;
		font-size: 32rpx;
	}

	.btn-checked {
		border: 1rpx solid;
		color: #fff;
		border-color: #fb3024;
		border-radius: 6rpx;
		font-size: 28rpx;
		background-color: #fb3024;
	}

	.btn-checke {
		border: 1rpx solid #f6f6f6;
		border-radius: 6rpx;
		font-size: 28rpx;
		color: #333333;
		background-color: #f6f6f6;
	}

	.table-item {
		width: 704rpx;
		height: 68rpx;
		background: #f6f6f6;
		border-radius: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 9rpx 23rpx 9rpx 17rpx;
		box-sizing: border;
		margin-bottom: 15rpx;
		color: #333;
	}

	.table-item.active {
		background-color: #fb3024;
		color: #ffffff;
	}

	.table-item-img {
		display: block;
		flex-shrink: 0;
		width: 50rpx;
		height: 50rpx;
		margin-right: 17rpx;
	}

	.table-item-name {
		font-size: 28rpx;
	}

	.table-num-box {
		width: 168rpx;
		height: 50rpx;
		background: #ffffff;
		margin-left: 20rpx;
		border-radius: 50rpx;
		padding: 0 9rpx;
		box-sizing: border-box;
	}

	.icon.icon-guanbi {
		color: #424242;
		font-size: 26rpx;
	}
</style>