<template>
  <view class="detail-container">
    <common-header title="返利详情" theme="rebate" @back="goBack" />
    
    <view class="detail-content">
      <view class="detail-card">
        <view class="detail-header">
          <text class="detail-title">{{ rebateData.userName || '用户返利' }}</text>
          <view class="status-badge" :class="{ 
            'pending': rebateData.status === 0,
            'approved': rebateData.status === 1,
            'paid': rebateData.status === 2,
            'rejected': rebateData.status === 3
          }">
            {{ getStatusText(rebateData.status) }}
          </view>
        </view>
        
        <view class="detail-body">
          <view class="detail-section">
            <text class="section-title">返利信息</text>
            <view class="detail-item">
              <text class="item-label">用户名称</text>
              <text class="item-value">{{ rebateData.userName || '暂无' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">返利金额</text>
              <text class="item-value amount">¥{{ rebateData.amount || 0 }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">返利类型</text>
              <text class="item-value">{{ getTypeText(rebateData.type) }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">返利比例</text>
              <text class="item-value">{{ rebateData.percentage || 0 }}%</text>
            </view>
            <view class="detail-item">
              <text class="item-label">关联订单</text>
              <text class="item-value">{{ rebateData.orderNo || '无' }}</text>
            </view>
          </view>
          
          <view class="detail-section">
            <text class="section-title">其他信息</text>
            <view class="detail-item">
              <text class="item-label">返利描述</text>
              <text class="item-value">{{ rebateData.description || '暂无描述' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">返利时间</text>
              <text class="item-value">{{ formatTime(rebateData.rebateTime) }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">创建时间</text>
              <text class="item-value">{{ formatTime(rebateData.createTime) }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">更新时间</text>
              <text class="item-value">{{ formatTime(rebateData.updateTime) }}</text>
            </view>
          </view>
        </view>
        
        <view class="detail-actions">
          <uni-button type="primary" size="small" @click="approveRebate" v-if="rebateData.status === 0">审批</uni-button>
          <uni-button type="warn" size="small" @click="rejectRebate" v-if="rebateData.status === 0">拒绝</uni-button>
          <uni-button type="primary" size="small" @click="payRebate" v-if="rebateData.status === 1">支付</uni-button>
        </view>
      </view>
    </view>
  </view>
</template>

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

export default {
  name: 'RebateDetail',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      rebateData: {},
      loading: false
    }
  },
  
  onLoad(option) {
    const { id } = option
    if (id) {
      this.getRebateDetail(id)
    }
  },
  
  methods: {
    async getRebateDetail(id) {
      this.loading = true
      try {
        // TODO: Replace with actual API call
        const response = await this.$http.get(`/brewery/rebate/${id}`)
        this.rebateData = 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] || '未知'
    },
    
    getTypeText(type) {
      const typeMap = {
        order: '订单返利',
        referral: '推荐返利',
        loyalty: '忠诚返利',
        promotion: '促销返利',
        other: '其他返利'
      }
      return typeMap[type] || '未知'
    },
    
    formatTime(time) {
      if (!time) return ''
      return time.substring(0, 16)
    },
    
    approveRebate() {
      uni.showModal({
        title: '确认审批',
        content: '确定要审批此返利记录吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateRebateStatus(1)
          }
        }
      })
    },
    
    rejectRebate() {
      uni.showModal({
        title: '确认拒绝',
        content: '确定要拒绝此返利记录吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateRebateStatus(3)
          }
        }
      })
    },
    
    payRebate() {
      uni.showModal({
        title: '确认支付',
        content: '确定要支付此返利吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateRebateStatus(2)
          }
        }
      })
    },
    
    async updateRebateStatus(status) {
      try {
        // TODO: Replace with actual API call
        await this.$http.put(`/brewery/rebate/${this.rebateData.id}/status`, { status })
        this.$modal.showToast('操作成功')
        this.rebateData.status = status
      } 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;
  
  &.pending {
    background: #fff2e8;
    color: #fa8c16;
  }
  
  &.approved {
    background: #e6f7ff;
    color: #1890ff;
  }
  
  &.paid {
    background: #e8f5e8;
    color: #52c41a;
  }
  
  &.rejected {
    background: #ffebee;
    color: #f44336;
  }
}

.detail-body {
  .detail-section {
    margin-bottom: 30rpx;
    
    .section-title {
      display: block;
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 20rpx;
      padding-bottom: 12rpx;
      border-bottom: 2rpx solid #f0f0f0;
    }
    
    .detail-item {
      display: flex;
      margin-bottom: 16rpx;
      
      .item-label {
        flex: 0 0 160rpx;
        font-size: 26rpx;
        color: #666;
      }
      
      .item-value {
        flex: 1;
        font-size: 26rpx;
        color: #333;
        line-height: 1.5;
        
        &.amount {
          font-weight: bold;
          color: #ff9500;
        }
      }
    }
  }
}

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