<template>
<view class="flex-col page" :style="{ backgroundImage: 'url(' + bkgPubilcPath + ')' }">
<!--  <image
		mode="aspectFit"
    class="self-stretch image"
    :src="goodObject.goodImg"
  /> -->
	<swiper class="swiper-box">
		<swiper-item v-for="(item,index) in imgArr" :key="index" >
			<image mode="aspectFill" class="self-stretch image" :src="item" @tap="previewImage(index)"></image>
		</swiper-item>
	</swiper>
  <view class="flex-col self-stretch section_2">
    <view class="self-start group">
      <text class="font text_2">￥</text>
      <text class="text">{{ goodObject.price.toFixed(2) }}</text> 
			<text class="discountsText">限时优惠{{ coupon.toFixed(2) }}元</text>
    </view>
    <view class="flex-row justify-between items-center self-stretch group_2">
      <text class="text_4">{{ goodObject.name }}</text>
      <view class="flex-row items-center group_3" v-if="festivalName !== '无'">
        <image
          class="image_2" mode="aspectFill"
          :src="publicPath + festivalUrl"
        />
        <text class="font text_3 ml-5">{{ festivalName }}限定</text>
      </view>
    </view>
    <view class="self-stretch divider view"></view>
    <view class="flex-col self-stretch group_4">
      <text class="self-stretch font_2">
        商品简介：{{ goodObject.intro }}
        <br/>
      </text>
    </view>
    <view class="self-stretch divider view_2"></view>
    <view class="flex-row items-center self-stretch group_5">
      <text class="font text_5">商品标签:</text>
      <view class="flex-col justify-start items-center text-wrapper ml-8" v-for="(item,index) in labelList" :key="index">
        <text class="font_3 text_6">#{{ item }}</text>
      </view>
    </view>
  </view>
  
  <view class="book-selected">
	<rich-text :nodes="richTextcontent"></rich-text>
  </view>

  
  
<!--  <view class="flex-col self-stretch section_3">
    <text class="self-start font_4 text_10">用户须知</text>
    <text class="self-start font text_11">1.发货后不接受退货、不接受7天无理由退货</text>
    <text class="self-start font text_12">2.商品购买后无法自主退款，需联系客服进行退款处理</text>
    <text class="self-stretch font_2 text_13">
      3.定制款商品购买后不接受退货退款，包售后需要录制开箱视频，并且开箱视频需要从拆包装开始
    </text>
    <text class="self-start font text_14">4.商品售罄后的款式也可以重新预定，需等待并联系客服</text>
    <text class="self-start font text_15">5.定制商品的具体发货日期根据制作团队制作情况而定</text>
    <text class="self-start font text_16">6.只售后邮寄破损</text>
  </view> -->
<!--  <image
    class="self-start image_3"
    :src="goodObject.detailImg"
  /> -->
  <view class="flex-row items-center self-stretch section_4">
    <button class="flex-col items-center" style="background-color: #ffffff;" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
      <image
        class="image_4"
        :src="storeHomeUrl + '/ProductDetails/lxkf.png'"
      />
      <text class="text_19 mt-3">客服</text>
    </button>
    <view class="flex-row flex-1 ml-34">
      <view class="flex-col justify-start items-center text-wrapper_4" @click="loadPop">
        <text class="font_4 text_17">加入购物车</text>
      </view>
      <view class="flex-col justify-start items-center text-wrapper_5 ml-9" @click="jump_buy">
        <text class="font_4 text_18">立即购买</text>
      </view>
    </view>
  </view>
</view>
<uni-popup ref="popup" background-color="#fff">
	<view>
		<addProduct></addProduct>
	</view>
</uni-popup>
</template>

<script setup >
import {nextTick, onMounted, onUnmounted, ref} from 'vue'
import { onLoad , onShow } from "@dcloudio/uni-app";
import { getFonts } from '../../../common/globalFont';
import { baseUrl } from '../../../api/request';
import  emitter from '../../../utils/emitter'
import addProduct from '../../Shopping-cart/component/addProduct.vue';
import { dealResult } from '../../../common/globalFunction';
import { storeHomeUrl,bkgPubilcPath } from '../../../common/globalImagesUrl';
import { publicPath } from '../../../common/globalImagesUrl';


