<template>
  <!--  <div class="myArea"> -->
  <el-dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    :fullscreen="true"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
  >
    <div class="mycontainer">
      <div class="myleft-box">
        <el-container>
          <!--头部 患者信息-->
          <el-header style="background-color: rgb(79, 138, 240); height: 40px">
            <div style="font-size: 20px; color: aliceblue; margin-top: 5px">
              <span>{{ rowinfo.pname }}</span>
              <span style="margin-left: 10px"> {{ rowinfo.gender }}</span>
              <span style="margin-left: 10px"> {{ age + '岁' }}</span>
              <span style="margin-left: 10px"> 检查编号：{{ rowinfo.examId }}</span>
              <span style="margin-left: 10px">
                检查日期：{{ formatDate(rowinfo.examDate, 'YYYY-MM-DD') }}</span
              >
              <span style="margin-left: 10px">检查机构： {{ rowinfo.orgName }}</span>
            </div>
          </el-header>
          <!--头部 功能按钮-->
          <el-header style="height: 40px; padding: 0px">
            <el-button-group class="ml-4" style="margin-top: 4px; margin-left: 0px">
              <el-button type="primary" plain
                ><el-icon><Filter /></el-icon> 采集</el-button
              >
              <el-popover
                placement="bottom"
                title="分享二维码"
                :width="250"
                trigger="click"
                :disabled="isshare"
              >
                <template #reference>
                  <el-button type="primary" plain :icon="Share" @click="shareclick()"
                    >分享</el-button
                  >
                </template>
                <div>
                  <canvas ref="qrcodeRef" width="200" height="200"></canvas>
                </div>
              </el-popover>

              <el-button type="primary" plain>
                <el-icon><RefreshLeft /></el-icon> 重采</el-button
              >
              <el-button
                type="primary"
                plain
                :icon="Edit"
                @click="getuporghiorgid(Primarykey, orgid)"
                >申请诊断</el-button
              >
              <el-button type="primary" plain @click="print"
                ><el-icon><Printer /></el-icon>打印</el-button
              >
              <el-button type="primary" plain @click="ECGApplyDialog"
                ><el-icon><User /></el-icon>申请返修</el-button
              >
              <el-button type="primary" plain @click="openECGDialog" v-if="isshowwjz"
                ><el-icon><Warning /></el-icon>危急值</el-button
              >
              <el-button type="danger" plain @click="openreprotdiag" v-if="isshowysb"
                ><el-icon><Check /></el-icon>已上报</el-button
              >
            </el-button-group>
          </el-header>
          <el-container>
            <!--左侧 心电图区域-->
            <el-aside width="96%" style="height: 81vh">
              <el-tabs type="border-card" style="height: 99%">
                <el-tab-pane :label="snapshotTime">
                  <!--心电图-->
                  <ECGhtml
                    v-if="isChildVisible"
                    :jsonurl="queryParams.ecgJsonDataFilePath"
                    :-isgrid="Isgrid"
                    :-ismeasure="Ismeasure"
                    :-isfd="IsFD"
                    :lineratio="lineratio"
                    :suduratio="suduratio"
                    :isrefresh="isrefresh"
                    :iscorrect="correct"
                    @update:value="handleUpdate"
                  />
                </el-tab-pane>
              </el-tabs>
            </el-aside>
            <!--右侧 功能按钮-->
            <el-main style="padding: 1px">
              <div
                style="margin-top: 30px; display: flex; flex-direction: column; align-items: center"
              >
                <el-dropdown placement="bottom" trigger="click" @command="handlezsCommand">
                  <el-button
                    style="width: 30px; height: 30px; margin-bottom: 10px"
                    type="primary"
                    plain
                  >
                    <el-icon><Odometer /></el-icon>
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="0.2">5mm/s</el-dropdown-item>
                      <el-dropdown-item command="0.4">10mm/s</el-dropdown-item>
                      <el-dropdown-item command="0.5">12.5mm/s</el-dropdown-item>
                      <el-dropdown-item command="1">25mm/s</el-dropdown-item>
                      <el-dropdown-item command="2">50mm/s</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
                <span style="font-size: 15px; margin-bottom: 10px">走速</span>

                <el-dropdown placement="bottom" trigger="click" @command="handleCommand">
                  <el-button
                    style="width: 30px; height: 30px; margin-bottom: 10px"
                    type="primary"
                    plain
                  >
                    <el-icon><Histogram /></el-icon>
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="0.012">2.5mm/mv</el-dropdown-item>
                      <el-dropdown-item command="0.025">5mm/mv</el-dropdown-item>
                      <el-dropdown-item command="0.05">10mm/mv</el-dropdown-item>
                      <el-dropdown-item command="0.1">20mm/mv</el-dropdown-item>
                      <el-dropdown-item command="0.2">40mm/mv</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
                <span style="font-size: 15px; margin-bottom: 10px">振幅</span>

                <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="measure()"
                >
                  <el-icon><EditPen /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">测量</span>

                <!-- <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="FD()"
                >
                  <el-icon><ZoomIn /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">放大</span> -->

                <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="iscorrect()"
                >
                  <el-icon><Switch /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">纠错</span>

                <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="Isgriddisplay()"
                >
                  <el-icon><Grid /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">网格</span>

                <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="Refresh"
                >
                  <el-icon><RefreshRight /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">刷新</span>

                <el-button
                  style="width: 30px; height: 30px; margin-bottom: 10px"
                  type="primary"
                  plain
                  @click="opencomparediag"
                >
                  <el-icon><Reading /></el-icon>
                </el-button>
                <span style="font-size: 15px; margin-bottom: 10px">对比</span>
              </div>
            </el-main>
          </el-container>
        </el-container>
      </div>
      <div class="myright-box">
        <el-form
          :model="queryParams"
          label-width="auto"
          id="elform"
          style="max-width: 600px; margin-top: 10px; margin-left: 10px; height: 89vh"
        >
          <!-- 第一行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="心率">
                <el-input v-model="queryParams.hr" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>bpm</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="P电轴">
                <el-input
                  v-model="queryParams.paxle"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>°</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第二行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="QRS电轴">
                <el-input
                  v-model="queryParams.qrsAxle"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>°</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="T电轴" s>
                <el-input
                  v-model="queryParams.taxle"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>°</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第三行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="P波时限">
                <el-input
                  v-model="queryParams.ptimeLimit"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>ms</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="PR间期">
                <el-input v-model="queryParams.pr" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>ms</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第四行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="QRS时限">
                <el-input
                  v-model="queryParams.qrsTimeLimit"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>ms</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="QT间期">
                <el-input v-model="queryParams.qt" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>ms</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第五行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="QTc">
                <el-input v-model="queryParams.qtc" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>ms</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="RV5">
                <el-input v-model="queryParams.rv5" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>mV</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第六行 -->
          <el-row :gutter="24" style="margin-right: 1px; height: 35px">
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="SV1">
                <el-input v-model="queryParams.sv1" suffix-icon="el-icon-date" style="width: 150px">
                  <template #append>mV</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="padding: 1px">
              <el-form-item label="RV5+SV1">
                <el-input
                  v-model="queryParams.rv5Sv1"
                  suffix-icon="el-icon-date"
                  style="width: 150px"
                >
                  <template #append>mV</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-tag type="info" style="font-size: 16px; width: 100%; color: black"
            >心电事件快照</el-tag
          >
          <el-input
            v-model="snapshotTime"
            style="width: 100%"
            :rows="4"
            type="textarea"
            placeholder=""
          />
          <el-tag type="info" style="font-size: 16px; width: 100%; color: black">智能词库</el-tag>
          <el-input
            v-model="queryParams.autoDiagResult"
            style="width: 100%"
            :rows="4"
            type="textarea"
            placeholder=""
          />
          <el-tag type="info" style="font-size: 16px; width: 100%; color: black"
            >医生诊断结论 <el-button @click="zdmodle" type="primary" text>诊断模版</el-button>
          </el-tag>
          <el-input
            class="your-component"
            v-model="queryParams.doctorDiagResult"
            style="width: 100%"
            type="textarea"
            placeholder=""
            ref="inputRef"
            :rows="eltextrow"
          />
          <div class="mybtnsty">
            <div>
              <el-button class="mybutwidth" type="warning" @click="insertValue('I')">I</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('II')">II</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('III')"
                >III</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('aVR')"
                >aVR</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('aVL')"
                >aVL</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('aVF')"
                >aVF</el-button
              >
            </div>
            <div>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V3R')"
                >V3R</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('V4R')"
                >V4R</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('V5R')"
                >V5R</el-button
              >
              <el-button class="mybutwidth" type="warning" @click="insertValue('V1')">V1</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V2')">V2</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V3')">V3</el-button>
            </div>
            <div>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V4')">V4</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V5')">V5</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V6')">V6</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V7')">V7</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V8')">V8</el-button>
              <el-button class="mybutwidth" type="warning" @click="insertValue('V9')">V9</el-button>
            </div>
          </div>
          <div class="mycenter-button">
            <el-button
              style="width: 200px"
              type="primary"
              @click="getuporghiorgid(Primarykey, orgid)"
              >申请诊断</el-button
            >
            <el-button style="width: 200px" type="primary" @click="save">保存</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </el-dialog>
  <!-- </div> -->
  <!--抽屉 用于展示诊断模版-->
  <el-drawer v-model="drawer" :with-header="false" :modal="true" size="20%">
    <span>诊断数据模版</span>
    <el-tree
      style="padding-top: 15px; padding-bottom: 20px; min-width: 195px; font-size: 20px"
      class="treeStyle"
      :check-on-click-node="true"
      :highlight-current="true"
      :default-expand-all="true"
      :data="treeData"
      :props="treeDefaultProps"
      node-key="id"
      :show-checkbox="false"
      :check-strictly="true"
      @check="handleTreeNodeClick"
      ref="selectTree"
      :expand-on-click-node="false"
    />
  </el-drawer>
  <!--打印弹窗(老)-->
  <el-dialog
    title=""
    v-if="false"
    v-model="isdiagshow"
    :fullscreen="true"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    @close="printclose"
  >
    <!--width: 1000px; height: 700px;-->
    <ECGprint :imagebase64="imagebase64" />
  </el-dialog>
  <div style="position: fixed; top: 0; left: 0; z-index: -100">
    <div id="hiddenPdfDiv" v-if="false && isprintimage">
      <!--心电图-->
      <ECGhtmlprint
        style="height: 1080px; width: 1920px; page-break-after: always; visibility: hidden"
        :extraInfo="queryParams"
        :jsonurl="queryParams.ecgJsonDataFilePath"
        :-isgrid="Isgrid"
        :-ismeasure="Ismeasure"
        :-isfd="IsFD"
        :lineratio="0.025"
        :suduratio="suduratio"
        :isrefresh="isrefresh"
        :iscorrect="correct"
        @update:value="handleUpdate"
        :printimage="isprintimage"
        @update:image="updateimagebase"
        :age="age"
        :billDoctorDepartment="rowinfo.billDoctorDepartment"
        :doctorname="rowinfo.doctorname"
        :regId="id"
        :pname="rowinfo.pname"
        :gender="rowinfo.gender"
      />
    </div>
  </div>
  <!--打印弹窗(新)-->
  <el-dialog
    v-model="isdiagshow"
    :fullscreen="true"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    @close="printclose"
  >
    <template #title>
      <div
        style="
          margin-bottom: -18px;
          text-align: center;
          font-family: Arial, sans-serif;
          color: #6e6666;
          font-size: 22px;
        "
      >
        打印预览
      </div>
    </template>
    <div>
      <div style="text-align: right">
        <el-button type="primary" style="width: 90px" v-print="{ id: 'printMe_ecg' }">
          <span>打印</span>
        </el-button>
      </div>
      <el-divider style="margin-top: 8px; margin-bottom: 10px" />
    </div>
    <div id="printMe_ecg">
      <div class="page-item-ecg">
        <ReportInfoECG
          style="height: 99.888%"
          :extraInfo="queryParams"
          :jsonurl="queryParams.ecgJsonDataFilePath"
          :lineratio="0.025"
          :suduratio="suduratio"
          :age="age"
          :billDoctorDepartment="rowinfo.billDoctorDepartment"
          :doctorname="rowinfo.doctorname"
          :regId="id"
          :orgName="rowinfo.orgName"
          :pname="rowinfo.pname"
          :gender="rowinfo.gender"
        />
      </div>
    </div>
  </el-dialog>
  <div style="position: fixed; top: 0; left: 0; z-index: -1100">
    <!-- html2canvas处理时，宽/高不确定可能导致异常效果，可以增加相应宽/高，或者固定宽高尺寸 -->
    <div v-if="isprintimage" id="ecg_pdf" class="page-pdf-ecg">
      <ReportInfoECG
        style="height: 99.888%"
        :isBackend="true"
        :createPdf="updateimagebase_new"
        :extraInfo="queryParams"
        :jsonurl="queryParams.ecgJsonDataFilePath"
        :lineratio="0.025"
        :suduratio="suduratio"
        :age="age"
        :billDoctorDepartment="rowinfo.billDoctorDepartment"
        :doctorname="rowinfo.doctorname"
        :regId="id"
        :orgName="rowinfo.orgName"
        :pname="rowinfo.pname"
        :gender="rowinfo.gender"
      />
    </div>
  </div>
  <ECGWarningDialog ref="ECGDialog" />
  <ECGReport ref="ECGReportDialog" />
  <ECGCopmareDialog ref="ECGCompare" />
  <ECGApplyforRepair ref="ECGApply"/>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ArrowLeft, ArrowRight, Delete, Edit, Share, Check } from '@element-plus/icons-vue'
