<template>
  <div class="order-list-container">
    <el-table :data="orderList" border v-loading="loading">
      <el-table-column prop="orderNo" label="医嘱号" width="120" />
      <el-table-column prop="itemName" label="药品名称" />
      <el-table-column prop="statusName" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusTagType(row.status)">{{ row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="开立时间" width="180" />
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getOrderStatusName } from '@/utils/orderStatusMapper'
import request from '@/utils/request'

const props = defineProps({
  node: { type: String, default: 'nurse' } // nurse | pharmacy
})

const orderList = ref([])
const loading = ref(false)

const fetchOrders = async () => {
  loading.value = true
  try {
    const res = await request.get('/api/inpatient/orders', { params: { node: props.node } })
    // 修复 Bug #569：前端统一使用映射工具转换状态名称，杜绝硬编码
    orderList.value = res.data.map(item => ({
      ...item,
      statusName: getOrderStatusName(item.status, props.node)
    }))
  } finally {
    loading.value = false
  }
}

const getStatusTagType = (code) => {
  const map = { 0: 'info', 1: 'warning', 2: 'primary', 3: 'success', 4: 'success', 5: 'warning', 6: 'success', 7: 'success' }
  return map[code] || 'info'
}

onMounted(fetchOrders)
</script>