const popup = ref(null)  //弹窗对象
const goodObject = ref({})  //商品对象
const idInfo = ref(0) //发送到下一页的商品id
const userInfo = ref({}) //用户信息
const labelList = ref([])  //标签
const imgArr = ref([])  //商品图片数组
const coupon = ref(0)  //当前商品可用的优惠卷
const cookie = wx.getStorageSync('cookie')
const richTextcontent = ref('')
const bkgUrl = ref(storeHomeUrl + '/ProductDetails/bkg.png')
const festivalName = ref('')
const festivalUrl = ref('')
//转换上一页面传来的商品
onLoad((options) => {
	getFonts()
	getProduct(JSON.parse(options.gid))  //页面加载的时候获取商品信息
})
onShow(()=>{ 
	userInfo.value = wx.getStorageSync('userInfo')  //获取用户信息
})

const closeHandler = () => {
	close()
}

onMounted(()=>{
	limitedTimeOffer()
	emitter.on('close', closeHandler)
})

onUnmounted(() => {
	emitter.off('close', closeHandler)
})


const getProduct = async (gid) =>{    //根据商品获取商品信息
	const res = await uni.request({
		url: baseUrl + '/goods/getById',
		method: 'POST',
		data: { id: gid },
		header: {
			cookie
		}
	})
	console.log('请求请求--->',res.data);
	if(res.data.code === 1) {
		goodObject.value = res.data.data
		richTextcontent.value = goodObject.value.richText
		console.log(richTextcontent.value)
		imgArr.value = res.data.data.goodImg.split(';')  //将后端传来的图片字符串转为图片数组
		imgArr.value = imgArr.value.map((e)=>{
			return publicPath + e
		})
		labelList.value = goodObject.value.label.split(';')  //分割字符串，生成标签数组
		labelList.value = labelList.value.filter((s)=>{  //使用filter将最后一个空值去掉
		 return s 
		})
		idInfo.value = goodObject.value.id
		festivalName.value = goodObject.value.festivalName.split(';')[0]
		festivalUrl.value = goodObject.value.festivalName.split(';')[1]
		console.log('=================>', festivalUrl.value)
	}
}
const loadPop =()=>{
	popup.value.open('bottom')  //从底部弹出
	emitter.emit('product',goodObject.value)
}
//关闭弹窗
const close =()=>{
	nextTick(() => {
		if (popup.value) {
			popup.value.close()
		}
	})
}
//将商品对象发送到下一个页面
const jump_buy = async ()=> {
	let res = await checkGeneralGood()
	if (!dealResult(res)) return 
	uni.navigateTo({
		url: '../../../pages/order/singleGoodOrder/singleGoodOrder?cartInfo=' + JSON.stringify(idInfo.value)
	})
}


//点击查看图片详情方法
const previewImage = (index) => {
	// console.log(url);
	uni.previewImage({
		current: index,
		urls:  imgArr.value
	})
}

const limitedTimeOffer = async () => {  //查询限时优惠接口
	const res = await uni.request({
		url: baseUrl + '/global/query/coupon/price',
		method: 'GET',
		header: {
			cookie
		}
	})
	console.log('res--->',res.data);
	if(res.data.code === 1) {
		coupon.value = res.data.data
	}
}











const checkGeneralGood = async () => {
	const res = await uni.request({
		url: baseUrl + '/goods/checkGeneralGood',
		method: 'POST',
		header: {
			cookie
		},
		data: {
			id: idInfo.value
		}
	})
	return res
}
</script>

<style lang="scss" scoped>
	
.book-selected {
  background-color: #ffffff;
  border-radius: 18.75rpx;
  width: 726rpx;
  height: auto;
  padding: 20rpx;
  margin: 20rpx auto 160rpx;
}
	
