<template>
  <div class="panel-section order-mgmt">
    <div class="sec-header">
      <span class="tit">订单</span>
      <!-- Right Aligned Tabs -->
      <div class="header-right-tabs">
        <span class="h-tab-item" :class="{ active: currentTab === 'PendingDispatch' }" @click="currentTab = 'PendingDispatch'">
          <span class="txt">待派单</span>
          <span class="num danger">{{ stats.pendingDispatch }}</span>
        </span>
        <span class="h-tab-item" :class="{ active: currentTab === 'PendingAccept' }" @click="currentTab = 'PendingAccept'">
          <span class="txt">待接单</span>
          <span class="num warning">{{ stats.pendingAccept }}</span>
        </span>
        <span class="h-tab-item" :class="{ active: currentTab === 'Processing' }" @click="currentTab = 'Processing'">
          <span class="txt">进行中</span>
          <span class="num primary">{{ stats.processing }}</span>
        </span>
      </div>
    </div>

    <!-- Order List -->
    <div class="list-wrapper">
      <el-scrollbar>
        <div v-if="orders.length === 0" class="empty-state">暂无数据</div>
        <div v-else v-for="order in orders" :key="order.id" class="list-card order-card" @click="$emit('focus', order.lng, order.lat)">
          <div class="card-left">
            <div class="type-tag" :class="order.typeCode">
              {{ getShortType(order.typeCode) }}
            </div>
          </div>
          <div class="card-main">
            <template v-if="order.typeCode === 'transport'">
              <div class="row-addr" :title="order.pickAddr"><span class="tag pick">取</span> {{ order.pickAddr }}</div>
              <div class="row-addr" :title="order.dropAddr"><span class="tag drop">送</span> {{ order.dropAddr }}</div>
              <div class="row-time">
                <el-icon><Clock /></el-icon> {{ order.time }}
                <span class="days-tag" v-if="order.daysLater">{{ order.daysLater }}</span>
              </div>
            </template>
            <template v-else>
              <div class="row-addr" :title="order.address"><span class="tag home">址</span> {{ order.address }}</div>
              <div class="row-time" style="margin-top: 4px">
                <el-icon><Clock /></el-icon> {{ order.time }}
                <span class="days-tag" v-if="order.daysLater">{{ order.daysLater }}</span>
              </div>
            </template>
          </div>

          <!-- Right: Status & Actions -->
          <div class="card-right">
            <el-tag size="small" :type="getOrderStatusType(order.status)" effect="plain">{{ getOrderStatusText(order.status) }}</el-tag>
            <div class="actions">
              <el-button v-if="order.status === 'pending_dispatch'" type="primary" size="small" @click.stop="$emit('dispatch', order)"
                >派单</el-button
              >
              <el-button
                v-else-if="['pending_accept', 'processing'].includes(order.status)"
                type="primary"
                size="small"
                plain
                @click.stop="$emit('dispatch', order)"
                >重新派单</el-button
              >
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { Clock } from '@element-plus/icons-vue';

const props = defineProps({
  modelValue: { type: String, default: 'PendingDispatch' },
  orders: { type: Array, default: () => [] },
  stats: { type: Object, default: () => ({}) }
});

const emit = defineEmits(['update:modelValue', 'focus', 'dispatch']);

const currentTab = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
});

const getShortType = (code) => {
  const map = { 'transport': '接送', 'feeding': '喂遛', 'washing': '洗护' };
  return map[code] || '订单';
};
const getOrderStatusText = (status) => {
  const map = { 'pending_dispatch': '待派单', 'pending_accept': '待接单', 'processing': '进行中', 'completed': '已完成' };
  return map[status];
};
const getOrderStatusType = (status) => {
  const map = { 'pending_dispatch': 'danger', 'pending_accept': 'warning', 'processing': 'primary', 'completed': 'success' };
  return map[status];
};
</script>

<style scoped>
.order-mgmt {
  border-bottom: 8px solid #f5f7fa;
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 50%;
}
.sec-header {
  height: 48px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #f0f0f0;
}
.sec-header .tit {
  font-weight: bold;
  font-size: 15px;
  color: #1f2f3d;
}

.header-right-tabs {
  display: flex;
  gap: 4px;
}
.h-tab-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  padding: 6px 12px;
  gap: 6px;
  border-radius: 4px;
  transition: all 0.2s;
  color: #606266;
}
.h-tab-item:hover {
  background: #f5f7fa;
}
.h-tab-item.active {
  background: #ecf5ff;
}
.h-tab-item.active .txt {
  color: #409eff;
  font-weight: bold;
}
.h-tab-item .txt {
  font-size: 14px;
}
.h-tab-item .num {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 0;
}
.h-tab-item .num.danger {
  color: #f56c6c;
}
.h-tab-item .num.warning {
  color: #e6a23c;
}
.h-tab-item .num.primary {
  color: #409eff;
}

.list-wrapper {
  flex: 1;
  overflow: hidden;
  padding: 8px 12px;
}
.empty-state {
  text-align: center;
  color: #909399;
  padding: 20px;
  font-size: 13px;
}

.list-card {
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 10px;
  display: flex;
  align-items: stretch;
  gap: 12px;
  transition: all 0.2s;
  cursor: pointer;
}
.list-card:hover {
  border-color: #c6e2ff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.card-left {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}
.order-card .type-tag {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}
.type-tag.transport {
  background: #e6a23c;
}
.type-tag.feeding {
  background: #67c23a;
}
.type-tag.washing {
  background: #409eff;
}

.card-main {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
}
.row-addr {
  font-size: 13px;
  color: #303133;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}
.row-addr .tag {
  font-size: 11px;
  color: #fff;
  padding: 1px 4px;
  border-radius: 4px;
  flex-shrink: 0;
  transform: scale(0.9);
}
.tag.pick {
  background: #409eff;
}
.tag.drop {
  background: #e6a23c;
}
.tag.home {
  background: #67c23a;
}

.row-time {
  font-size: 12px;
  color: #909399;
  display: flex;
  align-items: center;
  gap: 4px;
}
.days-tag {
  color: #f56c6c;
  background: #fef0f0;
  padding: 0 4px;
  border-radius: 4px;
  font-size: 11px;
  border: 1px solid #fde2e2;
  transform: scale(0.95);
}

.card-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  gap: 8px;
  margin-left: 8px;
  flex-shrink: 0;
}
.card-right .actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
</style>
