<script setup lang="ts">
import { reactive, watch } from 'vue'
import dayjs from 'dayjs'
import FieldItem from './FieldItem.vue'
import SignatureItem from './SignatureItem.vue'

export interface CheckInRegistrationForm {
  checkInNo?: string
  formDate?: string
  elderName?: string
  gender?: string
  age?: string | number
  photo?: string
  nationality?: string
  birthDate?: string
  nativePlace?: string
  householdPlace?: string
  nation?: string
  nationOther?: string
  politicalStatus?: string
  maritalStatus?: string
  idCardType?: string
  idCardNo?: string
  educationLevel?: string
  residentialAddress?: string
  guarantorName?: string
  guarantorRelation?: string
  guarantorPhone?: string
  guarantorIdCardType?: string
  guarantorIdCardNo?: string
  guarantorAddress?: string
  guarantorEmail?: string
  secondContactName?: string
  secondContactPhone?: string
  medicalPaymentMethod?: string
  medicalPaymentOther?: string
  incomeSource?: string[]
  incomeSourceOther?: string
  preResidenceHomeType?: string[]
  preResidenceHomeOther?: string
  checkInReason?: string
  pastHistoryDiseaseNone?: boolean | null
  pastHistoryDiseaseName1?: string
  pastHistoryDiseaseTime1?: string
  pastHistoryDiseaseName2?: string
  pastHistoryDiseaseTime2?: string
  pastHistorySurgery?: boolean | null
  pastHistorySurgeryName?: string
  pastHistorySurgeryTime?: string
  pastHistoryTrauma?: boolean | null
  pastHistoryTraumaPart1?: string
  pastHistoryTraumaTime1?: string
  pastHistoryTraumaPart2?: string
  pastHistoryTraumaTime2?: string
  currentDiseaseName1?: string
  currentDiseaseTime1?: string
  currentDiseaseStatus1?: string
  currentDiseaseName2?: string
  currentDiseaseTime2?: string
  currentDiseaseStatus2?: string
  regularVisitNone?: boolean | null
  regularVisitReason?: string
  regularVisitFreq?: string
  hospitalizationNone?: boolean | null
  hospitalizationCount?: string
  emergencyNone?: boolean | null
  emergencyCount?: string
  discomfortSymptomsNone?: boolean | null
  discomfortSymptomsList?: string[]
  discomfortSymptomsOther?: string
  allergyDrugNone?: boolean | null
  allergyDrugDetail?: string
  allergyFoodNone?: boolean | null
  allergyFoodDetail?: string
  allergyEnvNone?: boolean | null
  allergyEnvDetail?: string
  fallNone?: boolean | null
  fallDetail?: string
  memoryDeclineNone?: boolean | null
  memoryDeclineDetail?: string
  weightLossNone?: boolean | null
  weightLossWeight?: string
  urinaryIncontinenceNone?: boolean | null
  urinaryIncontinenceCount?: string
  sleepDisorderNone?: boolean | null
  sleepDisorderTypes?: string[]
  sleepDisorderOther?: string
  painNone?: boolean | null
  painPart?: string
  visionAbnormalNone?: boolean | null
  visionAbnormalTypes?: string[]
  visionAbnormalDegree?: string
  hearingLossNone?: boolean | null
  hearingLossDetail?: string
  mentalStatusNone?: boolean | null
  mentalStatusList?: string[]
  mentalStatusOther?: string
  languageExpression?: string[]
  languageExpressionOther?: string
  otherNotes?: string
  partyBSign?: string
  partyCSign?: string
  signDate?: string
}

const props = withDefaults(
  defineProps<{
    isTextMode?: boolean
    modelValue?: CheckInRegistrationForm
    elderId?: string | number
  }>(),
  {
    isTextMode: false,
    modelValue: () => ({}),
    elderId: ''
  }
)

const emit = defineEmits<{
  (e: 'update:modelValue', val: CheckInRegistrationForm): void
}>()

const createDefaultForm = (): CheckInRegistrationForm => ({
  checkInNo: '',
  formDate: dayjs().format('YYYY-MM-DD'),
  elderName: '',
  gender: '',
  age: '',
  photo: '',
  nationality: '中国',
  birthDate: '',
  nativePlace: '',
  householdPlace: '',
  nation: '汉族',
  nationOther: '',
  politicalStatus: '',
  maritalStatus: '',
  idCardType: '身份证',
  idCardNo: '',
  educationLevel: '',
  residentialAddress: '',
  guarantorName: '',
  guarantorRelation: '',
  guarantorPhone: '',
  guarantorIdCardType: '身份证',
  guarantorIdCardNo: '',
  guarantorAddress: '',
  guarantorEmail: '',
  secondContactName: '',
  secondContactPhone: '',
  medicalPaymentMethod: '',
  medicalPaymentOther: '',
  incomeSource: [],
  incomeSourceOther: '',
  preResidenceHomeType: [],
  preResidenceHomeOther: '',
  checkInReason: '',
  pastHistoryDiseaseNone: null,
  pastHistoryDiseaseName1: '',
  pastHistoryDiseaseTime1: '',
  pastHistoryDiseaseName2: '',
  pastHistoryDiseaseTime2: '',
  pastHistorySurgery: null,
  pastHistorySurgeryName: '',
  pastHistorySurgeryTime: '',
  pastHistoryTrauma: null,
  pastHistoryTraumaPart1: '',
  pastHistoryTraumaTime1: '',
  pastHistoryTraumaPart2: '',
  pastHistoryTraumaTime2: '',
  currentDiseaseName1: '',
  currentDiseaseTime1: '',
  currentDiseaseStatus1: '',
  currentDiseaseName2: '',
  currentDiseaseTime2: '',
  currentDiseaseStatus2: '',
  regularVisitNone: null,
  regularVisitReason: '',
  regularVisitFreq: '',
  hospitalizationNone: null,
  hospitalizationCount: '',
  emergencyNone: null,
  emergencyCount: '',
  discomfortSymptomsNone: null,
  discomfortSymptomsList: [],
  discomfortSymptomsOther: '',
  allergyDrugNone: null,
  allergyDrugDetail: '',
  allergyFoodNone: null,
  allergyFoodDetail: '',
  allergyEnvNone: null,
  allergyEnvDetail: '',
  fallNone: null,
  fallDetail: '',
  memoryDeclineNone: null,
  memoryDeclineDetail: '',
  weightLossNone: null,
  weightLossWeight: '',
  urinaryIncontinenceNone: null,
  urinaryIncontinenceCount: '',
  sleepDisorderNone: null,
  sleepDisorderTypes: [],
  sleepDisorderOther: '',
  painNone: null,
  painPart: '',
  visionAbnormalNone: null,
  visionAbnormalTypes: [],
  visionAbnormalDegree: '',
  hearingLossNone: null,
  hearingLossDetail: '',
  mentalStatusNone: null,
  mentalStatusList: [],
  mentalStatusOther: '',
  languageExpression: [],
  languageExpressionOther: '',
  otherNotes: '',
  partyBSign: '',
  partyCSign: '',
  signDate: dayjs().format('YYYY-MM-DD')
})