import ECGhtml from '@/views/ECG/ECGhtml.vue'
import ECGhtmlprint from '@/views/ECG/ECGhtmlprint.vue'
import { EcganalysisparasApi, EcganalysisparasVO } from '@/api/tblist/ecganalysisparas'
import { formatDate } from '@/utils/formatTime'
import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
import { ultrasoniccomApi, updateexamineimageVO } from '@/api/ultrasoniccom'
import { PatientexamlistApi, PatientexamlistVO } from '@/api/tblist/patientexamlist'
import QRCode from 'qrcode'
import ECGprint from '@/views/ECG/ECGprint.vue'
import ReportInfoECG from '@/views/applyregistration/reportPrintStatistics/ReportInfoECG.vue'
import htmlToPdf from '@/utils/htmlPdf'
import ECGWarningDialog from '@/views/ECG/ECGWaring/ECGWarningDialog.vue'
import ECGReport from '@/views/ECG/ECGWaring/ECGReport.vue'
import { WarningApi, WarningVO } from '@/api/system/warning'
import ECGCopmareDialog from '@/views/ECG/ECGCompare.vue'
import ECGApplyforRepair from '@/views/ECG/ECGModify/ECGApplyforRepair.vue'
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const id = ref() //患者ID
const orgid = ref() //机构编码
const Primarykey = ref() //主键ID
const rowinfo = ref() //当前患者数据行
const snapshotTime = ref() //心电事件快照事件
// 定义一个响应式变量来控制子组件的显示
const isChildVisible = ref(false)
const inputRef = ref() // 用于获取el-input的DOM引用
const saveFormVO = ref<EcganalysisparasVO>({} as EcganalysisparasVO)
const Profilevo = ref<ProfileVO>({} as ProfileVO) //当前登录人信息
const age = ref() //年龄
const Isgrid = ref(1) //是否显示网格 1显示  0不显示
const Ismeasure = ref(0) //是否开启测量 1开启 0 不开启
const IsFD = ref(false) //是否开启放大
const lineratio = ref(0.05) //画线的系数 振幅
const suduratio = ref(1) //画线的系数 走速
const drawer = ref(false) //是否显示抽屉
const diagnosemodel = ref() //存放诊断模版
const isrefresh = ref(false) //是否刷新
const correct = ref(false) //纠错功能
const eltextrow = ref(4) //诊断内容占据的行
const isshare = ref(true) //是否展示分享二维码
const qrcodeRef = ref(null)
const isdiagshow = ref(false) //是否显示打印窗体
const imagebase64 = ref('')
const isprintimage = ref(false) //是否生成打印文件base64
const ECGDialog = ref() //危急值消息弹窗
const isshowwjz = ref(false)
const isshowysb = ref(false)
const ECGReportDialog = ref() //危急值消息弹窗
const ECGCompare = ref() //心电图对比弹窗
const ECGApply = ref() //申请返修弹窗
// 树配置项
const treeDefaultProps = {
  children: 'children',
  label: 'tempname'
}
// 通用树数据
const treeData = ref([])