button::after {
	border: none;
}
.ml-5 {
  margin-left: 9.38rpx;
}
.mt-3 {
  margin-top: 5.63rpx;
}
.ml-9 {
  margin-left: 16.88rpx;
}
.page {
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: 100vh;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
}
.image {
  width: 100%;
  height: 100%;
}
.section_2 {
  padding-left: 18.75rpx;
  padding-top: 19.2rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
  width: 726rpx;
  margin: 0 auto;
}
.group {
  margin-left: 14.16rpx;
  line-height: 30.06rpx;
}
.font {
  font-size: 26.25rpx;
  font-family: 'FangZhengFonts';
  line-height: 25.84rpx;
  color: #323232;
}
.text_2 {
  color: #ff0000;
  line-height: 16.91rpx;
}
.text {
  color: #ff0000;
  font-size: 45rpx;
  font-family: 'FangZhengFonts';
  line-height: 30.06rpx;
}
.group_2 {
  margin-top: 19.11rpx;
}
.text_4 {
  color: #323232;
  font-size: 33.75rpx;
  font-family: 'FangZhengFonts';
  line-height: 32.7rpx;
}
.group_3 {
  margin-right: 19.63rpx;
}
.image_2 {
  width: 43.75rpx;
  height: 43.75rpx;
  border-radius: 50%;
}
.text_3 {
  font-size: 28.13rpx;
  line-height: 27.24rpx;
}
.divider {
  background-color: #cdcdcd;
  height: 1.88rpx;
}
.view {
  margin-right: 41.27rpx;
  margin-top: 18.28rpx;
}
.group_4 {
  padding: 10.56rpx 1rpx 32.04rpx 0;
}
.font_2 {
  font-size: 26.25rpx;
  font-family: 'FangZhengFonts';
  line-height: 31.88rpx;
  color: #323232;
}
.view_2 {
  margin-right: 41.27rpx;
}
.group_5 {
  padding: 16.88rpx 0 20.63rpx;
}
.text_5 {
  color: #323232;
  line-height: 24.62rpx;
}
.text-wrapper {
  padding: 7.26rpx 10rpx 10.31rpx 10rpx;
  background-color: #fbdedf;
  border-radius: 9.38rpx;
  // width: 90rpx;
  height: 39.38rpx;
}
.font_3 {
  font-size: 22.5rpx;
  font-family: 'FangZhengFonts';
  line-height: 21.71rpx;
  color: #323232;
}
.text_6 {
  line-height: 21.81rpx;
}
.text-wrapper_2 {
  padding: 7.35rpx 0 10.31rpx;
  background-color: #fbdedf;
  border-radius: 9.38rpx;
  height: 39.38rpx;
}
.text_7 {
  margin-left: 11.78rpx;
  margin-right: 8.83rpx;
}
.text-wrapper_1 {
  padding: 9.22rpx 0 9.15rpx;
  background-color: #fbdedf;
  border-radius: 9.38rpx;
  width: 105rpx;
  height: 39.38rpx;
}
.text_8 {
  line-height: 21rpx;
}
.text-wrapper_3 {
  padding: 9.13rpx 0 8.27rpx;
  background-color: #fbdedf;
  border-radius: 9.38rpx;
  width: 105rpx;
  height: 39.38rpx;
}
.text_9 {
  line-height: 21.98rpx;
}
.section_3 {
  margin-top: 35.63rpx;
  padding: 18.51rpx 11.72rpx 43.29rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.font_4 {
  font-size: 30rpx;
  font-family: 'FangZhengFonts';
  color: #ffffff;
}
.text_10 {
  color: #323232;
  line-height: 28.01rpx;
  text-decoration: underline;
}
.text_11 {
  margin-left: 10.14rpx;
  margin-top: 21.23rpx;
  line-height: 25.74rpx;
}
.text_12 {
  margin-left: 9.53rpx;
  margin-top: 19.46rpx;
  line-height: 25.54rpx;
}
.text_13 {
  margin: 16.44rpx 15.69rpx 0 9.62rpx;
}
.text_14 {
  margin-left: 9.41rpx;
  margin-top: 21.36rpx;
}
.text_15 {
  margin-left: 9.62rpx;
  margin-top: 24.9rpx;
  line-height: 25.63rpx;
}
.text_16 {
  margin-left: 10.14rpx;
  margin-top: 23.01rpx;
}
.image_3 {
  margin: 20.88rpx 0 120rpx;
  width: 100vw;
  height: 100vw;
}
.section_4 {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
  margin-top: 31.88rpx;
  padding: 13.13rpx 26.25rpx 16.88rpx;
  background-color: #ffffff;
}
.image_4 {
  width: 46.88rpx;
  height: 46.88rpx;
}
.text_19 {
  color: #9d2624;
  font-size: 22.5rpx;
  font-family: Open Sans;
  line-height: 20.72rpx;
}
.text-wrapper_4 {
  padding: 20.38rpx 0 22.63rpx;
  flex: 1 1 283.13rpx;
  background-color: #e79ea1;
  border-radius: 93.75rpx;
  height: 71.25rpx;
}
.text_17 {
  line-height: 28.24rpx;
}
.text-wrapper_5 {
  padding: 20.51rpx 0 22.97rpx;
  flex: 1 1 283.13rpx;
  background-color: #c35c5d;
  border-radius: 93.75rpx;
  height: 71.25rpx;
}
.text_18 {
  line-height: 27.77rpx;
}
.swiper-box {
  width: 100%;
  height: calc(150vw * 9 / 16);
}
.discountsText {
	color: #ff0000;
	font-size: 25rpx;
	font-family: 'FangZhengFonts';
	line-height: 30.06rpx;
	margin-left: 20rpx
}
@import url(../../../common/css/global.css);
</style>