const form = reactive<CheckInRegistrationForm>(createDefaultForm())

watch(
  () => props.modelValue,
  (val) => {
    if (val) Object.assign(form, createDefaultForm(), val)
  },
  { immediate: true, deep: true }
)

const emitForm = () => emit('update:modelValue', JSON.parse(JSON.stringify(form)))

const updateForm = (key: keyof CheckInRegistrationForm, val: any) => {
  ;(form as any)[key] = val
  emitForm()
}

// 从长者选择返回值填充数据（基于实际API返回值映射）
const fillFromElderData = (elder: Record<string, any>) => {
  // 基本信息
  form.elderName = elder.elderName || ''
  form.gender = elder.elderSex === 1 ? '男' : elder.elderSex === 2 ? '女' : ''
  form.age = elder.elderAge || ''
  form.photo = elder.elderImage || elder.elderCover || ''

  // 出生日期（处理时间戳或字符串）
  if (elder.birthday) {
    form.birthDate = typeof elder.birthday === 'number'
      ? dayjs(elder.birthday).format('YYYY-MM-DD')
      : elder.birthday
  }

  // 籍贯/户籍地址
  form.nativePlace = elder.registeredResidenceAddress || elder.region || ''
  form.householdPlace = elder.censusRegister || ''

  // 民族
  const nationMap: Record<string, string> = { '1': '汉族', '2': '蒙古族', '3': '回族', '4': '藏族' }
  form.nation = elder.nationId ? (nationMap[String(elder.nationId)] || '汉族') : '汉族'

  // 政治面貌
  const polMap: Record<string, string> = { '1': '中共党员', '2': '共青团员', '3': '民主党派', '4': '群众' }
  form.politicalStatus = elder.political ? polMap[String(elder.political)] || '' : ''

  // 婚姻状况
  const marMap: Record<number, string> = { 1: '未婚', 2: '已婚', 3: '丧偶', 4: '离异' }
  form.maritalStatus = elder.maritalStatus ? marMap[Number(elder.maritalStatus)] || '' : ''

  // 证件类型和号码
  const paperMap: Record<number, string> = { 1: '身份证', 2: '护照', 3: '军官证', 4: '其他' }
  form.idCardType = elder.papersType ? paperMap[Number(elder.papersType)] || '身份证' : '身份证'
  form.idCardNo = elder.idCard || ''

  // 文化程度
  const eduMap: Record<number, string> = { 1: '文盲半文盲', 2: '小学', 3: '初中', 4: '高中/技校/中专', 5: '大学专科及以上', 6: '不详', 7: '大学专科及以上' }
  form.educationLevel = elder.degreeEducation ? eduMap[Number(elder.degreeEducation)] || '' : ''

  // 居住地址
  form.residentialAddress = elder.address || ''

  // 医疗费用支付方式
  const payMap: Record<number, string> = { 1: '城镇职工基本医疗保险', 2: '城镇居民基本医疗保险', 3: '全公费', 4: '其它' }
  form.medicalPaymentMethod = elder.paymentMethod ? payMap[Number(elder.paymentMethod)] || '' : ''

  // 经济来源
  if (elder.economySource) {
    const srcMap: Record<string, string> = { '1': '退休金/养老金', '2': '子女补贴', '3': '亲友资助', '4': '其他补贴' }
    form.incomeSource = elder.economySource.split(',').filter(Boolean).map((k: string) => srcMap[k] || k).filter(Boolean)
  }

  // 过敏史
  if (elder.allergicDrug || elder.drugAllergy) {
    form.allergyDrugNone = false
    form.allergyDrugDetail = elder.allergicDrug || elder.drugAllergy || ''
  }

  // 入住编号
  form.checkInNo = elder.fileNumber || elder.contractNumber || ''

  emitForm()
}

// 修复：正确暴露方法名
defineExpose({ fillFromElderData, form })

// 打印模式 checkbox 渲染
const printCheck = (checked: boolean) => (checked ? '\u2611' : '\u2610')

// 不适症状选项
const symptomOptions = ['头痛','头晕','心悸','胸闷','胸痛','慢性咳嗽','咳痰','呼吸困难','多饮','多尿','体重下降','乏力','关节疼痛','手脚麻木','视力模糊','眼花','耳鸣','尿急','尿痛','尿频','腹泻','恶心呕吐','食欲减退','乳房胀痛','体位性低血压']

// 精神状况选项
const mentalOptions = ['游走','日夜颠倒','语言攻击行为','肢体攻击行为','对物品的攻击行为','妄想','幻觉','焦虑/恐惧','自伤/自杀','重复行为']

// 辅助方法 - 切换数组项
const toggleArrayItem = (arrKey: keyof CheckInRegistrationForm, val: string) => {
  const arr = (form as any)[arrKey] as string[]
  const idx = arr.indexOf(val)
  if (idx > -1) arr.splice(idx, 1)
  else arr.push(val)
  emitForm()
}

// 辅助方法 - 处理既往史"无"勾选（清空疾病名称）
const onPastHistoryNoneChange = () => {
  form.pastHistoryDiseaseNone = true
  form.pastHistoryDiseaseName1 = ''
  form.pastHistoryDiseaseTime1 = ''
  emitForm()
}

// 辅助方法 - 处理行内input输入（用于疾病名称、时间等字段）
const onInlineInput = (key: keyof CheckInRegistrationForm, e: Event) => {
  const target = e.target as HTMLInputElement
  ;(form as any)[key] = target.value
  emitForm()
}
</script>