/** 打开弹窗 */
const open = async (row: any) => {
  dialogVisible.value = true
  dialogTitle.value = '心电分析'
  resetForm()
  id.value = row.regId
  Primarykey.value = row.id
  orgid.value = row.orgId
  rowinfo.value = row
  // console.log( rowinfo.value)
  const data = await EcganalysisparasApi.getexamIDdata('MZCF0191729074962197_44', orgid.value)
  queryParams.value = data
  snapshotTime.value = formattedDate(queryParams.value.snapshotTime)
  await getlogininfo()
  calculateAge(row.birthday)
  Isgrid.value = 1 // 没次打开都是显示的 只有点击网格才会变化
  isChildVisible.value = true

  nextTick(() => {
    /* 计算右侧诊断*/
    const canvasContainer = document.getElementById('elform')
    if (canvasContainer) {
      let sl = 300
      const heightInPixels = canvasContainer.offsetHeight
      if (heightInPixels > 1000) {
        sl = -100
      }
      const divisor = 60 // 假设你想除以的数是2
      const result = (heightInPixels - sl) / divisor
      const integer = Math.round(result)
      eltextrow.value = integer
    }
  })
  //查询当前患者的危急值相关信息
  const warningdata = await WarningApi.getexmidororgiddata(row.examId, row.orgId)
  //判断是否显示危急值上报功能 条件是当前机构等于上级机构
  isshowwjz.value = Profilevo.value.orgId === row.highLevelOrgId ? true : false
  if (warningdata.reportDate) {
    isshowysb.value = Profilevo.value.orgId === row.orgId ? true : false
  }
}

