<template>
  <el-dialog
    title="订单详情"
    :visible.sync="visible"
    width="80%"
    :before-close="handleClose"
    class="order-detail-dialog"
  >
    <div v-loading="loading" class="order-detail-content">
      <!-- 订单状态头部 -->
      <div class="order-header">
        <div class="order-status">
          <el-tag :type="getStatusType(orderDetail.status)" size="large">
            {{ getStatusText(orderDetail.status) }}
          </el-tag>
        </div>
        <div class="order-no">
          <span class="label">订单号：</span>
          <span class="value">{{ orderDetail.orderNo }}</span>
        </div>
      </div>

      <!-- 订单信息主体 -->
      <el-row :gutter="20" class="order-info-section">
        <!-- 左侧：基础信息 -->
        <el-col :span="12">
          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-user"></i>
              <span>用户信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">用户姓名：</span>
                <span class="value">{{ orderDetail.userName || '未知用户' }}</span>
              </div>
              <div class="info-row">
                <span class="label">用户账号：</span>
                <span class="value">{{ orderDetail.userAccount || '-' }}</span>
              </div>
              <div class="info-row">
                <span class="label">手机号码：</span>
                <span class="value">{{ orderDetail.userPhone || '-' }}</span>
              </div>
            </div>
          </el-card>

          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-cpu"></i>
              <span>设备信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">设备名称：</span>
                <span class="value">{{ orderDetail.deviceName || orderDetail.deviceId || '未知设备' }}</span>
              </div>
              <div class="info-row">
                <span class="label">设备编号：</span>
                <span class="value">{{ orderDetail.deviceCode || '-' }}</span>
              </div>
              <div class="info-row">
                <span class="label">场地名称：</span>
                <span class="value">{{ orderDetail.siteName || orderDetail.siteId || '未知场地' }}</span>
              </div>
              <div class="info-row">
                <span class="label">无人机型号：</span>
                <span class="value">{{ orderDetail.aircraftName || orderDetail.aircraftId || '未知无人机' }}</span>
              </div>
            </div>
          </el-card>

          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-box"></i>
              <span>货道信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">租赁货道：</span>
                <span class="value">{{ orderDetail.rentCellName || orderDetail.rentCellId || '-' }}</span>
              </div>
              <div class="info-row">
                <span class="label">货道编号：</span>
                <span class="value">{{ orderDetail.rentCellCode || '-' }}</span>
              </div>
              <div class="info-row">
                <span class="label">退租货道：</span>
                <span class="value">{{ orderDetail.returnCellName || orderDetail.returnCellId || '-' }}</span>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 右侧：时间、金额、状态信息 -->
        <el-col :span="12">
          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-time"></i>
              <span>时间信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">创建时间：</span>
                <span class="value">{{ formatTime(orderDetail.createTime) }}</span>
              </div>
              <div class="info-row">
                <span class="label">开始时间：</span>
                <span class="value">{{ formatTime(orderDetail.startTime) }}</span>
              </div>
              <div class="info-row">
                <span class="label">结束时间：</span>
                <span class="value">{{ formatTime(orderDetail.endTime) }}</span>
              </div>
              <div class="info-row">
                <span class="label">更新时间：</span>
                <span class="value">{{ formatTime(orderDetail.updateTime) }}</span>
              </div>
            </div>
          </el-card>

          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-money"></i>
              <span>金额信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">实付金额：</span>
                <span class="value price">¥{{ orderDetail.actualAmount || 0 }}</span>
              </div>
              <div class="info-row">
                <span class="label">优惠金额：</span>
                <span class="value discount">¥{{ orderDetail.discountAmount || 0 }}</span>
              </div>
              <div class="info-row">
                <span class="label">分成比例：</span>
                <span class="value ratio">{{ (orderDetail.shareRatio || 0) * 100 }}%</span>
              </div>
              <div class="info-row">
                <span class="label">分成金额：</span>
                <span class="value share">¥{{ orderDetail.shareAmount || 0 }}</span>
              </div>
              <div class="info-row">
                <span class="label">平台收入：</span>
                <span class="value final">¥{{ orderDetail.finalAmount || 0 }}</span>
              </div>
            </div>
          </el-card>

          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-user-solid"></i>
              <span>代理商信息</span>
            </div>
            <div class="info-content">
              <div class="info-row">
                <span class="label">代理商：</span>
                <span class="value">{{ orderDetail.agentName || orderDetail.agentId || '未知代理商' }}</span>
              </div>
              <div class="info-row">
                <span class="label">套餐名称：</span>
                <span class="value">{{ orderDetail.feeRuleName || '未选择套餐' }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 扩展信息 -->
      <el-row :gutter="20" class="order-info-section">
        <el-col :span="24">
          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-document"></i>
              <span>扩展信息</span>
            </div>
            <div class="info-content">
              <el-row :gutter="20">
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">支付宝授权号：</span>
                    <span class="value">{{ orderDetail.aliAuthNo || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">RFID1：</span>
                    <span class="value">{{ orderDetail.rfid1 || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">RFID2：</span>
                    <span class="value">{{ orderDetail.rfid2 || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">退租设备：</span>
                    <span class="value">{{ orderDetail.returnDeviceName || orderDetail.returnDeviceId || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">退租场地：</span>
                    <span class="value">{{ orderDetail.returnSiteName || orderDetail.returnSiteId || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="info-row">
                    <span class="label">备注：</span>
                    <span class="value">{{ orderDetail.remark || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 操作记录 -->
      <el-row :gutter="20" class="order-info-section" v-if="orderDetail.status === 3">
        <el-col :span="24">
          <el-card class="info-card" shadow="never">
            <div slot="header" class="card-header">
              <i class="el-icon-s-operation"></i>
              <span>订单操作</span>
            </div>
            <div class="info-content">
              <div class="operation-timeline">
                <el-timeline>
                  <el-timeline-item
                    v-for="(activity, index) in orderActivities"
                    :key="index"
                    :timestamp="activity.time"
                    :type="activity.type"
                  >
                    {{ activity.content }}
                  </el-timeline-item>
                </el-timeline>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关闭</el-button>
      <el-button type="primary" @click="handlePrint" v-if="orderDetail.status === 3">
        <i class="el-icon-printer"></i>
        打印订单
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrderDetail } from '@/api/extend/uavOrder'

export default {
  name: 'OrderDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    orderId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      orderDetail: {}
    }
  },
  watch: {
    visible(newVal) {
      if (newVal && this.orderId) {
        this.loadOrderDetail()
      }
    }
  },
  methods: {
    // 加载订单详情
    async loadOrderDetail() {
      if (!this.orderId) return
      
      this.loading = true
      try {
        const response = await getOrderDetail(this.orderId)
        
        if (response && response.code === 200) {
          this.orderDetail = response.data || {}
        } else {
          this.$message.error((response && response.msg) || '获取订单详情失败')
        }
      } catch (error) {
        console.error('获取订单详情失败:', error)
        this.$message.error('获取订单详情失败')
      } finally {
        this.loading = false
      }
    },

    // 格式化时间
    formatTime(time) {
      if (!time) return '-'
      const date = new Date(time)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      })
    },

    // 获取状态类型
    getStatusType(status) {
      const statusMap = {
        1: 'warning',   // 待付款
        2: 'primary',   // 已租接
        3: 'success',   // 已完成
        4: 'danger',    // 已取消
        5: 'info',      // 已退款
        6: 'danger',    // 超时未付款
        7: 'warning',   // 归还中
        10: 'danger'    // 订单异常
      }
      return statusMap[status] || 'info'
    },

    // 获取状态文本
    getStatusText(status) {
      const statusMap = {
        1: '待付款',
        2: '已租接',
        3: '已完成',
        4: '已取消',
        5: '已退款',
        6: '超时未付款',
        7: '归还中',
        10: '订单异常'
      }
      return statusMap[status] || '未知状态'
    },

    // 关闭弹窗
    handleClose() {
      this.$emit('update:visible', false)
      this.orderDetail = {}
    },

    // 打印订单
    handlePrint() {
      this.$message.success('打印功能开发中...')
    }
  }
}
</script>

<style lang="scss" scoped>
.order-detail-dialog {
  .order-detail-content {
    max-height: 70vh;
    overflow-y: auto;
  }

  .order-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;

    .order-status {
      .el-tag {
        font-size: 16px;
        padding: 8px 16px;
      }
    }

    .order-no {
      .label {
        font-weight: bold;
        color: #606266;
      }
      .value {
        font-size: 16px;
        font-weight: bold;
        color: #303133;
        margin-left: 8px;
      }
    }
  }

  .order-info-section {
    margin-bottom: 20px;
  }

  .info-card {
    margin-bottom: 20px;
    border: 1px solid #e4e7ed;
    border-radius: 8px;

    .card-header {
      display: flex;
      align-items: center;
      font-weight: bold;
      color: #303133;

      i {
        margin-right: 8px;
        font-size: 16px;
      }
    }

    .info-content {
      .info-row {
        display: flex;
        margin-bottom: 12px;
        align-items: center;

        &:last-child {
          margin-bottom: 0;
        }

        .label {
          min-width: 100px;
          color: #606266;
          font-weight: 500;
        }

        .value {
          color: #303133;
          flex: 1;

          &.price {
            color: #f56c6c;
            font-weight: bold;
            font-size: 16px;
          }

          &.discount {
            color: #67c23a;
            font-weight: bold;
          }

          &.ratio {
            color: #409eff;
            font-weight: bold;
          }

          &.share {
            color: #e6a23c;
            font-weight: bold;
          }

          &.final {
            color: #f56c6c;
            font-weight: bold;
            font-size: 16px;
          }
        }
      }
    }
  }

  .operation-timeline {
    padding: 10px 0;
  }

  .dialog-footer {
    text-align: right;
    padding-top: 20px;
    border-top: 1px solid #e4e7ed;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .order-detail-dialog {
    .el-dialog {
      width: 95% !important;
    }

    .order-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 10px;
    }

    .info-card {
      .info-content {
        .info-row {
          flex-direction: column;
          align-items: flex-start;

          .label {
            min-width: auto;
            margin-bottom: 4px;
          }
        }
      }
    }
  }
}
</style> 