<template>
  <div class="check-in-registration">
    <!-- 标题 -->
    <div class="doc-title" style="font-size: 14px;margin-bottom: 12px;width: 100%;text-align: left;margin-top: 16px;">附件 2</div>
     <div class="doc-title" style="width: 100%;text-align: center;">入住登记表</div>
    <div class="doc-meta">
      <span>入住编号：<FieldItem :model-value="form.checkInNo" :is-text-mode="isTextMode" width="140px" @update:model-value="(v:any)=>updateForm('checkInNo',v)" /></span>
      <span>日期：{{ form.signDate || '____年__月__日' }}</span>
    </div>

    <!-- 主表格 - A4宽度适配，严格7列布局 -->
    <table class="reg-table" border="1" cellspacing="0" cellpadding="0">
      <!-- 明确定义7列，按设计图比例分配 -->
      <colgroup>
        <col style="width:9%" />   <!-- 第1列：最左标签(老年人/担保人) - 紧凑 -->
        <col style="width:13%" />  <!-- 第2列：标签/值 -->
        <col style="width:13%" />  <!-- 第3列：值/标签 -->
        <col style="width:13%" />  <!-- 第4列：标签/值 -->
        <col style="width:13%" />  <!-- 第5列：值/标签 -->
        <col style="width:13%" />  <!-- 第6列：标签/值 -->
        <col style="width:18%" />  <!-- 第7列：照片/值 -->
      </colgroup>
      <tbody>

      <!-- ========== 基本信息 + 照片区域（照片只覆盖前8行） ========== -->
      <!-- 第1行：老年人 | 照片(rowspan=8) -->
      <tr>
        <td class="lbl">老年人</td>
        <td><FieldItem :model-value="form.elderName" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('elderName',v)" /></td>
        <td class="lbl">性别</td>
        <td class="radio-cell">
          <template v-if="!isTextMode">
            <label><input type="radio" :checked="form.gender==='男'" @change="updateForm('gender','男')" /> 男</label>
            <label><input type="radio" :checked="form.gender==='女'" @change="updateForm('gender','女')" /> 女</label>
          </template>
          <template v-else>{{ printCheck(form.gender==='男') }} 男 {{ printCheck(form.gender==='女') }} 女</template>
        </td>
        <td class="lbl">年龄</td>
        <td><FieldItem :model-value="form.age" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('age',v)" /></td>
        <td class="photo-cell" rowspan="8">
          <div class="photo-box">
            <template v-if="form.photo"><img :src="form.photo" class="elder-photo" alt=""/></template>
            <template v-else><div class="photo-placeholder">照片</div></template>
          </div>
        </td>
      </tr>

      <!-- 第2行：国籍、出生日期 -->
      <tr>
        <td class="lbl">国籍</td>
        <td colspan="2"><FieldItem :model-value="form.nationality" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('nationality',v)" /></td>
        <td class="lbl">出生日期</td>
        <td colspan="2"><FieldItem :model-value="form.birthDate" :is-text-mode="isTextMode" type="date" width="100%" @update:model-value="(v:any)=>updateForm('birthDate',v)" /></td>
      </tr>

      <!-- 第3行：籍贯、户籍地 -->
      <tr>
        <td class="lbl">籍贯</td>
        <td colspan="2"><FieldItem :model-value="form.nativePlace" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('nativePlace',v)" /></td>
        <td class="lbl">户籍地</td>
        <td colspan="2"><FieldItem :model-value="form.householdPlace" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('householdPlace',v)" /></td>
      </tr>

      <!-- 第4行：民族、政治面貌 -->
      <tr>
        <td class="lbl">民族</td>
        <td colspan="2" class="check-cell">
          <template v-if="!isTextMode">
            <label><input type="radio" :checked="form.nation==='汉族'" @change="updateForm('nation','汉族')" /> 汉族</label>
            <label><input type="radio" :checked="form.nation==='其他'" @change="updateForm('nation','其他')" /> 其他：<input :value="form.nationOther" class="inline-input small" placeholder="" @input="onInlineInput('nationOther',$event)" /></label>
          </template>
          <template v-else>{{ printCheck(form.nation==='汉族') }} 汉族 {{ printCheck(form.nation==='其他') }} 其他：{{ form.nationOther }}</template>
        </td>
        <td class="lbl">政治面貌</td>
        <td colspan="2"><FieldItem :model-value="form.politicalStatus" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('politicalStatus',v)" /></td>
      </tr>

      <!-- 第5行：婚姻状况 -->
      <tr>
        <td class="lbl">婚姻状况</td>
        <td colspan="5" class="check-cell">
          <template v-if="!isTextMode">
            <label v-for="it in ['未婚','已婚','丧偶','离异','未说明婚姻情况']" :key="it"><input type="checkbox" :checked="form.maritalStatus===it" @change="()=>updateForm('maritalStatus',it)" /> {{ it }}</label>
          </template>
          <template v-else><template v-for="it in ['未婚','已婚','丧偶','离异','未说明婚姻情况']" :key="it">{{ printCheck(form.maritalStatus===it) }} {{ it }} </template></template>
        </td>
      </tr>

      <!-- 第6行：证件类型、证件号码 -->
      <tr>
        <td class="lbl">证件类型</td>
        <td colspan="2"><FieldItem :model-value="form.idCardType" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('idCardType',v)" /></td>
        <td class="lbl">证件号码</td>
        <td colspan="2"><FieldItem :model-value="form.idCardNo" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('idCardNo',v)" /></td>
      </tr>

      <!-- 第7行：文化程度 -->
      <tr>
        <td class="lbl">文化程度</td>
        <td colspan="5" class="check-cell">
          <template v-if="!isTextMode">
            <label v-for="it in ['文盲半文盲','小学','初中','高中/技校/中专','大学专科及以上','不详']" :key="it"><input type="checkbox" :checked="form.educationLevel===it" @change="()=>updateForm('educationLevel',it)" /> {{ it }}</label>
          </template>
          <template v-else><template v-for="it in ['文盲半文盲','小学','初中','高中/技校/中专','大学专科及以上','不详']" :key="it">{{ printCheck(form.educationLevel===it) }} {{ it }} </template></template>
        </td>
      </tr>

      <!-- 第8行：居住地址（照片rowspan=8结束） -->
      <tr>
        <td class="lbl">居住地址</td>
        <td colspan="5"><FieldItem :model-value="form.residentialAddress" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('residentialAddress',v)" /></td>
      </tr>

      <!-- ========== 担保人/联系人区域（照片已结束，每行严格7列） ========== -->
      <!-- 担保人第1行：担保人(1) | 姓名/名称(1) | guarantorName(1) | 关系(1) | guarantorRelation(1) | 手机号(1) | guarantorPhone(1) = 7列 -->
      <tr>
        <td class="lbl multi-line" rowspan="3">担保人/<br/>监护人/<br/>紧急<br/>联系人</td>
        <td class="lbl">姓名/名称</td>
        <td><FieldItem :model-value="form.guarantorName" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorName',v)" /></td>
        <td class="lbl">关系</td>
        <td><FieldItem :model-value="form.guarantorRelation" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorRelation',v)" /></td>
        <td class="lbl">手机号</td>
        <td><FieldItem :model-value="form.guarantorPhone" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorPhone',v)" /></td>
      </tr>

      <!-- 担保人第2行：证件类型(1) | guarantorIdCardType(1) | 证件号码(1) | guarantorIdCardNo(4) = 7列(已含担保人rowspan) -->
      <tr>
        <td class="lbl">证件类型</td>
        <td><FieldItem :model-value="form.guarantorIdCardType" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorIdCardType',v)" /></td>
        <td class="lbl" colspan="2">证件号码</td>
        <td colspan="2"><FieldItem :model-value="form.guarantorIdCardNo" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorIdCardNo',v)" /></td>
      </tr>

      <!-- 担保人第3行：通信地址(1) | guarantorAddress(3) | 电子邮箱(1) | guarantorEmail(1) = 7列 -->
      <tr>
        <td class="lbl">通信地址</td>
        <td colspan="3"><FieldItem :model-value="form.guarantorAddress" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorAddress',v)" /></td>
        <td class="lbl">电子邮箱</td>
        <td><FieldItem :model-value="form.guarantorEmail" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('guarantorEmail',v)" /></td>
      </tr>

      <!-- 第二联系人：标签(2) + 姓名(2) + 标签(1) + 手机号(2) = 7列 -->
      <tr>
        <td class="lbl" colspan="2">第二联系人</td>
        <td colspan="2"><FieldItem :model-value="form.secondContactName" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('secondContactName',v)" /></td>
        <td class="lbl">手机号</td>
        <td colspan="2"><FieldItem :model-value="form.secondContactPhone" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('secondContactPhone',v)" /></td>
      </tr>

      <!-- 医疗费用支付方式 -->
      <tr>
        <td class="lbl multi-line">医疗费用<br/>支付方式</td>
        <td colspan="6" class="check-cell">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.medicalPaymentMethod==='城镇职工基本医疗保险'" @change="()=>updateForm('medicalPaymentMethod','城镇职工基本医疗保险')" /> 城镇职工基本医疗保险</label>
            <label><input type="checkbox" :checked="form.medicalPaymentMethod==='城镇居民基本医疗保险'" @change="()=>updateForm('medicalPaymentMethod','城镇居民基本医疗保险')" /> 城镇居民基本医疗保险</label>
            <label><input type="checkbox" :checked="form.medicalPaymentMethod==='全公费'" @change="()=>updateForm('medicalPaymentMethod','全公费')" /> 全公费</label>
            <label><input type="checkbox" :checked="form.medicalPaymentMethod==='其它'" @change="()=>updateForm('medicalPaymentMethod','其它')" /> 其它：<input :value="form.medicalPaymentOther" class="inline-input small" placeholder="" @input="onInlineInput('medicalPaymentOther',$event)" /></label>
          </template>
          <template v-else>{{ printCheck(form.medicalPaymentMethod==='城镇职工基本医疗保险') }} 城镇职工基本医疗 {{ printCheck(form.medicalPaymentMethod==='城镇居民基本医疗保险') }} 城镇居民基本医疗 {{ printCheck(form.medicalPaymentMethod==='全公费') }} 全公费 {{ printCheck(form.medicalPaymentMethod==='其它') }} 其它：{{ form.medicalPaymentOther }}</template>
        </td>
      </tr>

      <!-- 经济来源 -->
      <tr>
        <td class="lbl">经济来源</td>
        <td colspan="6" class="check-cell">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.incomeSource.includes('退休金/养老金')" @change="toggleArrayItem('incomeSource','退休金/养老金')" /> 退休金/养老金</label>
            <label><input type="checkbox" :checked="form.incomeSource.includes('子女补贴')" @change="toggleArrayItem('incomeSource','子女补贴')" /> 子女补贴</label>
            <label><input type="checkbox" :checked="form.incomeSource.includes('亲友资助')" @change="toggleArrayItem('incomeSource','亲友资助')" /> 亲友资助</label>
            <label><input type="checkbox" :checked="form.incomeSource.includes('其他补贴')" @change="toggleArrayItem('incomeSource','其他补贴')" /> 其他补贴：<input :value="form.incomeSourceOther" class="inline-input small" placeholder="" @input="onInlineInput('incomeSourceOther',$event)" /></label>
          </template>
          <template v-else>{{ printCheck(form.incomeSource.includes('退休金/养老金')) }} 退休金/养老金 {{ printCheck(form.incomeSource.includes('子女补贴')) }} 子女补贴 {{ printCheck(form.incomeSource.includes('亲友资助')) }} 亲友资助 {{ printCheck(form.incomeSource.includes('其他补贴')) }} 其他补贴：{{ form.incomeSourceOther }}</template>
        </td>
      </tr>

      <!-- 入住前居住 -->
      <tr>
        <td class="lbl multi-line">入住前<br/>居住</td>
        <td colspan="6" class="check-cell">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('家（独居）')" @change="toggleArrayItem('preResidenceHomeType','家（独居）')" /> 家（独居）</label>
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('配偶')" @change="toggleArrayItem('preResidenceHomeType','配偶')" /> 配偶</label>
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('子女')" @change="toggleArrayItem('preResidenceHomeType','子女')" /> 子女</label>
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('保姆')" @change="toggleArrayItem('preResidenceHomeType','保姆')" /> 保姆</label>
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('其他')" @change="toggleArrayItem('preResidenceHomeType','其他')" /> 其他：<input :value="form.preResidenceHomeOther" class="inline-input small" placeholder="" @input="onInlineInput('preResidenceHomeOther',$event)" /></label>
            <label><input type="checkbox" :checked="form.preResidenceHomeType.includes('医院')" @change="toggleArrayItem('preResidenceHomeType','医院')" /> 医院</label>
          </template>
          <template v-else>{{ printCheck(form.preResidenceHomeType.includes('家（独居）')) }} 家（独居） {{ printCheck(form.preResidenceHomeType.includes('配偶')) }} 配偶 {{ printCheck(form.preResidenceHomeType.includes('子女')) }} 子女 {{ printCheck(form.preResidenceHomeType.includes('保姆')) }} 保姆 {{ printCheck(form.preResidenceHomeType.includes('其他')) }} 其他：{{ form.preResidenceHomeOther }} {{ printCheck(form.preResidenceHomeType.includes('医院')) }} 医院</template>
        </td>
      </tr>

      <!-- 入住机构原因 -->
      <tr>
        <td class="lbl multi-line">入住机构<br/>原因</td>
        <td colspan="6"><FieldItem :model-value="form.checkInReason" :is-text-mode="isTextMode" width="100%" @update:model-value="(v:any)=>updateForm('checkInReason',v)" /></td>
      </tr>

      <!-- ========== 健康状况 ========== -->
      <!-- 健康状况rowspan=10覆盖：既往史2行 + 手术史1行 + 外伤史2行 + 现患疾病2行 + 就医情况3行 -->
      <!-- 既往史第1行：健康(1) | 既往史(1) | 内容(5) = 7列 -->
      <tr>
        <td class="lbl multi-line" rowspan="10">健康状况</td>
        <td class="lbl" rowspan="2">既往史</td>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.pastHistoryDiseaseNone === true" @change="onPastHistoryNoneChange" /> 无 </label>
            <label><input type="checkbox" :checked="form.pastHistoryDiseaseNone === false" @change="updateForm('pastHistoryDiseaseNone',false)" /> 有：疾病名称：</label>
            <input :value="form.pastHistoryDiseaseName1" class="inline-input medium" placeholder="" @input="onInlineInput('pastHistoryDiseaseName1',$event)" />
            <span class="text-suffix">，确诊时间：</span>
            <input :value="form.pastHistoryDiseaseTime1" class="inline-input small" placeholder="" @input="onInlineInput('pastHistoryDiseaseTime1',$event)" />
          </template>
          <template v-else>{{ printCheck(form.pastHistoryDiseaseNone === true) }} 无 {{ printCheck(form.pastHistoryDiseaseNone === false) }} 有：疾病名称：{{ form.pastHistoryDiseaseName1 }} ，确诊时间：{{ form.pastHistoryDiseaseTime1 }}</template>
        </td>
      </tr>
      <!-- 既往史第2行：(健康继续) | (既往史继续) | 内容(5) -->
      <tr>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-prefix"></span>
            <span class="text-suffix">疾病名称：</span>
            <input :value="form.pastHistoryDiseaseName2" class="inline-input medium" placeholder="" @input="onInlineInput('pastHistoryDiseaseName2',$event)" />
            <span class="text-suffix">，确诊时间：</span>
            <input :value="form.pastHistoryDiseaseTime2" class="inline-input small" placeholder="" @input="onInlineInput('pastHistoryDiseaseTime2',$event)" />
          </template>
          <template v-else>疾病名称：{{ form.pastHistoryDiseaseName2 }} ，确诊时间：{{ form.pastHistoryDiseaseTime2 }}</template>
        </td>
      </tr>

      <!-- 手术史：健康(1) | 手术史(1) | 内容(5) = 7列 -->
      <tr>
        <td class="lbl">手术史</td>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.pastHistorySurgery === false" @change="updateForm('pastHistorySurgery',false)" /> 无 </label>
            <label><input type="checkbox" :checked="form.pastHistorySurgery === true" @change="updateForm('pastHistorySurgery',true)" /> 有：手术名称：</label>
            <input :value="form.pastHistorySurgeryName" class="inline-input medium" placeholder="" @input="onInlineInput('pastHistorySurgeryName',$event)" />
            <span class="text-suffix">，手术时间：</span>
            <input :value="form.pastHistorySurgeryTime" class="inline-input small" placeholder="" @input="onInlineInput('pastHistorySurgeryTime',$event)" />
          </template>
          <template v-else>{{ printCheck(form.pastHistorySurgery === false) }} 无 {{ printCheck(form.pastHistorySurgery === true) }} 有：手术名称：{{ form.pastHistorySurgeryName }} ，手术时间：{{ form.pastHistorySurgeryTime }}</template>
        </td>
      </tr>

      <!-- 外伤史第1行：健康(1) | 外伤史(1 rowspan=2) | 内容(5) = 7列 -->
      <tr>
        <td class="lbl" rowspan="2">外伤史</td>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.pastHistoryTrauma === false" @change="updateForm('pastHistoryTrauma',false)" /> 无 </label>
            <label><input type="checkbox" :checked="form.pastHistoryTrauma === true" @change="updateForm('pastHistoryTrauma',true)" /> 有：外伤部位：</label>
            <input :value="form.pastHistoryTraumaPart1" class="inline-input medium" placeholder="" @input="onInlineInput('pastHistoryTraumaPart1',$event)" />
            <span class="text-suffix">，发生时间：</span>
            <input :value="form.pastHistoryTraumaTime1" class="inline-input small" placeholder="" @input="onInlineInput('pastHistoryTraumaTime1',$event)" />
          </template>
          <template v-else>{{ printCheck(form.pastHistoryTrauma === false) }} 无 {{ printCheck(form.pastHistoryTrauma === true) }} 有：外伤部位：{{ form.pastHistoryTraumaPart1 }} ，发生时间：{{ form.pastHistoryTraumaTime1 }}</template>
        </td>
      </tr>
      <!-- 外伤史第2行：(健康继续) | (外伤史继续) | 内容(5) -->
      <tr>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-prefix ml20"></span>
            <span class="text-suffix">外伤部位：</span>
            <input :value="form.pastHistoryTraumaPart2" class="inline-input medium" placeholder="" @input="onInlineInput('pastHistoryTraumaPart2',$event)" />
            <span class="text-suffix">，发生时间：</span>
            <input :value="form.pastHistoryTraumaTime2" class="inline-input small" placeholder="" @input="onInlineInput('pastHistoryTraumaTime2',$event)" />
          </template>
          <template v-else>外伤部位：{{ form.pastHistoryTraumaPart2 }} ，发生时间：{{ form.pastHistoryTraumaTime2 }}</template>
        </td>
      </tr>

      <!-- 现患疾病 -->
      <tr>
        <td class="lbl" rowspan="2">现患疾病</td>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-suffix">疾病名称：</span>
            <input :value="form.currentDiseaseName1" class="inline-input medium" placeholder="" @input="onInlineInput('currentDiseaseName1',$event)" />
            <span class="text-suffix">，确诊时间：</span>
            <input :value="form.currentDiseaseTime1" class="inline-input small" placeholder="" @input="onInlineInput('currentDiseaseTime1',$event)" />
            <span class="text-suffix">，目前状况：</span>
            <input :value="form.currentDiseaseStatus1" class="inline-input small" placeholder="" @input="onInlineInput('currentDiseaseStatus1',$event)" />
          </template>
          <template v-else>疾病名称：{{ form.currentDiseaseName1 }} ，确诊时间：{{ form.currentDiseaseTime1 }} ，目前状况：{{ form.currentDiseaseStatus1 }}</template>
        </td>
      </tr>
      <tr>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-suffix">疾病名称：</span>
            <input :value="form.currentDiseaseName2" class="inline-input medium" placeholder="" @input="onInlineInput('currentDiseaseName2',$event)" />
            <span class="text-suffix">，确诊时间：</span>
            <input :value="form.currentDiseaseTime2" class="inline-input small" placeholder="" @input="onInlineInput('currentDiseaseTime2',$event)" />
            <span class="text-suffix">，目前状况：</span>
            <input :value="form.currentDiseaseStatus2" class="inline-input small" placeholder="" @input="onInlineInput('currentDiseaseStatus2',$event)" />
          </template>
          <template v-else>疾病名称：{{ form.currentDiseaseName2 }} ，确诊时间：{{ form.currentDiseaseTime2 }} ，目前状况：{{ form.currentDiseaseStatus2 }}</template>
        </td>
      </tr>

      <!-- 就医情况 -->
      <tr>
        <td class="lbl multi-line" rowspan="3">就医情况</td>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-suffix">固定时间就诊：</span>
            <label><input type="checkbox" :checked="form.regularVisitNone === true" @change="updateForm('regularVisitNone',true)" /> 无 </label>
            <label><input type="checkbox" :checked="form.regularVisitNone === false" @change="updateForm('regularVisitNone',false)" /> 有（原因：</label>
            <input :value="form.regularVisitReason" class="inline-input small" placeholder="" @input="onInlineInput('regularVisitReason',$event)" />
            <span class="text-suffix">，频率：___次/月）</span>
          </template>
          <template v-else>固定时间就诊：{{ printCheck(form.regularVisitNone === true) }} 无 {{ printCheck(form.regularVisitNone === false) }} 有（原因：{{ form.regularVisitReason }} ，频率：___次/月）</template>
        </td>
      </tr>
      <tr>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-suffix">近一年内住院情况：</span>
            <label><input type="checkbox" :checked="form.hospitalizationNone === true" @change="updateForm('hospitalizationNone',true)" /> 未住院 </label>
            <label><input type="checkbox" :checked="form.hospitalizationNone === false" @change="updateForm('hospitalizationNone',false)" /> 住过院（住院次数：<input :value="form.hospitalizationCount" class="inline-input tiny" placeholder="" @input="onInlineInput('hospitalizationCount',$event)" /> 次）</label>
          </template>
          <template v-else>近一年内住院情况：{{ printCheck(form.hospitalizationNone === true) }} 未住院 {{ printCheck(form.hospitalizationNone === false) }} 住过院（住院次数：{{ form.hospitalizationCount }} 次）</template>
        </td>
      </tr>
      <tr>
        <td colspan="5" class="disease-row">
          <template v-if="!isTextMode">
            <span class="text-suffix">近一年内急诊情况：</span>
            <label><input type="checkbox" :checked="form.emergencyNone === true" @change="updateForm('emergencyNone',true)" /> 未去过 </label>
            <label><input type="checkbox" :checked="form.emergencyNone === false" @change="updateForm('emergencyNone',false)" /> 去过急诊（去急诊次数：<input :value="form.emergencyCount" class="inline-input tiny" placeholder="" @input="onInlineInput('emergencyCount',$event)" /> 次）</label>
          </template>
          <template v-else>近一年内急诊情况：{{ printCheck(form.emergencyNone === true) }} 未去过 {{ printCheck(form.emergencyNone === false) }} 去过急诊（去急诊次数：{{ form.emergencyCount }} 次）</template>
        </td>
      </tr>

      <!-- 不适症状 -->
      <tr>
        <td class="lbl multi-line">不适症状<br/>（近一个<br/>月内情况）</td>
        <td colspan="6" class="symptom-cell">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.discomfortSymptomsNone === true" @change="updateForm('discomfortSymptomsNone',true);form.discomfortSymptomsList=[]" /> 无</label>
            <br/>
            <label><input type="checkbox" :checked="form.discomfortSymptomsNone === false" @change="updateForm('discomfortSymptomsNone',false)" /> 有（可多选）</label>
            <div v-if="form.discomfortSymptomsNone === false" class="symptom-list">
              <label v-for="s in symptomOptions" :key="s" class="symptom-item">
                <input type="checkbox" :checked="form.discomfortSymptomsList.includes(s)" @change="toggleArrayItem('discomfortSymptomsList',s)" /> {{ s }}
              </label>
              <span>其他：<input :value="form.discomfortSymptomsOther" class="inline-input small" placeholder="" @input="onInlineInput('discomfortSymptomsOther',$event)" /></span>
            </div>
          </template>
          <template v-else>
            {{ printCheck(form.discomfortSymptomsNone === true) }} 无
            <template v-if="form.discomfortSymptomsNone === false">{{ printCheck(true) }} 有：{{ form.discomfortSymptomsList.join('、') }} {{ form.discomfortSymptomsOther }}</template>
          </template>
        </td>
      </tr>

      <!-- 过敏史 -->
      <tr>
        <td class="lbl multi-line" rowspan="3">过敏史</td>
        <td colspan="6" class="allergy-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.allergyDrugNone === true" @change="updateForm('allergyDrugNone',true);updateForm('allergyDrugDetail','')" /> 无药物过敏 </label>
            <label><input type="checkbox" :checked="form.allergyDrugNone === false" @change="updateForm('allergyDrugNone',false)" /> 药物过敏：</label>
            <input :value="form.allergyDrugDetail" class="inline-input large" placeholder="" @input="onInlineInput('allergyDrugDetail',$event)" />
          </template>
          <template v-else>{{ printCheck(form.allergyDrugNone === true) }} 无药物过敏 {{ printCheck(form.allergyDrugNone === false) }} 药物过敏：{{ form.allergyDrugDetail }}</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="allergy-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.allergyFoodNone === true" @change="updateForm('allergyFoodNone',true);updateForm('allergyFoodDetail','')" /> 无食物过敏 </label>
            <label><input type="checkbox" :checked="form.allergyFoodNone === false" @change="updateForm('allergyFoodNone',false)" /> 食物过敏：</label>
            <input :value="form.allergyFoodDetail" class="inline-input large" placeholder="" @input="onInlineInput('allergyFoodDetail',$event)" />
          </template>
          <template v-else>{{ printCheck(form.allergyFoodNone === true) }} 无食物过敏 {{ printCheck(form.allergyFoodNone === false) }} 食物过敏：{{ form.allergyFoodDetail }}</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="allergy-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.allergyEnvNone === true" @change="updateForm('allergyEnvNone',true);updateForm('allergyEnvDetail','')" /> 无环境过敏 </label>
            <label><input type="checkbox" :checked="form.allergyEnvNone === false" @change="updateForm('allergyEnvNone',false)" /> 环境过敏：</label>
            <input :value="form.allergyEnvDetail" class="inline-input large" placeholder="" @input="onInlineInput('allergyEnvDetail',$event)" />
          </template>
          <template v-else>{{ printCheck(form.allergyEnvNone === true) }} 无环境过敏 {{ printCheck(form.allergyEnvNone === false) }} 环境过敏：{{ form.allergyEnvDetail }}</template>
        </td>
      </tr>

      <!-- 老年综合征 -->
      <tr>
        <td class="lbl multi-line" rowspan="9">老年综合<br/>征</td>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.fallNone === true" @change="updateForm('fallNone',true);updateForm('fallDetail','')" /> 无跌倒 </label>
            <label><input type="checkbox" :checked="form.fallNone === false" @change="updateForm('fallNone',false)" /> 有跌倒（</label>
            <input :value="form.fallDetail" class="inline-input medium" placeholder="详情" @input="onInlineInput('fallDetail',$event)" />
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.fallNone === true) }} 无跌倒 {{ printCheck(form.fallNone === false) }} 有跌倒（{{ form.fallDetail }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.memoryDeclineNone === true" @change="updateForm('memoryDeclineNone',true);updateForm('memoryDeclineDetail','')" /> 无记忆力减退 </label>
            <label><input type="checkbox" :checked="form.memoryDeclineNone === false" @change="updateForm('memoryDeclineNone',false)" /> 有记忆力减退（</label>
            <input :value="form.memoryDeclineDetail" class="inline-input medium" placeholder="详情" @input="onInlineInput('memoryDeclineDetail',$event)" />
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.memoryDeclineNone === true) }} 无记忆力减退 {{ printCheck(form.memoryDeclineNone === false) }} 有记忆力减退（{{ form.memoryDeclineDetail }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.weightLossNone === true" @change="updateForm('weightLossNone',true);updateForm('weightLossWeight','')" /> 无体重下降 </label>
            <label><input type="checkbox" :checked="form.weightLossNone === false" @change="updateForm('weightLossNone',false)" /> 有体重下降（半年内下降</label>
            <input :value="form.weightLossWeight" class="inline-input tiny" placeholder="kg" @input="onInlineInput('weightLossWeight',$event)" />
            <span>kg）</span>
          </template>
          <template v-else>{{ printCheck(form.weightLossNone === true) }} 无体重下降 {{ printCheck(form.weightLossNone === false) }} 有体重下降（半年内下降{{ form.weightLossWeight }}kg）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.urinaryIncontinenceNone === true" @change="updateForm('urinaryIncontinenceNone',true);updateForm('urinaryIncontinenceCount','')" /> 无尿失禁 </label>
            <label><input type="checkbox" :checked="form.urinaryIncontinenceNone === false" @change="updateForm('urinaryIncontinenceNone',false)" /> 有尿失禁（</label>
            <input :value="form.urinaryIncontinenceCount" class="inline-input small" placeholder="次数/日" @input="onInlineInput('urinaryIncontinenceCount',$event)" />
            <span>次/日）</span>
          </template>
          <template v-else>{{ printCheck(form.urinaryIncontinenceNone === true) }} 无尿失禁 {{ printCheck(form.urinaryIncontinenceNone === false) }} 有尿失禁（{{ form.urinaryIncontinenceCount }}次/日）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.sleepDisorderNone === true" @change="updateForm('sleepDisorderNone',true);form.sleepDisorderTypes=[]" /> 无睡眠障碍 </label>
            <label><input type="checkbox" :checked="form.sleepDisorderNone === false" @change="updateForm('sleepDisorderNone',false)" /> 有睡眠障碍（</label>
            <template v-if="form.sleepDisorderNone === false">
              <label v-for="st in ['入睡困难','早醒','夜间易醒','多梦']" :key="st" style="margin-right:4px;">
                <input type="checkbox" :checked="form.sleepDisorderTypes.includes(st)" @change="toggleArrayItem('sleepDisorderTypes',st)" /> {{ st }}
              </label>
              <span>其他：<input :value="form.sleepDisorderOther" class="inline-input small" placeholder="" @input="onInlineInput('sleepDisorderOther',$event)" /></span>
            </template>
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.sleepDisorderNone === true) }} 无睡眠障碍 {{ printCheck(form.sleepDisorderNone === false) }} 有睡眠障碍（{{ [...form.sleepDisorderTypes, form.sleepDisorderOther].filter(Boolean).join('、') }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.painNone === true" @change="updateForm('painNone',true);updateForm('painPart','')" /> 无疼痛 </label>
            <label><input type="checkbox" :checked="form.painNone === false" @change="updateForm('painNone',false)" /> 有疼痛（部位：</label>
            <input :value="form.painPart" class="inline-input medium" placeholder="" @input="onInlineInput('painPart',$event)" />
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.painNone === true) }} 无疼痛 {{ printCheck(form.painNone === false) }} 有疼痛（部位：{{ form.painPart }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.visionAbnormalNone === true" @change="updateForm('visionAbnormalNone',true);form.visionAbnormalTypes=[]" /> 无视力异常 </label>
            <label><input type="checkbox" :checked="form.visionAbnormalNone === false" @change="updateForm('visionAbnormalNone',false)" /> 有视力异常（</label>
            <template v-if="form.visionAbnormalNone === false">
              <label v-for="vt in ['远视','近视','散光','白内障','青光眼']" :key="vt" style="margin-right:4px;">
                <input type="checkbox" :checked="form.visionAbnormalTypes.includes(vt)" @change="toggleArrayItem('visionAbnormalTypes',vt)" /> {{ vt }}
              </label>
              <span>程度：<input :value="form.visionAbnormalDegree" class="inline-input small" placeholder="" @input="onInlineInput('visionAbnormalDegree',$event)" /></span>
            </template>
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.visionAbnormalNone === true) }} 无视力异常 {{ printCheck(form.visionAbnormalNone === false) }} 有视力异常（{{ [...form.visionAbnormalTypes, form.visionAbnormalDegree].filter(Boolean).join('、') }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.hearingLossNone === true" @change="updateForm('hearingLossNone',true);updateForm('hearingLossDetail','')" /> 无听力障碍 </label>
            <label><input type="checkbox" :checked="form.hearingLossNone === false" @change="updateForm('hearingLossNone',false)" /> 有听力障碍（</label>
            <input :value="form.hearingLossDetail" class="inline-input medium" placeholder="详情" @input="onInlineInput('hearingLossDetail',$event)" />
            <span>）</span>
          </template>
          <template v-else>{{ printCheck(form.hearingLossNone === true) }} 无听力障碍 {{ printCheck(form.hearingLossNone === false) }} 有听力障碍（{{ form.hearingLossDetail }}）</template>
        </td>
      </tr>
      <tr>
        <td colspan="6" class="syndrome-row">
          <template v-if="!isTextMode">
            <label><input type="checkbox" :checked="form.mentalStatusNone === true" @change="updateForm('mentalStatusNone',true);form.mentalStatusList=[]" /> 无精神问题 </label>
            <label><input type="checkbox" :checked="form.mentalStatusNone === false" @change="updateForm('mentalStatusNone',false)" /> 有（可多选）</label>
            <div v-if="form.mentalStatusNone === false" class="mental-list">
              <label v-for="m in mentalOptions" :key="m" class="mental-item">
                <input type="checkbox" :checked="form.mentalStatusList.includes(m)" @change="toggleArrayItem('mentalStatusList',m)" /> {{ m }}
              </label>
              <span>其他：<input :value="form.mentalStatusOther" class="inline-input small" placeholder="" @input="onInlineInput('mentalStatusOther',$event)" /></span>
            </div>
          </template>
          <template v-else>
            {{ printCheck(form.mentalStatusNone === true) }} 无精神问题
            <template v-if="form.mentalStatusNone === false">{{ printCheck(true) }} 有：{{ form.mentalStatusList.join('、') }} {{ form.mentalStatusOther }}</template>
          </template>
        </td>
      </tr>

      <!-- 语言表达 -->
      <tr>
        <td class="lbl">语言表达</td>
        <td colspan="6" class="check-cell">
          <template v-if="!isTextMode">
            <label v-for="le in ['清晰','含糊','失语','方言']" :key="le"><input type="checkbox" :checked="form.languageExpression.includes(le)" @change="toggleArrayItem('languageExpression',le)" /> {{ le }}</label>
            <span>其他：<input :value="form.languageExpressionOther" class="inline-input small" placeholder="" @input="onInlineInput('languageExpressionOther',$event)" /></span>
          </template>
          <template v-else>
            <template v-for="le in ['清晰','含糊','失语','方言']" :key="le">{{ printCheck(form.languageExpression.includes(le)) }} {{ le }} </template>
            其他：{{ form.languageExpressionOther }}
          </template>
        </td>
      </tr>

      <!-- 备注 -->
      <tr>
        <td class="lbl">备注</td>
        <td colspan="6">
          <template v-if="!isTextMode">
            <textarea v-model="form.otherNotes" rows="3" class="remark-textarea" placeholder="请输入备注信息" @input="emitForm"></textarea>
          </template>
          <template v-else>{{ form.otherNotes || '无' }}</template>
        </td>
      </tr>

      <!-- 签名区域：签章(2) + 乙方(2) + 签名(3) = 7列 -->
      <tr>
        <td class="lbl" rowspan="2" colspan="2">签章</td>
        <td colspan="2">乙方（入住人/监护人）签章：</td>
        <td colspan="3">
          <SignatureItem v-model="form.partyBSign" :is-text-mode="isTextMode" />
        </td>
      </tr>
      <tr>
        <td colspan="2">丙方（机构负责人）签章：</td>
        <td colspan="3">
          <SignatureItem v-model="form.partyCSign" :is-text-mode="isTextMode" />
        </td>
      </tr>
      <tr>
        <td class="lbl" colspan="2">签署日期</td>
        <td colspan="5">
          <FieldItem :model-value="form.signDate" :is-text-mode="isTextMode" type="date" width="180px" @update:model-value="(v:any)=>updateForm('signDate',v)" />
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped lang="scss">
/* A4纸宽度适配：210mm × 297mm，考虑打印边距后内容区约 190mm */
.check-in-registration {
  width: 210mm;
  max-width: 100%;
  margin: 0 auto;
  font-family: 'SimSun', 'Microsoft YaHei', serif;
  font-size: 14px;
  color: #333;
  background: #fff;
  padding: 4mm;

  /* 打印模式优化：A4纸尺寸 */
  @media print {
    width: 210mm !important;

    padding: 0;
    border: none;
    margin: 0;
    box-sizing: border-box;
  }
}

.doc-title {
  text-align: center;
  font-size: 22px;
  font-weight: bold;
  letter-spacing: 8px;
  margin: 16px 0 12px;
  color: #000;
}

.doc-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 14px;
}