// 返回的列表的数据
const queryParams = ref({
  id: '',
  orgId: '',
  examId: '',
  collectionTime: undefined,
  hr: '',
  paxle: '',
  qrsAxle: '',
  taxle: '',
  ptimeLimit: '',
  pr: '',
  qrsTimeLimit: '',
  qt: '',
  qtc: '',
  rv5: '',
  sv1: '',
  rv5Sv1: '',
  snapshotTime: undefined,
  autoDiagResult: '',
  autoDiagTime: undefined,
  doctorDiagResult: '',
  doctorDiagTime: undefined,
  doctorName: '',
  doctorId: '',
  departId: '',
  departName: '',
  isDelete: '',
  deleteTime: undefined,
  deleteDoctorName: '',
  deleteDoctorId: '',
  ecgDataFilePath: '',
  ecgJsonDataFilePath: '',
  createDate: undefined
})
//
async function save() {
  saveFormVO.value.id = queryParams.value.id
  saveFormVO.value.hr = queryParams.value.hr
  saveFormVO.value.pAxle = queryParams.value.paxle
  saveFormVO.value.qrsAxle = queryParams.value.qrsAxle
  saveFormVO.value.tAxle = queryParams.value.taxle
  saveFormVO.value.qrsTimeLimit = queryParams.value.ptimeLimit
  saveFormVO.value.pr = queryParams.value.pr
  saveFormVO.value.qrsTimeLimit = queryParams.value.qrsTimeLimit
  saveFormVO.value.qt = queryParams.value.qt
  saveFormVO.value.qtc = queryParams.value.qtc
  saveFormVO.value.rv5 = queryParams.value.rv5
  saveFormVO.value.sv1 = queryParams.value.sv1
  saveFormVO.value.rv5Sv1 = queryParams.value.rv5Sv1
  saveFormVO.value.autoDiagResult = queryParams.value.autoDiagResult
  saveFormVO.value.doctorDiagResult = queryParams.value.doctorDiagResult
  saveFormVO.value.doctorName = Profilevo.value.doctorname
  saveFormVO.value.doctorId = Profilevo.value.doctorID
  // saveFormVO.value.departId=queryParams.value.departId
  // saveFormVO.value.departName=queryParams.value.departName
  const ret = await EcganalysisparasApi.SaveEcganalysisparas(saveFormVO.value)
  if (ret) {
    //进行生成pdf
    const data = await EcganalysisparasApi.getexamIDdata('MZCF0191729074962197_44', orgid.value)
    queryParams.value = data
    message.alertSuccess('保存成功')

    //ElMessage.info('开始保存pdf')
    setTimeout(() => {
      isprintimage.value = true
    }, 900)
  }
}

