<template>
  <view class="detail-container">
    <common-header title="订单详情" theme="order" @back="goBack" />
    
    <view class="detail-content">
      <view class="detail-card">
        <view class="detail-header">
          <text class="detail-title">{{ orderData.orderNo || '订单编号' }}</text>
          <view class="status-badge" :class="{ 
            'pending': orderData.status === 0,
            'processing': orderData.status === 1,
            'completed': orderData.status === 2,
            'cancelled': orderData.status === 3
          }">
            {{ getStatusText(orderData.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">{{ orderData.customerName || '暂无' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">联系电话</text>
              <text class="item-value">{{ orderData.phone || '暂无' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">收货地址</text>
              <text class="item-value">{{ orderData.address || '暂无' }}</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">{{ orderData.productName || '暂无' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">订单数量</text>
              <text class="item-value">{{ orderData.quantity || 0 }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">单价</text>
              <text class="item-value">¥{{ orderData.unitPrice || 0 }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">总金额</text>
              <text class="item-value amount">¥{{ orderData.totalAmount || 0 }}</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">{{ orderData.remark || '暂无备注' }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">创建时间</text>
              <text class="item-value">{{ formatTime(orderData.createTime) }}</text>
            </view>
            <view class="detail-item">
              <text class="item-label">更新时间</text>
              <text class="item-value">{{ formatTime(orderData.updateTime) }}</text>
            </view>
          </view>
        </view>
        
        <view class="detail-actions">
          <uni-button type="default" size="small" @click="editOrder">编辑</uni-button>
          <uni-button type="primary" size="small" @click="processOrder" v-if="orderData.status === 0">处理</uni-button>
          <uni-button type="primary" size="small" @click="completeOrder" v-if="orderData.status === 1">完成</uni-button>
          <uni-button type="warn" size="small" @click="cancelOrder" v-if="orderData.status < 2">取消</uni-button>
        </view>
      </view>
    </view>
  </view>
</template>

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

export default {
  name: 'OrderDetail',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      orderData: {},
      loading: false
    }
  },
  
  onLoad(option) {
    const { id } = option
    if (id) {
      this.getOrderDetail(id)
    }
  },
  
  methods: {
    async getOrderDetail(id) {
      this.loading = true
      try {
        // TODO: Replace with actual API call
        const response = await this.$http.get(`/brewery/order/${id}`)
        this.orderData = 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)
    },
    
    editOrder() {
      uni.navigateTo({
        url: `/subpages/order/edit?id=${this.orderData.id}`
      })
    },
    
    processOrder() {
      uni.showModal({
        title: '确认处理',
        content: '确定要处理此订单吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateOrderStatus(1)
          }
        }
      })
    },
    
    completeOrder() {
      uni.showModal({
        title: '确认完成',
        content: '确定要完成此订单吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateOrderStatus(2)
          }
        }
      })
    },
    
    cancelOrder() {
      uni.showModal({
        title: '确认取消',
        content: '确定要取消此订单吗？',
        success: (res) => {
          if (res.confirm) {
            this.updateOrderStatus(3)
          }
        }
      })
    },
    
    async updateOrderStatus(status) {
      try {
        // TODO: Replace with actual API call
        await this.$http.put(`/brewery/order/${this.orderData.id}/status`, { status })
        this.$modal.showToast('操作成功')
        this.orderData.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;
  }
  
  &.processing {
    background: #e6f7ff;
    color: #1890ff;
  }
  
  &.completed {
    background: #e8f5e8;
    color: #52c41a;
  }
  
  &.cancelled {
    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: #007aff;
        }
      }
    }
  }
}

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