<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="ngasr-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">分</span>
          </div>
        </div>

        <!-- 评估表格 -->
        <div class="assessment-table">
          <table>
            <thead>
              <tr>
                <th class="risk-factor-col">危险因素</th>
                <th class="score-yes-col">是</th>
                <th class="score-no-col">否</th>
                <th class="select-col">选择</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in riskFactors" :key="index">
                <td class="risk-factor">{{ item.name }}</td>
                <td class="score-yes">{{ item.yesScore }}分</td>
                <td class="score-no">0分</td>
                <td class="select">
                  <el-radio-group v-model="form.riskScores[index]" :disabled="isDetail">
                    <el-radio :value="item.yesScore" style="margin-right: 26px"> 是</el-radio>
                    <el-radio :value="0">  否</el-radio>
                  </el-radio-group>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

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

        <!-- 预防措施 -->
        <div class="preventive-section">
          <div class="preventive-title">预防措施：</div>
          <div class="preventive-options">
            <el-checkbox v-model="form.preventiveMeasures" value="patrol" :disabled="isDetail">
                加强巡视
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" value="handover" :disabled="isDetail">
                严格交接班
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" value="checkItems" :disabled="isDetail">
                检查有无危险物品
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" value="emotion" :disabled="isDetail">
                注重长者情绪行为
            </el-checkbox>
            <el-checkbox v-model="form.preventiveMeasures" value="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>
        </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 v-if="isDetail" type="success" @click="handleExport">打印</el-button>
      <el-button style="margin-left: 22px;margin-right: 30px" v-loading="formLoading" type="primary" v-show="!isDetail" @click="submitForm">确定</el-button>
    </template>

  </el-drawer>
</template>

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

import dayjs from 'dayjs'

import { ngasrCreate, ngasrGetById, ngasrUpdate, ngasrGetByElderId } 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
}

// ========== NGASR自杀风险评估量表 表单序列化方法 ==========

// 危险因素列表
const riskFactors = [
  { name: '绝望感', yesScore: 3 },
  { name: '近期负性生活事件', yesScore: 1 },
  { name: '被害妄想或有被害内容的幻听', yesScore: 1 },
  { name: '情绪低落/兴趣丧失或愉快感缺乏', yesScore: 3 },
  { name: '人际和社会功能退缩', yesScore: 1 },
  { name: '言语流露自杀意图', yesScore: 1 },
  { name: '计划采取自杀行动', yesScore: 3 },
  { name: '自杀家族史', yesScore: 1 },
  { name: '近亲人死亡或重要的亲密关系丧失', yesScore: 3 },
  { name: '精神病史', yesScore: 1 },
  { name: '鳏夫/寡妇', yesScore: 1 },
  { name: '自杀未遂史', yesScore: 3 },
  { name: '社会-经济地位低下', yesScore: 1 },
  { name: '饮酒史或酒精滥用', yesScore: 1 },
  { name: '罹患晚期疾病', yesScore: 1 }
]



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

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

/** 自动判断风险等级 */
const autoJudgeRiskLevel = () => {
  const score = totalScore.value
  if (score === 0) form.riskLevel = 'none'
  else if (score >= 1 && score <= 5) form.riskLevel = 'low'
  else if (score >= 6 && score <= 8) form.riskLevel = 'medium'
  else form.riskLevel = 'high'
}



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

    // 各危险因素得分
    riskScores: form.riskScores || [],

    // 总分
    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.riskScores = formData.riskScores || new Array(15).fill(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() : ''
}

/** 重置NGASR评估表表单数据 */
const resetNGASRForm = () => {
  form.assessor = ''
  form.assessDate = ''

  // 各危险因素得分（15个因素，初始为0）
  form.riskScores = new Array(15).fill(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 loadNGASRData(id)
  } else {
    title.value = "新增-自杀风险评估"

  }
}

/** 加载评估数据 */
const loadNGASRData = async (id: number) => {
  try {
    const res = await ngasrGetById(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 loadNGASRByElderId = async (elderId: number) => {
  try {
    const res = await ngasrGetByElderId(elderId)
    if (res && res.assessData) {
      const formData = JSON.parse(res.assessData)
      deserializeFormData(formData)
    }
  } catch (error) {
    // 无历史数据，不处理
  }
}

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

  // 各危险因素得分（15个因素）
  riskScores: new Array(15).fill(0),

  // 风险程度
  riskLevel: '',

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

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

/** 计算总分 */
const totalScore = computed(() => {
  if (!form.riskScores || form.riskScores.length === 0) return 0
  return form.riskScores.reduce((sum, score) => sum + (score || 0), 0)
})

// 监听总分变化，自动判断风险等级
watch(totalScore, () => {
  autoJudgeRiskLevel()
})


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 ngasrUpdate(tempParams)
      if (res) {
        message.success(t('common.updateSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
      }
    } else {
      const res = await ngasrCreate(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()

  // 重置NGASR评估表表单
  resetNGASRForm()
}

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

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

  // 构建危险因素表格行
  const riskFactorRows = riskFactors.map((item, index) => {
    const score = form.riskScores[index] || 0
    return `
      <tr>
        <td class="risk-factor">${item.name}</td>
        <td class="score-yes">${item.yesScore}分</td>
        <td class="score-no">0分</td>
        <td class="select">${score === item.yesScore ? '☑ 是' : '☐ 是'} / ${score === 0 ? '☑ 否' : '☐ 否'}</td>
      </tr>
    `
  }).join('')

  // 构建打印内容
  const printContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>自杀风险评估量表(NGASR) - ${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: 8px;
          text-align: left;
        }
        .assessment-table th {
          background: #e8e8e8;
          font-weight: bold;
          text-align: center;
        }
        .assessment-table .risk-factor-col {
          width: 45%;
        }
        .assessment-table .score-yes-col,
        .assessment-table .score-no-col {
          width: 12%;
          text-align: center;
        }
        .assessment-table .select-col {
          width: 31%;
          text-align: center;
        }
        .assessment-table .risk-factor {
          text-align: left;
        }
        .assessment-table .score-yes,
        .assessment-table .score-no {
          text-align: center;
        }

        .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: 15px;
        }
        .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>自杀风险评估量表(NGASR)</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> 分
        </div>

      </div>

      <!-- 评估表格 -->
      <table class="assessment-table">
        <thead>
          <tr>
            <th class="risk-factor-col">危险因素</th>
            <th class="score-yes-col">是</th>
            <th class="score-no-col">否</th>
            <th class="select-col">选择</th>
          </tr>
        </thead>
        <tbody>
          ${riskFactorRows}
        </tbody>
      </table>

      <!-- 风险程度判断 -->
      <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-5分</span>
          <span class="risk-option">${form.riskLevel === 'medium' ? '☑' : '☐'} 中风险：6-8分</span>
          <span class="risk-option">${form.riskLevel === 'high' ? '☑' : '☐'} 高风险：≥9分</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('handover') ? '☑' : '☐'} 严格交接班</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('checkItems') ? '☑' : '☐'} 检查有无危险物品</div>
          <div class="measure-item">${form.preventiveMeasures?.includes('emotion') ? '☑' : '☐'} 注重长者情绪行为</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">

.form-title {
  text-align: center;
  font-size: 20px;
  width: 100%;
  margin-bottom: 20px;
}

.mmse-form {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;





  .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;
  }

  // 调整 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;
  }
}

// NGASR自杀风险评估量表 特有样式
.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>
