<template>
  <view class="goods-detail">
    <!-- 轮播图 -->
    <view class="banner-container">
      <uv-swiper 
      indicator
       indicatorMode="dot"
       indicatorActiveColor="blue"
      height="700rpx"
      :loading="!imageList.length"
      :list="imageList"></uv-swiper>
    </view>
    
    <!-- 商品信息 -->
    <view class="goods-info">
      <!-- 积分信息 -->
      <view class="points-section">
        <image src="/static/积分图标.png" class="points-icon" mode="aspectFit"></image>
        <text class="points-text">{{ goodsData.price }}积分</text>
      </view>
      
      <!-- 商品标题 -->
      <view class="title-section">
        <text class="goods-title">{{ goodsData.title }}</text>
      </view>
      
    </view>
    
    <!-- 商品详情独立容器 -->
    <view class="detail-container">
      <!-- 商品详情标题 -->
      <view class="detail-title-section">
        <rich-text :nodes="goodsData.details"></rich-text>
      </view>
      
      <!-- 商品图集 -->
      <!-- <view class="gallery-section">
        <view class="gallery-grid">
          <image 
            v-for="(img, index) in goodsData.image.split(',')" 
            :key="index"
            :src="img" 
            class="gallery-image" 
            mode="aspectFill"
            @click="previewImage(img, goodsData.image.split(','))"
          ></image>
        </view>
      </view> -->
    </view>
    
    <!-- 底部操作栏 -->
    <view class="bottom-bar">
      <view class="stock-info">
        <text class="stock-text"> {{ goodsData.inventory }}/{{ goodsData.sales }}</text>
        <text class="stock-label">库存</text>
      </view>
      
      <view class="favorite-section" @click="toggleFavorite">
        <uv-icon name="heart" size="24" :color="goodsData.isCollection === 1 ? 'red' : '#cccccc'"></uv-icon>
        <text class="favorite-text">收藏</text>
      </view>
      
      <view class="exchange-btn" @click="onExchange">
        <text class="exchange-text">申请兑换</text>
      </view>
    </view>
    <GlobalPopup ref="globalPopupRef" />
  </view>

</template>

<script>
export default {
  name: 'GoodsDetail',
  data() {
    return {
      goodsId: '',
      goodsData: {
        id: 1,
        name: '哪吒之魔童闹海新款首套装哪吒校内艺术手办树脂摆件学生小礼品',
        points: 100,
        category: '积分兑换',
        exchangeCount: 120,
        stock: 50,
        description: '这是一款美味的薄脆小饼干，口感酥脆，营养丰富。采用优质原料制作，无添加剂，适合全家人享用。每一口都能感受到浓郁的香味和酥脆的口感，是您休闲时光的最佳选择。',
        gallery: [
          '/static/商城_商品1.png',
          '/static/商城_商品2.png',
          '/static/bannerImage.png',
          '/static/商城_商品1.png',
          '/static/商城_商品2.png',
          '/static/bannerImage.png'
        ]
      }
    }
  },
  computed: {
    imageList() {
      if (this.goodsData.image) {
        return this.goodsData.image.split(',')
      }
      return []
    }
  },
  onLoad(options) {
    if (options.id) {
      this.goodsId = options.id;
      this.getGoodsDetail(options.id);
    }
  },
  methods: {
    async getGoodsDetail(id) {
      let params = {}
      if (uni.getStorageSync('token')) {
        params.token = uni.getStorageSync('token')
      }
      const res = await this.$api.shop.queryGoodsById({ goodsId: id, ...params })
      this.goodsData = res.result
    },

    async toggleFavorite() {
      const res = await this.$api.shop.collectionGoods({ goodsId: this.goodsId })
      uni.showToast({
        title: res.message,
        icon: res.code === 200 ? 'success' : 'none'
      });
      this.getGoodsDetail(this.goodsId)
    },
    
    previewImage(current, urls) {
      uni.previewImage({
        current: current,
        urls: urls
      });
    },
    
    onExchange() {
      uni.showModal({
        title: '确认兑换',
        content: `确定要用${this.goodsData.price}积分兑换${this.goodsData.title}吗？`,
        success: async (res) => {
          if (res.confirm) {
            try{
              const res2 = await this.$api.shop.buyGoods({
                goodsId: this.goodsId
              })
              this.$refs.globalPopupRef.open({
                content: '恭喜您兑换成功！',
                subContent: '请及时到社区领取商品！',
                titleType: 'exchange',
                popupType: 'success',
                closefn: () => {
                  setTimeout(() => {
                    uni.navigateBack()
                  }, 300);
                }
              })

              
            }catch (error) {
              console.log(error);
              
              if(error.code === 500 && error.message === '商品库存不足，兑换失败'){
                this.$refs.globalPopupRef.open({
                  content: '库存不足！',
                  subContent: '库存不足暂时不能兑换哦！',
                  titleType: 'exchange',
                  popupType: 'fail',
                  closefn: () => {
                    setTimeout(() => {
                      uni.navigateBack()
                    }, 300);
                  }
                })
              }else if(error.code === 500 && error.message === '积分不足，兑换失败'){
                this.$refs.globalPopupRef.open({
                  content: '您的积分不够哦！',
                  subContent: '可以参与更多活动获取积分哦！',
                  titleType: 'exchange',
                  popupType: 'fail',
                  closefn: () => {
                    setTimeout(() => {
                      uni.navigateBack()
                    }, 300);
                  }
                })
              }
            }
          }
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-detail {
  background: #f8f8f8;
  min-height: 100vh;
  padding-bottom: 120rpx; // 为底部固定栏留出空间
}

.banner-container {
  height: 700rpx;
  .banner-swiper {
    width: 100%;
    
    .banner-image {
      width: 100%;
      height: 100%;
    }
  }
}

.goods-info {
  background: #ffffff;
  margin-top: 20rpx;
  padding: 30rpx;
}

.points-section {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
  
  .points-icon {
    width: 32rpx;
    height: 32rpx;
    margin-right: 8rpx;
  }
  
  .points-text {
    font-size: 32rpx;
    font-weight: bold;
    color: #1488DB;
  }
}

.title-section {
  margin-bottom: 40rpx;
  
  .goods-title {
    font-size: 28rpx;
    color: #333333;
    line-height: 1.5;
    display: block;
  }
}

.detail-container {
  background: #ffffff;
  margin-top: 20rpx;
  padding: 30rpx;
  margin-bottom: 120rpx;
}

.detail-title-section {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 30rpx;
  
  .detail-title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333333;
  }
}

.gallery-section {
  margin-bottom: 40rpx;
  
  .gallery-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16rpx;
    
    .gallery-image {
      width: 100%;
      height: 200rpx;
      border-radius: 12rpx;
      border: 1rpx solid #f0f0f0;
    }
  }
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  padding: 20rpx 30rpx;
  border-top: 1rpx solid #f0f0f0;
  z-index: 999;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  .stock-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .stock-text {
      font-size: 24rpx;
      color: #1488DB;
      margin-bottom: 4rpx;
      font-weight: bold;
    }
    
    .stock-label {
      font-size: 22rpx;
      color: #999999;
    }
  }
  
  .favorite-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .favorite-text {
      font-size: 22rpx;
      color: #999999;
      margin-top: 4rpx;
    }
  }
  
  .exchange-btn {
    flex: 2;
    margin-left: 50rpx;
    height: 80rpx;
    border-radius: 40rpx;
    background-color: #1488DB;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .exchange-text {
      color: #ffffff;
      font-size: 28rpx;
      // font-weight: bold;
    }
  }
}
</style>