<template>
  <div class="pending-emr-container">
    <div class="header">
      <h2>待写病历</h2>
      <div class="actions">
        <el-button type="primary" @click="refreshList">刷新</el-button>
      </div>
    </div>
    
    <el-card class="search-card">
      <el-form :model="queryParams" inline>
        <el-form-item label="患者姓名">
          <el-input 
            v-model="queryParams.patientName" 
            placeholder="请输入患者姓名" 
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table 
      :data="emrList" 
      v-loading="loading" 
      style="width: 100%"
      highlight-current-row
      @row-click="handleRowClick"
    >
      <el-table-column prop="patientName" label="患者姓名" width="120" />
      <el-table-column prop="busNo" label="病历号" width="150" />
      <el-table-column prop="gender" label="性别" width="80">
        <template #default="{ row }">
          {{ getGenderText(row.gender) }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="registerTime" label="挂号时间" width="180">
        <template #default="{ row }">
          {{ parseTime(row.registerTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click.stop="handleWriteEmr(row)">写病历</el-button>
          <el-divider direction="vertical" />
          <el-button link type="primary" @click.stop="handleViewPatient(row)">查看患者</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:size="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="PendingEmr">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listPendingEmr, getPendingEmrCount } from '@/views/doctorstation/components/api.js'
import { parseTime } from '@/utils/index.js'
import Pagination from '@/components/Pagination'

// 响应式数据
const loading = ref(true)
const total = ref(0)
const emrList = ref([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  patientName: undefined
})

// 获取待写病历列表
const getList = async () => {
  loading.value = true
  try {
    const response = await listPendingEmr(queryParams)
    // 根据后端返回的数据结构调整
    if (response.code === 200) {
      emrList.value = response.data || []
      total.value = Array.isArray(response.data) ? response.data.length : 0
    } else {
      ElMessage.error(response.msg || '获取待写病历列表失败')
      emrList.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('获取待写病历列表失败:', error)
    ElMessage.error('获取待写病历列表失败')
    emrList.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.patientName = undefined
  queryParams.pageNum = 1
  getList()
}

// 刷新列表
const refreshList = () => {
  getList()
}

// 行点击事件
const handleRowClick = (row) => {
  // 可以在这里处理行点击事件
  console.log('点击行:', row)
}

// 写病历
const handleWriteEmr = (row) => {
  console.log('写病历:', row)
  // 触发写病历事件，通知父组件切换到病历页面
  emit('writeEmr', row)
}

// 查看患者
const handleViewPatient = (row) => {
  console.log('查看患者:', row)
  // 触发查看患者事件
  emit('viewPatient', row)
}

// 获取性别文本
const getGenderText = (genderValue) => {
  const genderMap = {
    1: '男',
    2: '女'
  }
  return genderMap[genderValue] || '未知'
}

// 初始化
onMounted(() => {
  getList()
})

// 定义emit
const emit = defineEmits(['writeEmr', 'viewPatient'])
</script>

<style lang="scss" scoped>
.pending-emr-container {
  padding: 20px;
  
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      font-size: 18px;
      font-weight: bold;
    }
  }
  
  .search-card {
    margin-bottom: 20px;
  }
}
</style>