<template>
  <page-layer>
    <template #header>
      <el-input v-model="patNo" style="width: 120px" @keydown.enter="queryData"/>
      <el-button type="primary" @click="queryData">查询</el-button>
      <el-button type="success" @click="recovery" :disabled="!rowData.id">恢复</el-button>
    </template>

    <template #aside>
      <xc-table :local-data="deleteData"
                :height="200"
                @rowClick="rowClick"
                layout="total,prev, pager, next">
        <el-table-column label="名称" prop="emrName"/>
        <el-table-column label="创建时间" prop="createDate">
          <template #default="{row}">
            <span v-html="huanHangXianShi(row.createDate)"/>
          </template>
        </el-table-column>
      </xc-table>
    </template>

    <template #main>
      <div v-loading="!isReady" style="height: 100%">
        <iframe
            height="80%"
            width="100%"
            ref="emrRef"
            src="/emr/runtime/#/editor"
        />
      </div>
    </template>

  </page-layer>
</template>

<script setup name='ResumeMedicalRecords'>
import {
  getDeleteMedicalRecord,
  getInvalidByDocumentId,
  resumeMedicalRecords
} from "@/api/zhu-yuan-yi-sheng/emr-patient";
import PageLayer from "@/layout/PageLayer.vue";
import {huanZheXinXi} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import XcTable from "@/components/xiao-chan/xc-table/XcTable.vue";
import {huanHangXianShi} from "@/utils/date";
import * as net from "net";
import {EMRInteractive} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import {BizException, ExceptionEnum} from "@/utils/BizException";

const deleteData = ref([])
const patNo = ref('')
const rowData = ref({})
const emrRef = ref()
const currentEmr = ref()
const isReady = ref(false)

const queryData = () => {
  if (!patNo.value) return
  getDeleteMedicalRecord(patNo.value).then((res) => {
    deleteData.value = res
    rowData.value = {}
    if (editor != null) {
      editor.setDocument({}, true, true)
    }
  })
}

const recovery = () => {
  resumeMedicalRecords(rowData.value.emrDocumentId).then((res) => {
    queryData()
  })
}

const rowClick = (val) => {
  if (!isReady.value) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请等待编辑器加载完成')
  }
  rowData.value = val;
  getInvalidByDocumentId(rowData.value.emrDocumentId).then((res) => {
    editor.setDocument(res, true, true)
    editor.setEditorMode('readonly')
  })

}

watch(() => huanZheXinXi.value.inpatientNo, () => {
  patNo.value = huanZheXinXi.value.inpatientNo
  queryData()
})

let editor = null

const editorEvents = {
  'ready': (event) => {
    isReady.value = true
    editor = currentEmr.value.getEditor()
  }
}

onMounted(async () => {
  await nextTick();
  currentEmr.value = new EMRInteractive({}, editorEvents);
  emrRef.value.parentElement.emr = currentEmr.value
})

</script>

<style scoped lang="scss">

</style>
