<template>
  <div class="write-record">
    <el-table :data="orderList" style="width: 100%" v-loading="loading">
      <!-- 表头列略 -->
    </el-table>
    <el-pagination
      v-if="total > pageSize"
      background
      layout="prev, pager, next, jumper"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @current-change="handlePageChange" />
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { getPendingOrders } from '@/api/outpatient'

const props = defineProps({
  patientId: { type: [String, Number], required: true }
})

const orderList = ref([])
const loading = ref(false)
const pageNum = ref(1)
const pageSize = ref(20)
const total = ref(0)

const fetchOrders = async () => {
  loading.value = true
  try {
    const res = await getPendingOrders(props.patientId, pageNum.value, pageSize.value)
    orderList.value = res.data
    total.value = res.total || orderList.value.length // 若后端未返回 total，使用列表长度
  } finally {
    loading.value = false
  }
}

const handlePageChange = (newPage) => {
  pageNum.value = newPage
  fetchOrders()
}

onMounted(fetchOrders)
watch(() => props.patientId, fetchOrders)
</script>
