<template>
  <div class="shipping-order-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <!-- 用户信息 -->
      <el-table-column label="用户信息" min-width="160" fixed="left">
        <template #default="{ row }">
          <div class="user-info">
            <div class="nickname">{{ row.userNickname || '-' }}</div>
            <div class="uid">UID: {{ row.userId }}</div>
            <div class="mobile" v-if="row.userMobile">{{ row.userMobile }}</div>
          </div>
        </template>
      </el-table-column>

      <!-- 发货单号 -->
      <el-table-column prop="sendNum" label="发货单号" min-width="180">
        <template #default="{ row }">
          <span class="order-num">{{ row.sendNum }}</span>
        </template>
      </el-table-column>

      <!-- 发货数量 -->
      <el-table-column prop="count" label="发货数量" width="100" align="center">
        <template #default="{ row }">
          <span class="count">{{ row.count }}</span>
        </template>
      </el-table-column>

      <!-- 总价格 -->
      <el-table-column label="总价格" width="120" align="right">
        <template #default="{ row }">
          <span class="money">¥{{ row.totalPrice?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>

      <!-- 运费 -->
      <el-table-column label="运费" width="100" align="right">
        <template #default="{ row }">
          <span class="freight">¥{{ row.freight?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>

      <!-- 收货信息 -->
      <el-table-column label="收货信息" min-width="200">
        <template #default="{ row }">
          <div class="receiver-info">
            <div class="receiver-name">{{ row.name || '-' }}</div>
            <div class="receiver-mobile" v-if="row.receiverMobile">{{ row.receiverMobile }}</div>
            <div class="receiver-address" v-if="row.address">{{ row.address }}</div>
          </div>
        </template>
      </el-table-column>

      <!-- 订单状态 -->
      <el-table-column label="状态" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)" size="small">
            {{ row.statusName }}
          </el-tag>
        </template>
      </el-table-column>

      <!-- 时间 -->
      <el-table-column label="时间" min-width="160">
        <template #default="{ row }">
          <div class="time-info">
            <div>申请: {{ row.createdAt }}</div>
            <div v-if="row.payTime">支付: {{ row.payTime }}</div>
            <div v-if="row.sendTime">发货: {{ row.sendTime }}</div>
            <div v-if="row.receiveTime">签收: {{ row.receiveTime }}</div>
          </div>
        </template>
      </el-table-column>

      <!-- 备注 -->
      <el-table-column prop="message" label="备注" min-width="120">
        <template #default="{ row }">
          <span class="message">{{ row.message || '-' }}</span>
        </template>
      </el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作" width="160" fixed="right" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleViewDetail(row)">
            查看详情
          </el-button>
          <el-button
            v-if="canCancel(row.status)"
            type="danger"
            link
            @click="handleCancelShip(row)"
          >
            取消发货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="currentPageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      class="pagination"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { ShippingStatus, type ShippingOrderResponse } from '@/api/business/order'

interface Props {
  data: ShippingOrderResponse[]
  loading: boolean
  total: number
  page: number
  pageSize: number
}

const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'page-change', page: number): void
  (e: 'size-change', size: number): void
  (e: 'view-detail', row: ShippingOrderResponse): void
  (e: 'cancel-ship', row: ShippingOrderResponse): void
}>()

const currentPage = ref(props.page)
const currentPageSize = ref(props.pageSize)

watch(() => props.page, (val) => {
  currentPage.value = val
})

watch(() => props.pageSize, (val) => {
  currentPageSize.value = val
})

// 获取状态标签类型
const getStatusType = (status: number): 'success' | 'warning' | 'info' | 'danger' => {
  switch (status) {
    case ShippingStatus.Pending:
      return 'warning'
    case ShippingStatus.Shipped:
      return 'info'
    case ShippingStatus.Received:
      return 'success'
    case ShippingStatus.Cancelled:
      return 'danger'
    default:
      return 'info'
  }
}

// 判断是否可以取消发货
const canCancel = (status: number): boolean => {
  return status === ShippingStatus.Pending
}

const handlePageChange = (page: number) => {
  emit('page-change', page)
}

const handleSizeChange = (size: number) => {
  emit('size-change', size)
}

const handleViewDetail = (row: ShippingOrderResponse) => {
  emit('view-detail', row)
}

const handleCancelShip = (row: ShippingOrderResponse) => {
  emit('cancel-ship', row)
}
</script>

<style scoped>
.shipping-order-table {
  width: 100%;
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nickname {
  font-weight: 500;
}

.uid {
  font-size: 12px;
  color: #909399;
}

.mobile {
  font-size: 12px;
  color: #606266;
}

.order-num {
  font-family: monospace;
  font-size: 13px;
}

.count {
  font-weight: 500;
  color: #409eff;
}

.money {
  color: #e6a23c;
  font-weight: 500;
}

.freight {
  color: #67c23a;
}

.receiver-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
}

.receiver-name {
  font-weight: 500;
}

.receiver-mobile {
  color: #606266;
}

.receiver-address {
  color: #909399;
  font-size: 12px;
  line-height: 1.4;
}

.time-info {
  font-size: 12px;
  line-height: 1.6;
  color: #606266;
}

.message {
  font-size: 12px;
  color: #909399;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
