<template>
  <el-container>

    <el-header>
      <el-button type="primary" @click="clickSaveData">testSave</el-button>
      <el-button type="primary" @click="testPrint">testPrint</el-button>
      <el-button type="primary" @click="testDelete">testDelete</el-button>
    </el-header>

    <el-container>

      <el-aside>
        <emr-sidebar></emr-sidebar>
      </el-aside>

      <el-main>

        <div class="emr-iframe" name="emr">

          <iframe id="emrIframe"
                  :src="caseHistoryUrl"/>

        </div>

      </el-main>

    </el-container>

  </el-container>
</template>

<script>

import {huanZheXinXi} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import store from "@/store";
import {uuid} from "@/utils/getUuid";
import {getEmrInpatientData} from "@/api/dictionary/emr-data-maintenance-api";
import EmrSidebar from "@/components/zhu-yuan-yi-sheng/emr/EmrSidebar.vue";
import {ElMessage} from "element-plus";
import {getPatientsEmr} from "@/api/zhu-yuan-yi-sheng/emr-api";

export default {
  components: {EmrSidebar},
  setup() {

    const currentEmr = ref(null)

    const caseHistoryUrl = ref('')

    let categoryCode = $ref('7e25f0c0070511edbc820dada413ba28')
    let documentId = $ref('')

    const clickSaveData = () => {
      currentEmr.value.saveDocument(categoryCode);
    }

    const testPrint = () => {
      currentEmr.value.printDocument(true, null);
    }

    const testDelete = () => {
      currentEmr.value.deleteDocument(documentId);
    }

    const getCaseHistoryUrl = (documentId, categoryCode, categroyId, patientId) => {
      caseHistoryUrl.value = `/emr/runtime/?documentId=${documentId}&categoryCode=${categoryCode}&categroyId=${categroyId}&patientId=${patientId}#/`;
    }

    onMounted(() => {

      nextTick(async () => {

        getCaseHistoryUrl(documentId, categoryCode, '', huanZheXinXi.value.inpatientNo)

        let data = await getEmrInpatientData({
          patNo: huanZheXinXi.value.inpatientNo,
          times: huanZheXinXi.value.admissTimes
        })

        data['_id'] = uuid(31, 62)
        currentEmr.value = new EMRInteractive(data);
        const iframe = document.getElementById('emrIframe')
        iframe.parentElement.emr = currentEmr.value

        // 获取患者已经存在的
        getPatientsEmr(huanZheXinXi.value.inpatientNo).then((res) => {
          console.log(res)
        })

      })

    })

    return {
      clickSaveData,
      testPrint,
      testDelete,
      caseHistoryUrl,
    }
  }
}


function EMRInteractive(data) {
  this.setEditor = (editor, runtime) => {
    this.editor = editor;
    this.runtime = runtime;
  };

  this.getAppContext = () => {
    return {
      endpoints: {
        app: "/bdp/dataservice/api",
      },
      input: {
        user: store.state.user.info.code,
        name: store.state.user.info.name
      },
      login: {
        token: store.state.user.info.token
      },
      data
    };
  };

  this.saveDocument = (categoryCode) => {
    let data = this.editor.getDocument();
    data.properties.categoryCode = categoryCode
    data.properties.patientId = huanZheXinXi.value.inpatientNo;
    data.properties.creator = store.state.user.info.code;
    data.properties.createTime = new Date()
    data.properties.modifier = store.state.user.info.code;
    data.properties.modifyTime = new Date()
    this.runtime.saveDocument(
        data,
        (res) => {
          // let validator = this.editor.getValidator();
          console.log(this.editor)
          // let valid = validator.valid();
          // console.log(valid)
          ElMessage.success("保存成功。")
        },
        (err) => {
          ElMessage.success("emr保存失败，" + err)
        }
    );

  };

  this.deleteDocument = (documentId) => {
    this.runtime.delDocument(documentId,
        (res) => {
          ElMessage.success("删除成功")
        },
        (err) => {
          ElMessage.success("删除失败" + err)
        }
    );
  };

  this.printDocument = (showPreview, docs = null) => {

    this.editor && this.editor.execute("print", {

      value: {
        showPreview
      }

    })
  };
}

</script>

<style scoped>
.emr-iframe, iframe {
  width: 100%;
  height: 700px;
}
</style>
