<template>
	<view class="shop-detail-page">
		<HeaderCom></HeaderCom>
		<scroll-view scroll-y="true" class="scroll-view">
			<swiper class="swiper" circular indicator-dots>
				<swiper-item v-for="(item,index) in detailData.rollingImagesResource" :key="index" class="swiper-item">
					<view class="swiper-img">
						<img :src="item.url" style="width: 100%;height: 750rpx;" alt="" />
						<!-- <image :src="item.url" mode="aspectFit"></image> -->
					</view>
				</swiper-item>
			</swiper>
			<view class="detail">
				<view class="name">{{detailData.name||''}}</view>
				<view class="info">
					<image src="@/static/an/point.gif" mode="" class="point-icon"></image>
					<view class="point">{{detailData.mallPoint||''}}</view>
					<view class="price">¥{{detailData.price.sale||0}}</view>
				</view>
				<view class="freight">{{feeRule}}</view>
			</view>
			<view class="introduce">
				<view class="title">详情介绍</view>
				<!-- <img :src="detailData.detailImagesResource[0].url" alt="" style="width: 100%;"> -->
				<template v-for="(item,index) in detailData.detailImagesResource">
					<img :src="item.url" mode="aspectFit" :key="index" style="width: 100%;"></img>
				</template>
			</view>
			<view class="footer">
				<view class="left">
					<view class="left-item" @click="goHome">
						<image src="@/static/shop/sy.png" mode="aspectFit" class="icon"></image>
						首页
					</view>
					<view class="left-item" @click="handleCustomer">
						<image src="@/static/shop/kf.png" mode="aspectFit" class="icon"></image>
						客服
					</view>
					<view class="left-item" @click="copy">
						<image src="@/static/shop/fx.png" mode="aspectFit" class="icon"></image>
						分享
					</view>
				</view>
				<view class="purchase" @click="buy">立即抢购</view>
			</view>
		</scroll-view>
		
		<popup-view v-model="show" mode="bottom" border-radius="15" zIndex="1">
			<view class="popup-main">
				<view class="popup-header">
					<image :src="detailData.coverResource.url" mode="" class="popup-header-img"></image>
					<view class="popup-info">
						<view class="popup-info-name">{{detailData.name}}</view>
						<view class="popup-info-price">
							<view class="popup-clover">
								<image src="@/static/an/point.gif" mode="" class="clover-img"></image>
								{{detailData.mallPoint}}
							</view>
							
							<view class="popup-price">
								<text class="popup-symbol">¥</text>{{detailData.price.sale}}
							</view>
						</view>
					</view>
				</view>
				<view class="popup-footer">
					<view class="footer-num">
						<view class="footer-info">
							购买数量
						</view>
						<view class="item-step">
							<image src="@/static/mine/sub.png" class="item-sub" mode="" @click="sub"></image>
							<view class="item-num">{{quantity}}</view>
							<image src="@/static/mine/add.png" class="item-add" mode="" @click="add"></image>
						</view>
					</view>
					<view class="now-buy" @click="confirmBuy">立即支付</view>
				</view>
			</view>
		</popup-view>
	</view>
</template>

