<template>
  <el-drawer
    v-model="dialogVisible"
    :title="title"
    resizable
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    size="70%"
    :before-close="handleClosed"
  >

    <div class="mmse-form">
      <h1 class="form-title">感知觉与沟通评估表</h1>
      <!-- 基本信息 -->
      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>长者姓名</text>
          <search-the-elderly ref="selectElderRef" :disabled="isDetail" @update_elder="elderUp" v-model="dataForm.elderName" :tId="dataForm.tenantId"/>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>档案号</text>
          <el-input v-if="!isDetail" v-model="dataForm.contractNumber" disabled />
          <el-text v-else disabled="">{{dataForm.contractNumber}}</el-text>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>入院日期</text>
          <el-input v-if="!isDetail" :model-value="dayjs(dataForm.checkInTime).format('YYYY-MM-DD')=='Invalid Date'?'':dayjs(dataForm.checkInTime).format('YYYY-MM-DD')" disabled />
          <el-text v-else disabled="">{{dayjs(dataForm.checkInTime).format('YYYY-MM-DD')}}</el-text>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>床位号</text>
          <el-input v-if="!isDetail" v-model="dataForm.bedName" disabled />
          <el-text v-else disabled="">{{dataForm.bedName}}</el-text>
        </el-col>
      </el-row>

      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>评估人</text>
          <el-input :disabled="isDetail" v-model="form.assessor"  />
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>评估日期</text>
          <el-date-picker :disabled="isDetail" v-model="form.assessDate" type="date" style="width: 100%;"/>

        </el-col>
      </el-row>

      <!-- 表单内容区域 -->
      <div class="form-body">
        <!-- 总分显示 -->
        <div class="total-score-section">
          <div class="total-score">
            <span class="score-label">总得分：</span>
            <span class="score-value">{{ totalScore }}</span>
            <span class="score-max">/ 4分</span>
          </div>
        </div>

        <!-- 评估表格 -->
        <div class="assessment-table">
          <table>
            <thead>
              <tr>
                <th class="criteria-col">评估准则</th>
                <th class="score-col">分值</th>
                <th class="select-col">选择</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td class="criteria">能看清书报上的标准字体</td>
                <td class="score">0分</td>
                <td class="select">
                  <el-radio v-model="form.visionScore" :label="0" :disabled="isDetail"/>
                </td>
              </tr>
              <tr>
                <td class="criteria">能看清楚大字体；但看不清书报上的标准字体</td>
                <td class="score">1分</td>
                <td class="select">
                  <el-radio v-model="form.visionScore" :label="1" :disabled="isDetail"/>
                </td>
              </tr>
              <tr>
                <td class="criteria">视力有限；看不清报纸大标题；但能辨认物体</td>
                <td class="score">2分</td>
                <td class="select">
                  <el-radio v-model="form.visionScore" :label="2" :disabled="isDetail"/>
                </td>
              </tr>
              <tr>
                <td class="criteria">辨认物体有困难；但眼睛能跟随物体移动；只能看到光、颜色和形状</td>
                <td class="score">3分</td>
                <td class="select">
                  <el-radio v-model="form.visionScore" :label="3" :disabled="isDetail"/>
                </td>
              </tr>
              <tr>
                <td class="criteria">没有视力；眼睛不能跟随物体移动</td>
                <td class="score">4分</td>
                <td class="select">
                  <el-radio v-model="form.visionScore" :label="4" :disabled="isDetail"/>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- 备注 -->
        <div class="remark-section">
          <span class="remark-label">备注：</span>
          <span class="remark-text">若平日戴老花镜或近视镜，应在佩戴眼镜的情况下评估。</span>
        </div>

        <!-- 风险程度判断 -->
        <div class="risk-judgment-section">
          <div class="risk-judgment-title">风险程度判断：</div>
          <div class="risk-options">
            <el-radio v-model="form.riskLevel" label="none" :disabled="isDetail">
                无风险：0分
            </el-radio>
            <el-radio v-model="form.riskLevel" label="low" :disabled="isDetail">
                低风险：1分
            </el-radio>
            <el-radio v-model="form.riskLevel" label="medium" :disabled="isDetail">
                中风险：2分
            </el-radio>
            <el-radio v-model="form.riskLevel" label="high" :disabled="isDetail">
                高风险：≥3分
            </el-radio>
          </div>
        </div>

        <!-- 预防措施 -->
        <div class="preventive-section">
          <div class="preventive-title">预防措施：</div>
          <div class="preventive-options">
            <el-checkbox v-model="form.preventiveMeasures" label="patrol" :disabled="isDetail">
                加强巡查
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="food" :disabled="isDetail">
                合理食物
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="expression" :disabled="isDetail">
                观察表情
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="medication" :disabled="isDetail">
                药物统一管控
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="feeding" :disabled="isDetail">
                严格协助进食操作规程
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="card" :disabled="isDetail">
                设立床头卡标识
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" label="other" :disabled="isDetail">
                其他
            </el-checkbox>
            <el-input v-if="form.preventiveMeasures.includes('other')" v-model="form.preventiveMeasuresOther" placeholder="请输入其他措施" :disabled="isDetail" class="other-input"/>
          </div>
        </div>

        <!-- 签名区域 -->
        <div class="signature-section">
          <div class="signature-row">
            <div class="signature-item">
              <span class="signature-label">家属/监护人签名：</span>
              <el-input v-model="form.familySignature" :disabled="isDetail" class="signature-input"/>
            </div>