/* 表格基础样式 - A4纸适配 */
.reg-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed; /* 固定表格布局，按比例分配列宽 */
  font-size: 13px;

  td {
    border: 1px solid #333;
    padding: 3mm 2mm;
    vertical-align: middle;
    word-break: break-all;
  }

  /* 打印模式 */
  @media print {
    td {
      border-color: #000;
      padding: 2mm 1.5mm;
    }
  }
}

/* 标签单元格 - 紧凑型，只留最小padding */
.lbl {
  background-color: #f5f7fa;
  font-weight: bold;
  text-align: center;
  white-space: nowrap;
  padding: 2mm 0mm !important; /* 标签列更紧凑 */

  &.multi-line {
    line-height: 1.4;
    padding: 4px;
  }

  /* 打印模式 */
  @media print {
    background-color: #f0f0f0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* 列宽定义 - 已改用内联style精确控制 */
/* .w80 { width: 80px; }
.w120 { width: 120px; }
.w60 { width: 60px; }
.w90 { width: 90px; }
.w50 { width: 50px; } */

/* 单选格 */
.radio-cell {
  white-space: nowrap;

  label {
    margin-right: 12px;
    cursor: pointer;

    input[type="radio"] {
      margin-right: 2px;
    }
  }
}

/* 多选格 */
.check-cell {
  label {
    margin-right: 16px;
    cursor: pointer;
    white-space: nowrap;

    input[type="checkbox"] {
      margin-right: 2px;
    }
  }

  @media print {
    label {
      margin-right: 12px;
    }
  }
}

/* 照片单元格 - 最右侧第7列，由colgroup控制宽度 */
.photo-cell {
  text-align: center;
  vertical-align: top !important;
  padding: 2mm 1mm;
  background: #fafafa;

  .photo-box {
    width: 31mm;
    height: 40mm;
    
    border: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: #fff;

    .elder-photo {
      max-width: 100%;
      max-height: 100%;
      object-fit: cover;
    }

    .photo-placeholder {
      color: #999;
      font-size: 14px;
    }
  }

  @media print {
    background: #fff !important;
  }
}

/* 行内输入框 */
.inline-input {
  border: 1px solid #dcdfe6;
  border-radius: 3px;
  padding: 2px 6px;
  font-size: 13px;
  outline: none;
  vertical-align: middle;
  background: #fff;

  &:focus {
    border-color: #409eff;
  }

  &.tiny { width: 40px; }
  &.small { width: 80px; }
  &.medium { width: 140px; }
  &.large { width: 280px; }
}

/* 文本前后缀 */
.text-prefix {
  display: inline-block;
  margin-left: 20px;
}
.ml20 { margin-left: 20px; }
.text-suffix {
  margin: 0 4px;
  white-space: nowrap;
}

/* 疾病行 */
.disease-row {
  label {
    white-space: nowrap;
    input[type="checkbox"] { margin-right: 2px; }
  }
}

/* 不适症状 */
.symptom-cell {
  line-height: 1.8;

  .symptom-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    margin-top: 6px;
    padding: 8px;
    background: #fafafa;
    border-radius: 4px;

    .symptom-item {
      margin: 0;
    }
  }
}

/* 过敏行 */
.allergy-row {
  label {
    white-space: nowrap;
    input[type="checkbox"] { margin-right: 2px; }
  }
}

/* 老年综合征行 */
.syndrome-row {
  label {
    white-space: nowrap;
    input[type="checkbox"] { margin-right: 2px; }
  }
}

/* 精神状态列表 */
.mental-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-top: 6px;
  padding: 8px;
  background: #fafafa;
  border-radius: 4px;

  .mental-item {
    margin: 0;
  }
}

/* 备注文本域 */
.remark-textarea {
  width: 98%;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  outline: none;
  box-sizing: border-box;

  &:focus {
    border-color: #409eff;
  }
}

/* 打印模式全局样式 */
@media print {
  
  .check-in-registration {
    font-size: 12pt;
  }

  .doc-title {
    font-size: 18pt;
  }

  .reg-table {
    font-size: 11pt;
  }

  .inline-input,
  .remark-textarea {
    border: none;
    border-bottom: 1px solid #333;
    background: transparent;
  }

  .photo-box {
    border: 1px solid #333;
  }

  .symptom-list,
  .mental-list {
    background: transparent;
    border: none;
  }
}
</style>
