<template>
  <view class="detail-container">
    <common-header title="活动详情" theme="activity" @back="goBack" />
    
    <view class="detail-content">
      <view class="detail-card">
        <view class="detail-header">
          <text class="detail-title">{{ activityData.title || '活动标题' }}</text>
          <view class="status-badge" :class="{ 
            'draft': activityData.status === 0,
            'published': activityData.status === 1,
            'active': activityData.status === 2,
            'finished': activityData.status === 3
          }">
            {{ getStatusText(activityData.status) }}
          </view>
        </view>
        
        <view class="detail-body">
          <view class="detail-item">
            <text class="item-label">活动描述</text>
            <text class="item-value">{{ activityData.description || '暂无描述' }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">活动时间</text>
            <text class="item-value">{{ formatTime(activityData.startTime) }} - {{ formatTime(activityData.endTime) }}</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">参与人数</text>
            <text class="item-value">{{ activityData.participantCount || 0 }}人</text>
          </view>
          
          <view class="detail-item">
            <text class="item-label">创建时间</text>
            <text class="item-value">{{ formatTime(activityData.createTime) }}</text>
          </view>
        </view>
        
        <view class="detail-actions">
          <uni-button type="default" size="small" @click="editActivity">编辑</uni-button>
          <uni-button type="primary" size="small" @click="publishActivity" v-if="activityData.status === 0">发布</uni-button>
        </view>
      </view>
    </view>
  </view>
</template>

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

export default {
  name: 'ActivityDetail',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      activityData: {},
      loading: false
    }
  },
  
  onLoad(option) {
    const { id } = option
    if (id) {
      this.getActivityDetail(id)
    }
  },
  
  methods: {
    async getActivityDetail(id) {
      this.loading = true
      try {
        // TODO: Replace with actual API call
        const response = await this.$http.get(`/brewery/activity/${id}`)
        this.activityData = response.data || {}
      } catch (error) {
        console.error('获取活动详情失败:', error)
        this.$modal.showToast('获取活动详情失败')
      } finally {
        this.loading = false
      }
    },
    
    getStatusText(status) {
      const statusMap = {
        0: '草稿',
        1: '已发布',
        2: '进行中',
        3: '已结束'
      }
      return statusMap[status] || '未知'
    },
    
    formatTime(time) {
      if (!time) return ''
      return time.substring(0, 16)
    },
    
    editActivity() {
      uni.navigateTo({
        url: `/subpages/activity/edit?id=${this.activityData.id}`
      })
    },
    
    publishActivity() {
      uni.showModal({
        title: '提示',
        content: '确定要发布此活动吗？',
        success: (res) => {
          if (res.confirm) {
            this.doPublishActivity()
          }
        }
      })
    },
    
    async doPublishActivity() {
      try {
        await this.$http.post(`/brewery/activity/${this.activityData.id}/publish`)
        this.$modal.showToast('发布成功')
        this.activityData.status = 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;
  
  &.draft {
    background: #f5f5f5;
    color: #666;
  }
  
  &.published {
    background: #e8f5e8;
    color: #52c41a;
  }
  
  &.active {
    background: #fff2e8;
    color: #fa8c16;
  }
  
  &.finished {
    background: #f0f0f0;
    color: #999;
  }
}

.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> 