<!--            <div class="signature-item date-item">-->
<!--              <span class="signature-label">日期：</span>-->
<!--              <el-date-picker v-model="form.familySignDate" type="date" :disabled="isDetail" class="date-picker"/>-->
<!--            </div>-->
          </div>
        </div>

        <!-- 说明 -->
        <div class="note-section">
          <div class="note-title">说明：</div>
          <div class="note-content">
            <p>1. 新入住长者应在长者入住24小时内完成首次评估；</p>
            <p>2. 定期评估：</p>
            <p class="indent">（1）认知照护专区：高风险1个月评估一次，中风险、低风险3个月评估一次；</p>
            <p class="indent">（2）非认知照护专区：高风险1个月评估一次，中风险3个月评估一次，低风险6个月评估一次；</p>
            <p>3. 当长者身体发生变化者，应及时进行动态评估。</p>
          </div>
        </div>
      </div>
    </div>


    <template #footer>
      <el-button @click="handleClosed">关闭</el-button>
      <el-button style="margin-left: 22px;margin-right: 30px" v-loading="formLoading" type="primary" v-show="!isDetail" @click="submitForm">确定</el-button>
      <el-button v-if="isDetail" type="success" @click="handleExport">打印</el-button>
    </template>

  </el-drawer>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'

import dayjs from 'dayjs'

import { perceptionCreate, perceptionGetById, perceptionUpdate, perceptionGetByElderId } from "@/api/social-work";
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const title = ref('')
const dialogVisible = ref(false) // 弹窗
const formRef = ref() // 表单 Ref
const selectElderRef = ref() // 表单 Ref
const isDetail = ref(false) // 是否详情打开
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
let dataForm = ref({
  // 表单字段
  id: undefined,
  idCard: '',
  contractNumber: '', //档案号
  elderName: '',//长者姓名
  bedName: '', //床位号
  elderAge: '', //年龄
  elderSex: '', //性别
  checkInTime: '', //入院日期
  elderId: '',
  tenantId: undefined
})




const elderUp = (e) => {
  dataForm.value.elderName = e.elderName
  dataForm.value.elderId = e.id
  dataForm.value.elderSex = e.elderSex === 1 ? '男' : '女'
  dataForm.value.bedName = e.bedName || ''
  dataForm.value.checkInTime = e.checkInTime
  dataForm.value.contractNumber = e.contractNumber
  dataForm.value.elderAge = e.elderAge
}

// ========== 感知觉与沟通评估表 表单序列化方法 ==========

/** 计算总分 */
const totalScore = computed(() => {
  return form.visionScore || 0
})

/** 风险程度文本 */
const riskLevelText = computed(() => {
  const score = totalScore.value
  if (score === 0) return '无风险'
  if (score === 1) return '低风险'
  if (score === 2) return '中风险'
  return '高风险'
})