<script>
	
	import { myMixins } from "@/mixins/mixins.js";
	import { productDetailApi,favorItemApi,delFavorItemApi } from '@/API/product.js'
	import { carriageRule } from '@/API/shop.js'
	
	import {
		customerApi
	} from '@/API/user.js'
	import HeaderCom from '@/pages/components/header.vue'
	export default {
	mixins: [myMixins],
		data() {
			return {
				id:"",
				show: false, //立即购买弹框
				quantity:1,
				detailData:{
					detailImages:"",
					price:{},
					coverResource:{}
				},//商品详情
				scroll:0,
				footerType:"",//是否现实底部
				customer:'',
				feeRule:''
			}
		},
		components:{HeaderCom},
		onLoad(opation) {
			this.id = opation.id
			this.footerType = opation.footer ? opation.footer : ''
			this.getDetail()
			// this.getdeliveryFee()
			if (uni.getStorageSync('box-token')) {
				this.getCustomer()
			}
			this.getCarriageRule()
		},
		onPageScroll(res){
			this.scroll = res.scrollTop
		},
		methods: {
			getCarriageRule(){
				carriageRule().then(res => {
					if(res.code === 200){
						this.feeRule = res.data
					}else{
						this.$api.msg(res.message)
					}
				})
			},
			copy() {
				
				let search = window.location.href
				uni.setClipboardData({
					data: `${search}`,
				})
				console.log(search);

			},
			handleCustomer() {
				window.open(this.customer)
			},
			// 获取详情
			getDetail(){
				uni.showLoading()
				productDetailApi({id:this.id}).then(res => {
					uni.hideLoading()
					if(res.code === 200){
						// res.data.rollingImagesResource.unshift(res.data.coverResource)
						this.detailData = res.data
					}else{
						this.$api.msg(res.message)
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
			goBack() {
				uni.navigateBack()
			},
			// 滚动到顶部
			goTop() {
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0 // number number number！
				});
			},
			// 是否收藏
			collect() {
				uni.showLoading()
				if(this.detailData.favorite){
					delFavorItemApi({productId:this.detailData.id}).then(res => {
						uni.hideLoading()
						if(res.code === 200){
							this.$api.msg('取消收藏')
							this.getDetail()
						}else{
							this.$api.msg(res.message)
						}
					}).catch(err => {
						uni.hideLoading()
					})
				}else{
					favorItemApi({productId:this.detailData.id}).then(res => {
						uni.hideLoading()
						if(res.code === 200){
							this.$api.msg('收藏成功')
							this.getDetail()
						}else{
							this.$api.msg(res.message)
						}
					}).catch(err => {
						uni.hideLoading()
					})
				}
			},
			getCustomer() {
				customerApi().then(res => {
					if (res.code === 200) {
						this.customer = res.data
					} else {
						this.$api.msg(res.message)
					}
				})
			},
			// 减
			sub(){
				if(this.quantity === 1){
					this.$api.msg("不能小于1！");
					return;
				}
				if(this.quantity > 1){
					this.quantity--
				}
			},
			// 加
			add(){
				this.quantity++
			},
			// 立即购买弹框
			buy() {
				// uni.navigateTo({
				// 	url:`/pages/shop/confirm?id=${this.id}&quantity=${this.quantity}`
				// })
				this.quantity = 1
				this.show = true
			},
			// 确认购买
			confirmBuy(){
				// 清空购买地址
				uni.removeStorageSync('address')
				this.show = false;
				uni.navigateTo({
					url:`/pages/shop/confirm?id=${this.id}&quantity=${this.quantity}`
				})
				this.quantity = 1
				// uni.navigateTo({
				// 	url:`/pages/order/confirm?id=${this.id}&quantity=${this.quantity}`
				// })
			},
			goHome(){
				uni.switchTab({
					url:`/pages/index/new-index`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #F5F5F5;
	}
	.shop-detail-page{
		// padding-top: 112rpx;
		.scroll-view{
			
			.swiper{
				width: 750rpx;
				height: 750rpx;
				// height: 480rpx;
				.swiper-item{
					width: 750rpx;
					height: 750rpx;
					// height: 480rpx;
					.swiper-img{
						width: 750rpx;
						height: 750rpx;
						// height: 480rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						background: #F5F5F5;
					}
				}
			}
			.detail{
				width: 750rpx;
				background: #FFFFFF;
				padding: 24rpx 32rpx 32rpx 32rpx;
				.name{
					width: 686rpx;
					font-weight: 500;
					font-size: 28rpx;
					color: #666666;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.info{
					display: flex;
					align-items: center;
					margin-top: 16rpx;
					.point-icon{
						width: 48rpx;
						height: 48rpx;
					}
					.point{
						margin-left: 16rpx;
						font-weight: 800;
						font-size: 40rpx;
						color: #000000;
					}
					.price{
						margin-left: 24rpx;
						font-weight: 500;
						font-size: 28rpx;
						color: #999999;
						text-decoration: line-through;
					}
				}
				.freight{
					font-weight: 500;
					font-size: 24rpx;
					color: #999999;
					margin-top: 4rpx;
				}
			}
			.introduce{
				width: 100%;
				margin-top: 16rpx;
				padding-bottom: 144rpx;
				background-color: #fff;
				.title{
					padding: 24rpx 32rpx;
					font-weight: 800;
					font-size: 28rpx;
					color: #000000;
				}
			}
			.footer{
				position: fixed;
				bottom: 0;
				width: 100%;
				height: 144rpx;
				padding: 24rpx 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #fff;
				.left{
					display: flex;
					align-items: center;
					
					
					.left-item{
						display: flex;
						align-items: center;
						justify-content: center;
						flex-direction: column;
						font-weight: 500;
						font-size: 20rpx;
						color: #666666;
						margin-right: 56rpx;
						.icon{
							width: 48rpx;
							height: 48rpx;
							margin-bottom: 8rpx;
						}
					}
				}
				.purchase{
					width: 328rpx;
					height: 96rpx;
					background: linear-gradient( 90deg, #39B2FF 0%, #3354FF 100%), #D9D9D9;
					border-radius: 8rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: bold;
					font-size: 32rpx;
					color: #FFFFFF;
				}
			}
		}
		.popup-main {
			padding: 30rpx 22rpx;
			// padding: 32rpx 32rpx 0rpx 32rpx;
			z-index: 9991;
			// max-height: 900rpx;
			// position: relative;
			.popup-header {
				display: flex;
				align-items: center;
				height: 158rpx;
		
				.popup-header-img {
					width: 158rpx;
					height: 158rpx;
					border-radius: 10rpx;
				}
		
				.popup-info {
					margin-left: 25rpx;
					.popup-info-name {
						
						width: 482rpx;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						font-weight: bold;
						font-size: 28rpx;
						color: #333333;
					}
		
					.popup-info-price {
						margin-top: 60rpx;
						display: flex;
						align-items: center;
		
						.popup-price {
							display: flex;
							align-items: center;
							font-weight: 300;
							font-size: 16rpx;
							color: #888888;
							margin-left: 20rpx;
							height: 50rpx;
							display: flex;
							align-items: flex-end;
							padding-bottom: 8rpx;
							text-decoration: line-through;
							.popup-symbol {
								// font-size: 20rpx;
							}
						}
		
						.popup-clover {
							display: flex;
							align-items: center;
							font-weight: bold;
							font-size: 42rpx;
							color: #000000;
							height: 50rpx;
		
							.clover-img {
								width: 48rpx;
								height: 48rpx;
								margin-right: 6rpx;
							}
						}
					}
				}
			}
		
			.popup-title {
				color: #333333;
				font-weight: 800;
				font-size: 28rpx;
				margin-top: 48rpx;
				margin-bottom: 24rpx;
			}
			.specification-list{
				// display: flex;
				// align-items: center;
				// flex-wrap: wrap;
				
				width: 100%;
				max-height: 600rpx;
				margin-bottom: 50rpx;
				padding-bottom: 220rpx;
				overflow-y: auto;
				.specification-item {
					z-index: 9999;
					display: inline-block;
					padding: 0 64rpx;
					// width: 200rpx;
					height: 56rpx;
					line-height: 56rpx;
					border: 2rpx solid #B3B3BE;
					border-radius: 28rpx;
					// display: flex;
					// align-items: center;
					// justify-content: center;
					font-size: 24rpx;
					color: #B3B3BE;
					margin-right: 32rpx;
					margin-bottom: 32rpx;
				}
				.specification-item-active {
					border: 2rpx solid #333333;
					color: #333333;
				}
			}
			
			.popup-footer{
				z-index: 9999;
				background-color: #fff;
				// padding-bottom: 20rpx;
				margin-top: 30rpx;
				.footer-num{
					// display: flex;
					// align-items: center;
					// justify-content: space-between;
					z-index: 9999;
					margin-top: 20rpx;
					.footer-info{
						display: flex;
						align-items: center;
						font-size: 28rpx;
						font-weight: 800;
						color: #333333;
						.footer-info-num{
							margin-left: 10rpx;
							font-size: 24rpx;
							color: #999999;
							font-weight: 400;
						}	
					}
					.item-step {
						margin-top: 17rpx;
						z-index: 9999;
						display: flex;
						align-items: center;
					
						.item-sub {
							width: 41rpx;
							height: 41rpx;
						}
					
						.item-num {
							width: 78rpx;
							font-size: 28rpx;
							color: #333333;
							display: flex;
							align-items: center;
							justify-content: center;
						}
					
						.item-add {
							width: 41rpx;
							height: 41rpx;
						}
					}
				}
				.now-buy{
					z-index: 9999;
					margin-top: 46rpx;
					
					
					width: 706rpx;
					height: 88rpx;
					background: linear-gradient( 90deg, #39B2FF 0%, #3354FF 100%), #D9D9D9;
					border-radius: 16rpx;
					
					
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 32rpx;
					color: #FFFFFF;
				}
			}
		}
		.sss{
			width: 100%;
			background-size: cover;background-repeat: no-repeat;
		}
	}
</style>