<template>
  <div class="smart-queue-container">
    <el-card>
      <template #header>
        <div class="header-actions">
          <span class="title">智能分诊排队管理 - {{ deptName }}</span>
          <div class="filters">
            <!-- 修复 Bug #544：新增历史队列查询入口，默认绑定当天 -->
            <el-date-picker
              v-model="queryDate"
              type="date"
              placeholder="选择查询日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              data-testid="queue-date-picker"
              @change="fetchQueueData"
            />
            <el-button type="primary" @click="fetchQueueData" data-testid="search-btn">查询</el-button>
          </div>
        </div>
      </template>

      <el-table :data="queueList" border style="width: 100%" data-testid="queue-table" v-loading="loading">
        <el-table-column prop="patientName" label="患者姓名" width="150" />
        <el-table-column prop="status" label="排队状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusLabel(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="queueTime" label="排队时间" />
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button link type="primary" size="small">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getQueueList } from '@/api/triage'

const deptName = ref('呼吸内科')
const queryDate = ref(new Date().toISOString().split('T')[0]) // 默认当天
const queueList = ref([])
const loading = ref(false)

const fetchQueueData = async () => {
  loading.value = true
  try {
    // 假设 deptId 由路由参数或全局状态管理，此处以固定值演示
    const res = await getQueueList({ deptId: 101, queryDate: queryDate.value })
    queueList.value = res.data || []
  } catch (e) {
    console.error('获取队列数据失败', e)
  } finally {
    loading.value = false
  }
}

const getStatusLabel = (status) => {
  const map = { 0: '待诊', 1: '就诊中', 2: '过号', 3: '完诊' }
  return map[status] || '未知'
}

const getStatusType = (status) => {
  const map = { 0: 'info', 1: 'warning', 2: 'danger', 3: 'success' }
  return map[status] || 'info'
}

onMounted(fetchQueueData)
</script>

<style scoped>
.smart-queue-container { padding: 20px; }
.header-actions { display: flex; justify-content: space-between; align-items: center; }
.title { font-size: 18px; font-weight: bold; }
.filters { display: flex; gap: 12px; }
</style>
