<template>
  <view class="goods-detail-page">
    <!-- 商品轮播图 -->
    <swiper class="goods-swiper" :indicator-dots="true" :autoplay="true" circular>
      <swiper-item v-for="(image, index) in goodsInfo.images" :key="index">
        <image :src="image" class="swiper-image" mode="aspectFill"></image>
      </swiper-item>
    </swiper>
    
    <!-- 商品信息 -->
    <view class="goods-info-section">
      <view class="goods-price">¥{{ goodsInfo.price }}</view>
      <view class="goods-name">{{ goodsInfo.name }}</view>
      <view class="goods-subtitle">{{ goodsInfo.subtitle }}</view>
    </view>
    
    <!-- 商品详情 -->
    <view class="goods-detail-section">
      <view class="section-title">商品详情</view>
      <view class="detail-content">
        <rich-text :nodes="goodsInfo.detail"></rich-text>
      </view>
    </view>
    
    <!-- 底部操作栏 -->
    <view class="bottom-bar">
      <view class="left-actions">
        <button class="action-btn" open-type="contact">
          <text class="icon">💬</text>
          <text class="text">客服</text>
        </button>
      </view>
      <view class="right-actions">
        <button class="buy-btn" @click="handleBuy">立即购买</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsId: null,
      goodsInfo: {
        images: [],
        name: '',
        subtitle: '',
        price: '0.00',
        detail: ''
      }
    }
  },
  
  onLoad(options) {
    this.goodsId = options.id;
    this.loadGoodsDetail();
  },
  
  methods: {
    // 加载商品详情
    async loadGoodsDetail() {
      try {
        const res = await this.$http.get('/api/goods/detail', {
          id: this.goodsId
        });
        
        if (res.code === 0) {
          this.goodsInfo = res.data;
          // 处理轮播图
          if (typeof this.goodsInfo.images === 'string') {
            this.goodsInfo.images = this.goodsInfo.images.split(',');
          }
        }
      } catch (error) {
        console.error('加载商品详情失败:', error);
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        });
      }
    },
    
    // 立即购买
    handleBuy() {
      // 检查登录
      const token = uni.getStorageSync('token');
      if (!token) {
        uni.navigateTo({
          url: '/pages/sub-pages/login/index'
        });
        return;
      }
      
      uni.showToast({
        title: '功能开发中',
        icon: 'none'
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-detail-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

.goods-swiper {
  width: 100%;
  height: 750rpx;
  
  .swiper-image {
    width: 100%;
    height: 100%;
  }
}

.goods-info-section {
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .goods-price {
    font-size: 48rpx;
    font-weight: bold;
    color: #FF4757;
    margin-bottom: 20rpx;
  }
  
  .goods-name {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    line-height: 1.4;
    margin-bottom: 15rpx;
  }
  
  .goods-subtitle {
    font-size: 26rpx;
    color: #999;
    line-height: 1.5;
  }
}

.goods-detail-section {
  background-color: #fff;
  padding: 30rpx;
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 30rpx;
  }
  
  .detail-content {
    line-height: 1.6;
  }
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  padding: 15rpx 30rpx;
  background-color: #fff;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
  
  .left-actions {
    margin-right: 20rpx;
    
    .action-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 0;
      background-color: transparent;
      border: none;
      font-size: 20rpx;
      color: #666;
      
      .icon {
        font-size: 40rpx;
        margin-bottom: 4rpx;
      }
      
      .text {
        font-size: 22rpx;
      }
    }
  }
  
  .right-actions {
    flex: 1;
    
    .buy-btn {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background: linear-gradient(90deg, #FF6B6B, #FF4757);
      color: #fff;
      border-radius: 40rpx;
      font-size: 32rpx;
      border: none;
    }
  }
}
</style>

