<template>
  <view class="detail-container">
    <common-header title="产品详情" theme="newproduct" @back="goBack" />
    
    <view class="detail-content">
      <view class="detail-card">
        <view class="detail-header">
          <text class="detail-title">{{ productData.name || '产品名称' }}</text>
          <view class="status-badge" :class="{ 
            'online': productData.status === 1,
            'offline': productData.status === 0,
            'pending': productData.status === 2
          }">
            {{ getStatusText(productData.status) }}
          </view>
        </view>
        
        <view class="detail-body">
          <view class="detail-item">
            <text class="item-label">产品分类</text>
            <text class="item-value">{{ getCategoryText(productData.category) }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">产品价格</text>
            <text class="item-value">¥{{ productData.price || 0 }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">产品描述</text>
            <text class="item-value">{{ productData.description || '暂无描述' }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">产品特色</text>
            <text class="item-value">{{ productData.features || '暂无特色' }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">上线时间</text>
            <text class="item-value">{{ formatTime(productData.launchTime) }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">创建时间</text>
            <text class="item-value">{{ formatTime(productData.createTime) }}</text>
          </view>
        </view>
        
        <view class="detail-actions">
          <uni-button type="default" size="small" @click="editProduct">编辑</uni-button>
          <uni-button type="primary" size="small" @click="toggleStatus" v-if="productData.status === 0">上线</uni-button>
          <uni-button type="warn" size="small" @click="toggleStatus" v-if="productData.status === 1">下线</uni-button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import CommonHeader from '@/components/common-header/common-header.vue'

export default {
  name: 'NewProductDetail',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      productData: {},
      loading: false
    }
  },
  
  onLoad(option) {
    const { id } = option
    if (id) {
      this.getProductDetail(id)
    }
  },
  
  methods: {
    async getProductDetail(id) {
      this.loading = true
      try {
        // TODO: Replace with actual API call
        const response = await this.$http.get(`/brewery/newproduct/${id}`)
        this.productData = response.data || {}
      } catch (error) {
        console.error('获取产品详情失败:', error)
        this.$modal.showToast('获取产品详情失败')
      } finally {
        this.loading = false
      }
    },
    
    getStatusText(status) {
      const statusMap = {
        0: '待上线',
        1: '已上线',
        2: '审核中'
      }
      return statusMap[status] || '未知'
    },
    
    getCategoryText(category) {
      const categoryMap = {
        liquor: '白酒',
        wine: '红酒',
        beer: '啤酒',
        cocktail: '鸡尾酒',
        other: '其他'
      }
      return categoryMap[category] || '未知'
    },
    
    formatTime(time) {
      if (!time) return ''
      return time.substring(0, 16)
    },
    
    editProduct() {
      uni.navigateTo({
        url: `/subpages/newproduct/edit?id=${this.productData.id}`
      })
    },
    
    toggleStatus() {
      const isOnline = this.productData.status === 1
      const action = isOnline ? '下线' : '上线'
      
      uni.showModal({
        title: '提示',
        content: `确定要${action}此产品吗？`,
        success: (res) => {
          if (res.confirm) {
            this.doToggleStatus(isOnline)
          }
        }
      })
    },
    
    async doToggleStatus(isOnline) {
      try {
        const action = isOnline ? 'offline' : 'online'
        await this.$http.post(`/brewery/newproduct/${this.productData.id}/${action}`)
        this.$modal.showToast(`${isOnline ? '下线' : '上线'}成功`)
        this.productData.status = isOnline ? 0 : 1
      } catch (error) {
        console.error('操作失败:', error)
        this.$modal.showToast('操作失败')
      }
    },
    
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-container {
  min-height: 100vh;
  background: #f5f7fa;
}

.detail-content {
  padding: 20rpx;
}

.detail-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 30rpx;
  padding-bottom: 20rpx;
  border-bottom: 1px solid #f0f0f0;
}

.detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.status-badge {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  
  &.online {
    background: #e8f5e8;
    color: #52c41a;
  }
  
  &.offline {
    background: #f0f0f0;
    color: #999;
  }
  
  &.pending {
    background: #fff2e8;
    color: #fa8c16;
  }
}

.detail-body {
  .detail-item {
    margin-bottom: 24rpx;
    
    .item-label {
      display: block;
      font-size: 28rpx;
      color: #666;
      margin-bottom: 8rpx;
    }
    
    .item-value {
      font-size: 30rpx;
      color: #333;
      line-height: 1.5;
    }
  }
}

.detail-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 30rpx;
  padding-top: 20rpx;
  border-top: 1px solid #f0f0f0;
}
</style> 