/** 风险程度样式类 */
const riskLevelClass = computed(() => {
  const score = totalScore.value
  if (score === 0) return 'risk-none'
  if (score === 1) return 'risk-low'
  if (score === 2) return 'risk-medium'
  return 'risk-high'
})

/** 将表单数据序列化为 JSON 对象 */
const serializeFormData = () => {
  return {
    // 基本信息
    assessor: form.assessor || '',
    assessDate: form.assessDate ? dayjs(form.assessDate).format('YYYY-MM-DD') : '',

    // 视力评分
    visionScore: form.visionScore || 0,

    // 总分
    totalScore: totalScore.value,

    // 风险程度
    riskLevel: form.riskLevel || '',

    // 预防措施
    preventiveMeasures: form.preventiveMeasures || [],
    preventiveMeasuresOther: form.preventiveMeasuresOther || '',

    // 签名
    familySignature: form.familySignature || '',
    familySignDate: form.familySignDate ? dayjs(form.familySignDate).format('YYYY-MM-DD') : ''
  }
}

/** 将 JSON 对象反序列化为表单数据 */
const deserializeFormData = (formData: Record<string, any>) => {
  if (!formData) return

  // 基本信息
  form.assessor = formData.assessor || ''
  form.assessDate = formData.assessDate ? dayjs(formData.assessDate).toDate() : ''

  // 视力评分
  form.visionScore = formData.visionScore ?? 0

  // 风险程度
  form.riskLevel = formData.riskLevel || ''

  // 预防措施
  form.preventiveMeasures = formData.preventiveMeasures || []
  form.preventiveMeasuresOther = formData.preventiveMeasuresOther || ''

  // 签名
  form.familySignature = formData.familySignature || ''
  form.familySignDate = formData.familySignDate ? dayjs(formData.familySignDate).toDate() : ''
}

/** 重置感知觉与沟通评估表表单数据 */
const resetPerceptionForm = () => {
  form.assessor = ''
  form.assessDate = ''

  // 视力评分
  form.visionScore = 0

  // 风险程度
  form.riskLevel = ''

  // 预防措施
  form.preventiveMeasures = []
  form.preventiveMeasuresOther = ''

  // 签名
  form.familySignature = ''
  form.familySignDate = ''
}

/** 打开弹窗 */
const open = async (tenantId, id?: any, detail: boolean = false) => {
  resetForm()
  dialogVisible.value = true
  dataForm.value.id = id || undefined
  dataForm.value.tenantId = tenantId
  isDetail.value = detail
  if (id) {
    title.value = "编辑-感知觉与沟通评估"
    // 加载评估数据
    await loadPerceptionData(id)
  } else {
    title.value = "新增-感知觉与沟通评估"

  }
}

/** 加载评估数据 */
const loadPerceptionData = async (id: number) => {
  try {
    const res = await perceptionGetById(id)
    if (res) {
      // 填充长者基本信息
      dataForm.value.elderName = res.elderName || ''
      dataForm.value.elderId = res.elderId || ''
      dataForm.value.elderSex = res.elderSex || ''
      dataForm.value.bedName = res.bedName || ''
      dataForm.value.checkInTime = res.checkInTime || ''
      dataForm.value.contractNumber = res.fileNumber || ''
      dataForm.value.elderAge = res.elderAge || ''
      await selectElderRef.value.upData(res.elderName, res.elderId)
      // 解析 assessData
      if (res.assessData) {
        const formData = JSON.parse(res.assessData)
        deserializeFormData(formData)
      }
    }
  } catch (error) {
    message.error('加载评估数据失败')
  }
}

/** 根据长者ID加载评估数据 */
const loadPerceptionByElderId = async (elderId: number) => {
  try {
    const res = await perceptionGetByElderId(elderId)
    if (res && res.assessData) {
      const formData = JSON.parse(res.assessData)
      deserializeFormData(formData)
    }
  } catch (error) {
    // 无历史数据，不处理
  }
}

