<template>
  <el-dialog v-model="visible" title="传染病报告卡" width="800px" @open="loadReportData" destroy-on-close>
    <el-form :model="form" label-width="100px" class="report-form">
      <el-form-item label="现住址">
        <el-input v-model="form.currentAddress" name="currentAddress" placeholder="现住址" />
      </el-form-item>
      <el-form-item label="职业">
        <el-input v-model="form.occupation" name="occupation" placeholder="职业" />
      </el-form-item>
      <!-- 其他报卡字段省略 -->
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交上报</el-button>
    </template>
  </el-dialog>
</template>

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

const visible = ref(false)
const form = reactive({
  patientId: null,
  currentAddress: '',
  occupation: ''
})

/**
 * 打开报卡弹窗
 * @param {Number} patientId 患者ID
 */
const open = (patientId) => {
  form.patientId = patientId
  visible.value = true
}

/**
 * Bug #572 Fix: 弹窗打开时请求后端初始化接口，自动填充档案数据
 */
const loadReportData = async () => {
  if (!form.patientId) return
  try {
    const res = await request.get(`/outpatient/infectious-disease-report/init/${form.patientId}`)
    if (res.code === 200 && res.data) {
      form.currentAddress = res.data.currentAddress || ''
      form.occupation = res.data.occupation || ''
    }
  } catch (error) {
    console.error('加载传染病报告卡数据失败:', error)
  }
}

const handleSubmit = () => {
  // 提交逻辑（略）
  visible.value = false
}

defineExpose({ open })
</script>

<style scoped>
.report-form {
  padding: 20px;
}
</style>