//展示图片
function updateimagebase(newValue) {
  imagebase64.value = newValue
}

const updateimagebase_new = async () => {
  try {
    const image_base64 = await htmlToPdf.getDivContentAsBase64('#ecg_pdf')
    try {
      //生成PDF
      htmlToPdf
        .createPdfFromBase64(image_base64)
        .then((pdfBase64) => {
          PatientexamlistApi.ftppdf({
            id: Primarykey.value,
            imagebase: pdfBase64,
            model: '0',
            folderPath: 'C:\\work\\pacs' + '\\' + Primarykey.value
          })
          ElMessage.info('pdf保存成功')
        })
        .catch((error) => {
          console.log(error)
        })
    } finally {
    }
  } catch {
  } finally {
    nextTick(() => {
      isprintimage.value = false
    })
  }
}

//重置数据
function resetForm() {
  Isgrid.value = 1 //是否显示网格 1显示  0不显示
  Ismeasure.value = 0 //是否开启测量 1开启 0 不开启
  IsFD.value = false //是否开启放大
  lineratio.value = 0.05 //画线的系数 振幅
  suduratio.value = 1 //画线的系数 走速
}

//关闭打印窗口
function printclose() {
  isprintimage.value = false
  imagebase64.value = ''
  isdiagshow.value = false
}
//打印
function print() {
  if (!queryParams.value.autoDiagTime) {
    isdiagshow.value = false
    //isprintimage.value = false
    message.alertError('请保存后再进行打印')
  } else {
    isdiagshow.value = true
    //isprintimage.value = true
  }
}
//纠错窗体回传值
const handleUpdate = (newValue) => {
  correct.value = newValue
}
//申请返修界面弹窗
function ECGApplyDialog() {
  ECGApply.value.opendiag('1',rowinfo.value,Profilevo.value.orgId,Profilevo.value.doctorname)
}
//危急值弹窗
function openECGDialog() {
  ECGDialog.value.opendiag('1', rowinfo.value)
}
//已上报危急值弹窗
function openreprotdiag() {
  ECGReportDialog.value.opendiag(rowinfo.value)
}
//对比弹窗
function opencomparediag() {
  ECGCompare.value.opencomparediag(rowinfo.value, queryParams.value.snapshotTime)
}
//纠错
function iscorrect() {
  correct.value = correct.value ? false : true
}