const form = reactive({
  // 基本信息
  assessor: '',
  assessDate: '',

  // 视力评分（0-4分）
  visionScore: 0,

  // 风险程度
  riskLevel: '',

  // 预防措施
  preventiveMeasures: [],
  preventiveMeasuresOther: '',

  // 签名
  familySignature: '',
  familySignDate: ''
})

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  if (formLoading.value) {
    return
  }
  formLoading.value = true
  // 提交请求
  try {
    const assessData = serializeFormData()
    const tempParams = {
      ...dataForm.value,
      assessData: JSON.stringify(assessData),
      totalScore: totalScore.value,
      assessor: form.assessor,
      assessDate: form.assessDate ? dayjs(form.assessDate).format('YYYY-MM-DD') : ''
    }

    if (dataForm.value.id) {
      const res = await perceptionUpdate(tempParams)
      if (res) {
        message.success(t('common.updateSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
      }
    } else {
      const res = await perceptionCreate(tempParams)
      if (res) {
        message.success(t('common.createSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
      }
    }
  } finally {
    setTimeout(() => {
      formLoading.value = false
    }, 500)
  }
}



/** 重置表单 */
const resetForm = () => {
  dataForm.value = {
    id: undefined,
    idCard: '',
    contractNumber: '', //档案号
    elderName: '',//长者姓名
    bedName: '', //床位号
    elderAge: '', //年龄
    elderSex: '', //性别
    checkInTime: '', //入院日期
    elderId: '',
    tenantId: undefined
  }
  formRef.value?.resetFields()

  // 重置感知觉与沟通评估表表单
  resetPerceptionForm()
}

// 关闭表单
const handleClosed = () => {
  dialogVisible.value = false
  resetForm()
}

/** 导出PDF */
const handleExport = () => {
  // 创建打印窗口
  const printWindow = window.open('', '_blank')
  if (!printWindow) {
    message.error('请允许弹出窗口')
    return
  }

  // 辅助函数：获取视力评分选项
  const getVisionMark = (score) => {
    const marks = [
      { score: 0, text: '能看清书报上的标准字体（0分）' },
      { score: 1, text: '能看清楚大字体（1分）' },
      { score: 2, text: '视力有限，能辨认物体（2分）' },
      { score: 3, text: '辨认物体有困难（3分）' },
      { score: 4, text: '没有视力（4分）' }
    ]
    return marks.map(m => `${m.score === score ? '☑' : '☐'} ${m.text}`).join('<br>')
  }

  // 构建打印内容
  const printContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>感知觉与沟通评估表 - ${dataForm.value.elderName || ''}</title>
      <style>
        @media print {
          @page { size: A4 portrait; margin: 15mm; }
        }
        body {
          font-family: 'SimSun', 'Microsoft YaHei', serif;
          font-size: 11pt;
          line-height: 1.5;
          color: #333;
        }
        .header {
          text-align: center;
          margin-bottom: 20px;
          border-bottom: 2px solid #333;
          padding-bottom: 10px;
        }
        .header h1 {
          font-size: 18pt;
          margin: 0;
          letter-spacing: 2px;
        }
        .info-section {
          margin-bottom: 15px;
          padding: 10px;
          border: 1px solid #999;
          background: #fafafa;
        }
        .info-row {
          display: flex;
          flex-wrap: wrap;
          gap: 20px;
        }
        .info-item {
          display: flex;
          align-items: center;
        }
        .info-item .label {
          font-weight: bold;
          margin-right: 8px;
          color: #555;
        }
        .info-item .value {
          border-bottom: 1px solid #333;
          min-width: 80px;
          padding: 0 5px;
          text-align: center;
        }
        .score-summary {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 10px 15px;
          background: #f0f0f0;
          border: 2px solid #333;
          margin-bottom: 15px;
        }
        .total-score {
          font-size: 14pt;
          font-weight: bold;
        }
        .total-score .score-value {
          color: #d9534f;
          font-size: 18pt;
        }
        .risk-level {
          font-size: 12pt;
          font-weight: bold;
          padding: 5px 15px;
        }
        .risk-none { color: #5cb85c; }
        .risk-low { color: #f0ad4e; }
        .risk-medium { color: #d9534f; }
        .risk-high { color: #d9534f; background: #ffe6e6; padding: 5px 10px; border-radius: 3px; }

        .assessment-table {
          width: 100%;
          border-collapse: collapse;
          margin-bottom: 15px;
        }
        .assessment-table th, .assessment-table td {
          border: 1px solid #333;
          padding: 10px;
          text-align: left;
        }
        .assessment-table th {
          background: #e8e8e8;
          font-weight: bold;
          text-align: center;
        }
        .assessment-table .criteria-col {
          width: 60%;
        }
        .assessment-table .score-col {
          width: 15%;
          text-align: center;
        }
        .assessment-table .select-col {
          width: 25%;
          text-align: center;
        }
        .assessment-table .criteria {
          text-align: left;
        }
        .assessment-table .score {
          text-align: center;
          font-weight: bold;
        }

        .remark-section {
          margin: 15px 0;
          padding: 10px;
          border: 1px solid #999;
          background: #fafafa;
        }
        .remark-label {
          font-weight: bold;
        }

        .risk-judgment-section {
          margin: 15px 0;
          padding: 10px;
          border: 1px solid #999;
        }
        .risk-judgment-title {
          font-weight: bold;
          margin-bottom: 10px;
        }
        .risk-options {
          display: flex;
          flex-wrap: wrap;
          gap: 20px;
        }
        .risk-option {
          display: inline-block;
        }

        .measures-section {
          margin: 15px 0;
          border: 1px solid #999;
        }
        .measures-title {
          font-weight: bold;
          background: #e8e8e8;
          padding: 8px 12px;
          border-bottom: 1px solid #999;
        }
        .measures-content {
          padding: 10px 12px;
        }
        .measure-item {
          margin-bottom: 8px;
          font-size: 10pt;
        }
        .measure-other {
          margin-left: 10px;
          border-bottom: 1px solid #333;
          min-width: 200px;
          display: inline-block;
        }

        .signature-section {
          margin-top: 30px;
          padding: 15px;
          border: 1px solid #999;
        }
        .signature-row {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        .signature-item {
          display: flex;
          align-items: center;
          gap: 10px;
        }
        .signature-label {
          font-weight: bold;
        }
        .signature-value {
          border-bottom: 1px solid #333;
          min-width: 150px;
          height: 25px;
        }

        .note-section {
          margin-top: 20px;
          padding: 15px;
          border: 1px solid #999;
          background: #fafafa;
        }
        .note-title {
          font-weight: bold;
          margin-bottom: 10px;
        }
        .note-content {
          font-size: 10pt;
          line-height: 1.8;
        }
        .note-content p {
          margin: 5px 0;
        }
        .note-content .indent {
          margin-left: 20px;
        }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>感知觉与沟通评估表</h1>
      </div>

      <div class="info-section">
        <div class="info-row">
          <div class="info-item">
            <span class="label">长者姓名：</span>
            <span class="value">${dataForm.value.elderName || ''}</span>
          </div>
          <div class="info-item">
            <span class="label">档案号：</span>
            <span class="value">${dataForm.value.contractNumber || ''}</span>
          </div>
          <div class="info-item">
            <span class="label">床位号：</span>
            <span class="value">${dataForm.value.bedName || ''}</span>
          </div>
        </div>
        <div class="info-row" style="margin-top: 10px;">
          <div class="info-item">
            <span class="label">评估日期：</span>
            <span class="value">${form.assessDate ? dayjs(form.assessDate).format('YYYY-MM-DD') : ''}</span>
          </div>
          <div class="info-item">
            <span class="label">评估人：</span>
            <span class="value">${form.assessor || ''}</span>
          </div>
        </div>
      </div>

      <div class="score-summary">
        <div class="total-score">
          总得分：<span class="score-value">${totalScore.value}</span> / 4分
        </div>
      </div>

      <!-- 评估表格 -->
      <table class="assessment-table">
        <thead>
          <tr>
            <th class="criteria-col">评估准则</th>
            <th class="score-col">分值</th>
            <th class="select-col">选择</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="criteria">能看清书报上的标准字体</td>
            <td class="score">0分</td>
            <td class="select">${form.visionScore === 0 ? '☑' : '☐'}</td>
          </tr>
          <tr>
            <td class="criteria">能看清楚大字体；但看不清书报上的标准字体</td>
            <td class="score">1分</td>
            <td class="select">${form.visionScore === 1 ? '☑' : '☐'}</td>
          </tr>
          <tr>
            <td class="criteria">视力有限；看不清报纸大标题；但能辨认物体</td>
            <td class="score">2分</td>
            <td class="select">${form.visionScore === 2 ? '☑' : '☐'}</td>
          </tr>
          <tr>
            <td class="criteria">辨认物体有困难；但眼睛能跟随物体移动；只能看到光、颜色和形状</td>
            <td class="score">3分</td>
            <td class="select">${form.visionScore === 3 ? '☑' : '☐'}</td>
          </tr>
          <tr>
            <td class="criteria">没有视力；眼睛不能跟随物体移动</td>
            <td class="score">4分</td>
            <td class="select">${form.visionScore === 4 ? '☑' : '☐'}</td>
          </tr>
        </tbody>
      </table>

      <!-- 备注 -->
      <div class="remark-section">
        <span class="remark-label">备注：</span>
        <span>若平日戴老花镜或近视镜，应在佩戴眼镜的情况下评估。</span>
      </div>

      <!-- 风险程度判断 -->
      <div class="risk-judgment-section">
        <div class="risk-judgment-title">风险程度判断：</div>
        <div class="risk-options">
          <span class="risk-option">${form.riskLevel === 'none' ? '☑' : '☐'} 无风险：0分</span>
          <span class="risk-option">${form.riskLevel === 'low' ? '☑' : '☐'} 低风险：1分</span>
          <span class="risk-option">${form.riskLevel === 'medium' ? '☑' : '☐'} 中风险：2分</span>
          <span class="risk-option">${form.riskLevel === 'high' ? '☑' : '☐'} 高风险：≥3分</span>
        </div>
      </div>

      <!-- 预防措施 -->
      <div class="measures-section">
        <div class="measures-title">预防措施</div>
        <div class="measures-content">
          <div class="measure-item">${form.preventiveMeasures?.includes('patrol') ? '☑' : '☐'} 加强巡查</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('food') ? '☑' : '☐'} 合理食物</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('expression') ? '☑' : '☐'} 观察表情</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('medication') ? '☑' : '☐'} 药物统一管控</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('feeding') ? '☑' : '☐'} 严格协助进食操作规程</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('card') ? '☑' : '☐'} 设立床头卡标识</div>
          <div class="measure-item">
            ${form.preventiveMeasures?.includes('other') ? '☑' : '☐'} 其他
            ${form.preventiveMeasures?.includes('other') ? '<span class="measure-other">' + (form.preventiveMeasuresOther || '') + '</span>' : ''}
          </div>
        </div>
      </div>

      <!-- 签名区域 -->
      <div class="signature-section">
        <div class="signature-row">
          <div class="signature-item">
            <span class="signature-label">家属/监护人签名：</span>
            <span class="signature-value">${form.familySignature || ''}</span>
          </div>
        </div>
      </div>

      <!-- 说明 -->
      <div class="note-section">
        <div class="note-title">说明：</div>
        <div class="note-content">
          <p>1. 新入住长者应在长者入住24小时内完成首次评估；</p>
          <p>2. 定期评估：</p>
          <p class="indent">（1）认知照护专区：高风险1个月评估一次，中风险、低风险3个月评估一次；</p>
          <p class="indent">（2）非认知照护专区：高风险1个月评估一次，中风险3个月评估一次，低风险6个月评估一次；</p>
          <p>3. 当长者身体发生变化者，应及时进行动态评估。</p>
        </div>
      </div>
    </body>
    </html>
  `

  // 写入内容并打印
  printWindow.document.write(printContent)
  printWindow.document.close()

  // 延迟打印，确保样式加载完成
  setTimeout(() => {
    printWindow.print()
  }, 500)
}

</script>

<style scoped lang="scss">
.mmse-form {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;

  .form-title {
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 20px;
  }



  .info-row {
    display: flex;
    gap: 40px;
    width: 100%;
    flex-direction: row;
    align-items: center;
    margin-bottom: 10px;

    .info-item {
      display: flex;
      align-items: center;
      gap: 8px;

      .label {
        width: 82px;
        text-align: right;
        margin-right: 4px;
        white-space: nowrap;
      }
    }
  }


  .form-body {
    border: 1px solid #333;
    padding: 15px;
  }

  .section {
    margin-bottom: 20px;

    &:last-child {
      margin-bottom: 0;
    }

    .section-title {
      font-weight: bold;
      margin-bottom: 10px;
    }
  }

  .form-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;

    .item-number {
      font-weight: bold;
      min-width: 25px;
    }

    .item-label {
      white-space: nowrap;
      min-width: fit-content;
    }

  }

  .other-input {
    width: 200px;
    margin-left: 10px;
  }

  // 调整 Element Plus 组件样式
  :deep(.el-input__inner) {
    height: 28px;
    line-height: 28px;
    border-top: none;
    border-left: none;
    border-right: none;
    border-radius: 0;
    padding: 0 4px;

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


  :deep(.el-checkbox__label),
  :deep(.el-radio__label) {
    padding-left: 4px;
  }
}
.row{
  margin-bottom: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  text{
    text-align: right;
    margin-right: 4px;
    width: 82px;
  }
}

// 感知觉与沟通评估表 特有样式
.total-score-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 20px;

  .total-score {
    display: flex;
    align-items: baseline;
    gap: 4px;

    .score-label {
      font-size: 16px;
      font-weight: bold;
    }

    .score-value {
      font-size: 28px;
      font-weight: bold;
      color: #409eff;
    }

    .score-max {
      font-size: 14px;
      color: #909399;
    }
  }

  .risk-level {
    font-size: 16px;
    font-weight: bold;
    padding: 8px 16px;
    border-radius: 4px;

    &.risk-none {
      color: #67c23a;
      background: #f0f9eb;
    }

    &.risk-low {
      color: #e6a23c;
      background: #fdf6ec;
    }

    &.risk-medium {
      color: #f56c6c;
      background: #fef0f0;
    }

    &.risk-high {
      color: #f56c6c;
      background: #fef0f0;
      border: 1px solid #f56c6c;
    }
  }
}

.assessment-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;

  table {
    width: 100%;
    border: 1px solid #333;

    th, td {
      border: 1px solid #333;
      padding: 12px;
      text-align: left;
    }

    th {
      background: #f5f7fa;
      font-weight: bold;
      text-align: center;
    }

    .criteria-col {
      width: 60%;
    }

    .score-col {
      width: 15%;
      text-align: center;
    }

    .select-col {
      width: 25%;
      text-align: center;
    }

    .criteria {
      text-align: left;
    }

    .score {
      text-align: center;
      font-weight: bold;
    }

    .select {
      text-align: center;

      :deep(.el-radio) {
        margin-right: 0;
      }
    }
  }
}

.remark-section {
  margin: 15px 0;
  padding: 12px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .remark-label {
    font-weight: bold;
    color: #606266;
  }

  .remark-text {
    color: #606266;
  }
}

.risk-judgment-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .risk-judgment-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .risk-options {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;

    :deep(.el-radio) {
      margin-right: 0;
    }
  }
}

.preventive-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .preventive-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .preventive-options {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;

    :deep(.el-checkbox) {
      margin-right: 20px;
      margin-bottom: 8px;
    }

    .other-input {
      width: 200px;
      margin-left: 10px;
    }
  }
}

.signature-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .signature-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
  }

  .signature-item {
    display: flex;
    align-items: center;
    gap: 10px;

    &.date-item {
      :deep(.el-date-picker) {
        width: 150px;
      }
    }
  }

  .signature-label {
    font-weight: bold;
    white-space: nowrap;
  }

  .signature-input {
    width: 150px;
  }

  .date-picker {
    width: 150px;
  }
}

.note-section {
  margin-top: 20px;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .note-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .note-content {
    font-size: 13px;
    line-height: 1.8;
    color: #606266;

    p {
      margin: 5px 0;
    }

    .indent {
      margin-left: 20px;
    }
  }
}
</style>
