<template>
	<view class='product-page' v-show="productInfo!=null">
		<view class='p-banner' v-show="goodsImage!=''">
			<image :src='goodsImage'  class='banner-item'></image>
		</view>
		<view class="p-box">
			<view class='box-top'>
				<!-- <view class='top-text-s' v-if="goodsName.length>5">{{goodsName}}</view> -->
				<view class='top-text' >{{goodsName}}</view>
				<view class='top-btn'>在售</view>
			</view>
			<view class='box-footer'>
				<view class='footer-l'><text>¥</text>{{goodsMoney}}</view>
				<view class='footer-r'>{{lookSum}}次浏览</view>
			</view>
		</view>
		<view class='p-detail' >
			<view class='det-text'>{{goodsDesc}}</view>
			<view class="det-imglist" >
				<view class='list-item' v-for="item in bannerList" :key="item.id">
					<image class='img-f' :src='item'></image>
				</view>
				
			</view>
		</view>
		<view class='p-footer'>
			<view class='footer-l'>
				<view class='l-attention' @click="Clickatten">
					<view :class="follow ?'a-icon-show':'a-icon' "></view>
					<view class='a-text' v-if="follow==false">关注</view>
					<view class='a-texts' v-else>已关注</view>
				</view>
				<view class='l-tel' @click='callTel'>
					<view class="tel-icon"></view>
					<view class='tel-text'>电话</view>
				</view>
			</view>
			<view class='footer-r' @click='Gopurse'>立即购买</view>
		</view>
		
		<!-- 信息确认弹框 -->
		<view-modal :show="showback" @cancel="cancel" :showCancel="true" class='infrom-modal' :title="' '">
			<view class='modal-header'>
				<view class='header-l'>
					<image :src='goodsImage'></image>
				</view>
				<view class="header-r">
					<view class='r-prise'>
						<text class='prise-icon'>¥</text>{{goodsMoney}}
					</view>
					<view class='r-num'>库存{{stock}}件</view>
					<view class='r-text'>选择数量</view>
				</view>
			</view>
			<view class='modal-mian'>
				<view class='main-number'>
					<view class='number-text'>购买数量</view>
					<view class='number-line'>
						<view class='line-minus' @click='minusCount'></view>
						<view class='line-count'>{{quantity}}</view>
						<view class='line-add' @click="addCount"></view>
					</view>
				</view>
			</view>
			<view class='modal-btn' @click="buySub">
				立即购买
			</view>
		</view-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showback:false,
				quantity:1,
				bannerList:[],
				goodId:0,
				productInfo:null,
				goodsImage:'',
				goodsName:'',
				goodsMoney:0,
				stock:0,
				lookSum:0,
				goodsDesc:'',
				token:'',
				mobileNum:'',
				follow:false
			}
		},
		onLoad(options) {
			const token=uni.getStorageSync('token')
			this.token=token
			if(options){
				const { goodId, }=JSON.parse(options.mydata)
				console.log(goodId,'options')
				this.goodId=goodId
				this.init()
				this.initTel()
			}
		},
		methods: {
			init(){
				const self=this
				
				const params={
					  "goodId": self.goodId
				}
				self.$request('/pawn/category/getGoodsDetail','POST',params).then(res => {
					console.log(res.result,'index');
					const { goodsImage,goodsDetailImages,goodsName,stock,lookSum,goodsMoney,goodsDesc,follow}=res.result
					self.productInfo=res.result
					self.goodsImage=goodsImage
					self.goodsMoney=goodsMoney
					self.goodsName=goodsName
					self.stock=stock
					self.lookSum=lookSum
					self.goodsDesc=goodsDesc
					self.bannerList=JSON.parse(goodsDetailImages)
					self.follow=follow
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			initTel(){
				const self=this
				self.$request('/pawn/setting','GET').then(res => {
				
					const { protocol,phone }=res.result
					
					self.mobileNum=phone
					console.log(self.mobileNum,'00')
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			buySub(){
				if(this.token==''){
					this.Go('uni:/pages/login/login')
				}else{
					if(this.quantity==0){
						this.toast('商品数量不能为0')
						return false
					}
					this.Go('uni:/pages/detailResult/detailResult','redirect')
				}
			},
			Gopurse(){
				this.showback=true
			},
			addCount(){
				console.log(this.quantity,'8888')
				if(this.quantity>=this.stock){
					uni.showToast({
						title: '超过库存量了',
						icon: "none",
						duration: 2000
					})
					return false
				}else{
					this.quantity++;
				}
			},
			minusCount(){
				if(this.quantity==0){
					uni.showToast({
						title: '已经是最少的了',
						icon: "none",
						duration: 2000
					})
					this.quantity=1
					return false
				}else{
					this.quantity--;
				}
			},
			
			cancel(){
				this.showback=false
			},
			callTel(){
				if(this.token==''){
					this.Go('uni:/pages/login/login')
				}else{
					uni.makePhoneCall({
						phoneNumber: this.mobileNum//仅为示例
					});
				}
			},
			Clickatten(){
				if(this.token==''){
					this.Go('uni:/pages/login/login')
				}else{
					this.follow=!this.follow
					const self=this
					const params={
						  "goodId": self.goodId
					}
					self.$request('/pawn/category/followGood','POST',params).then(res => {
						console.log(res.result,'followGood');
						
					}).catch(err=>{
					  console.log(err,'请求出错')
					})
				}
				
			},
			bannerItem(item){
				console.log(item,'0988')
			}
		},
		onShow() {
			const token=uni.getStorageSync('token')
			this.token=token
		}
	}
</script>

<style src='./index.less' lang="less" scoped></style>