//刷新
function Refresh() {
  isrefresh.value = isrefresh.value ? false : true
}

/** 申请后更新上级机构字段*/
const getuporghiorgid = async (id: number, orgId: string) => {
  try {
    if (rowinfo.value.highLevelOrgId) {
      message.alertWarning('已提交申请')
      return
    }
    await message.confirm('是否要进行申请？', '确认提示')
    const data = await PatientexamlistApi.getuporghiid(id, orgId)
    if (data == '申请成功') {
      message.success('申请成功')
    } else {
      await message.info(data)
    }
  } catch {}
}

// 树节点选中事件
const handleTreeNodeClick = async (data) => {
  if (data.dataType === '0') {
    const confirmed = await message.delConfirm('是否选择当前诊断', '确认')
    if (confirmed) {
      diagnosemodel.value = data.diagResults
      insertValue(diagnosemodel.value)
      drawer.value = false
      console.log(diagnosemodel.value)
    }
  }
}

//诊断模版
async function zdmodle() {
  const dd = await ultrasoniccomApi.getreporttemplatelist('', '', '')
  treeData.value = dd
  drawer.value = drawer.value ? false : true
}

//获取振幅
function handleCommand(value) {
  lineratio.value = value
}
//走苏
function handlezsCommand(value) {
  suduratio.value = value
}

//是否显示网格
function Isgriddisplay() {
  const value = Isgrid.value === 1 ? 0 : 1
  Isgrid.value = value
}
//是否开启 测量
function measure() {
  const value = Ismeasure.value === 1 ? 0 : 1
  Ismeasure.value = value
  if (value === 1) {
    message.alertSuccess('开启测量')
  } else {
    message.alertSuccess('关闭测量')
  }
}
//是否开启 放大
function FD() {
  const value = IsFD.value === false ? true : false
  IsFD.value = value
  if (value) {
    message.alertSuccess('开启放大')
  } else {
    message.alertSuccess('关闭放大')
  }
}
//分享点击
function shareclick() {
  if (rowinfo.value.pdfurl) {
    isshare.value = false
    // 确保 DOM 已经渲染
    QRCode.toCanvas(qrcodeRef.value, rowinfo.value.pdfurl, function (error) {
      if (error) console.error(error)
      console.log('QR code generated!')
    })
  } else {
    isshare.value = true
    message.alertError('请保存后再进行分享')
  }
}
//根据光标位置 插入值
const insertValue = (textvalue: String) => {
  const textarea = inputRef.value.$el.querySelector('textarea') // 获取textarea DOM元素
  const start = textarea.selectionStart // 光标位置
  const end = textarea.selectionEnd
  const value = textarea.value // 当前输入框的值
  queryParams.value.doctorDiagResult = value.slice(0, start) + textvalue + value.slice(end)
  // 更新光标位置
  textarea.selectionStart = textarea.selectionEnd = start + textvalue.length
  //textarea.focus() // 使输入框重新获得焦点
}
// 创建一个计算属性来转换时间戳
function formattedDate(timestamp) {
  const date = new Date(timestamp)
  const strdata = formatDate(date, 'YYYY-MM-DD HH:mm:ss')
  return strdata // 转换为本地日期时间字符串
}

//获取当前登录人信息
const getlogininfo = async () => {
  Profilevo.value = await getUserProfile()
}

// 计算年龄

const calculateAge = (birthdate) => {
  if (!birthdate) {
    age.value = null
    return
  }

  const today = new Date()
  const birth = new Date(birthdate)

  let yearsDiff = today.getFullYear() - birth.getFullYear()

  // Check if the birthday hasn't occurred yet this year
  const hasBirthdayPassed =
    today.getMonth() > birth.getMonth() ||
    (today.getMonth() === birth.getMonth() && today.getDate() >= birth.getDate())

  if (!hasBirthdayPassed) {
    yearsDiff--
  }

  age.value = yearsDiff
}
/*心电图数据相关*/

/*心电图数据相关*/
defineOptions({ name: 'ECGForm' })
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style lang="scss" scoped>
.mycontainer {
  display: flex;
  width: 100%;
  height: 90vh;
}

.myleft-box {
  flex: 4; /* 左侧盒子占据一半空间 */
  border-style: solid; /* 设置边框样式为实线 */
  border-width: 1px; /* 设置边框宽度为2像素 */
  border-color: #cccccc; /* 设置边框颜色为黑色 */
  height: 90vh;
}

.myright-box {
  flex: 1; /* 右侧盒子占据一半空间 */
  border-style: solid solid solid; /* 上、右、下边框为实线 */
  border-width: 1px; /* 设置边框宽度为1像素 */
  border-color: #cccccc; /* 设置边框颜色为灰色 */
  height: 90vh;
  border-left-style: none; /* 移除左边边框 */
}
.mybutwidth {
  margin-left: 10px;
  margin-bottom: 5px;
  width: 50px;
}

.mycenter-button {
  display: flex; /* 启用Flexbox布局 */
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 30px; /* 根据需要设置容器高度 */
  margin-top: 10px;
}
.mybtnsty {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/*心电图区域样式*/

/*心电图区域样式*/

/* 打印(报告单) */
.page-item-ecg {
  padding: 1mm 0.8mm 1mm 0.6mm;
  max-width: 1184px;
  margin: 3vh auto 0 auto;
  border: 2px solid #ccc;
  border-radius: 8px;
}

.page-pdf-ecg {
  border-width: 0;
  padding: 0 0.8mm 0 0.8mm;
  width: 294mm;
  height: 206mm;
  page-break-after: always;
}

@media print {
  .ignore-print {
    display: none;
  }

  .page-item-ecg {
    border-width: 0;
    height: 206mm;
    page-break-after: always;
    padding: 0;
    max-width: none;
    width: calc(100% - 3mm);
    margin: 0 0 0 1.2mm;
  }
}
</style>
