<template>
  <div ref="container" v-loading="loading" class="none-dicom-reading-container">
    <!-- 访视阅片 -->
    <VisitReview
      v-if="isShow && readingCategory && readingCategory=== 1"
      :trial-id="trialId"
      :subject-id="subjectId"
      :subject-code="subjectCode"
      :visit-task-id="visitTaskId"
      :reading-category="readingCategory"
      :is-exists-clinical-data="isExistsClinicalData"
    />
    <!-- 全局阅片 -->
    <GlobalReview
      v-else-if="isShow && readingCategory && readingCategory === 2"
      :trial-id="trialId"
      :subject-id="subjectId"
      :visit-task-id="visitTaskId"
      :reading-category="readingCategory"
      :subject-code="subjectCode"
      :task-blind-name="taskBlindName"
      :is-reading-show-subject-info="isReadingShowSubjectInfo"
      :is-reading-show-previous-results="isReadingShowPreviousResults"
      :is-exists-clinical-data="isExistsClinicalData"
    />
    <!-- 裁判阅片 -->
    <AdReview
      v-else-if="isShow && readingCategory && readingCategory === 4"
      :trial-id="trialId"
      :subject-id="subjectId"
      :visit-task-id="visitTaskId"
      :reading-category="readingCategory"
      :subject-code="subjectCode"
      :task-blind-name="taskBlindName"
      :is-reading-show-subject-info="isReadingShowSubjectInfo"
      :is-reading-show-previous-results="isReadingShowPreviousResults"
      :is-exists-clinical-data="isExistsClinicalData"
    />

    <!-- 肿瘤学阅片 -->
    <OncologyReview
      v-else-if="isShow && readingCategory && readingCategory === 5"
      :trial-id="trialId"
      :subject-id="subjectId"
      :visit-task-id="visitTaskId"
      :reading-category="readingCategory"
      :subject-code="subjectCode"
      :task-blind-name="taskBlindName"
      :is-reading-show-subject-info="isReadingShowSubjectInfo"
      :is-reading-show-previous-results="isReadingShowPreviousResults"
      :is-exists-clinical-data="isExistsClinicalData"
    />

    <el-dialog
      width="75%"
      :visible.sync="dialogVisible"
      style="margin:0px"
      custom-class="dialog-container"
      :show-close="false"
    >
      <span slot="title" class="dialog-footer">
        当前阅片任务存在临床数据，请查看。若已查看，请点击“确认”
      </span>
      <div :style="{'height':dialogH,'margin':0}">
        <ClinicalData
          :trial-id="trialId"
          :subject-id="subjectId"
          :visit-task-id="visitTaskId"
        />
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleConfirmCD">确认</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
import { getNextTask, readClinicalData } from '@/api/trials'
import store from '@/store'
import { changeURLStatic } from '@/utils/history.js'
import DicomEvent from '@/views/trials/trials-panel/reading/dicoms/components/DicomEvent'
import VisitReview from './components/VisitReview'
import GlobalReview from '@/views/trials/trials-panel/reading/global-review'
import AdReview from '@/views/trials/trials-panel/reading/ad-review'
import ClinicalData from '@/views/trials/trials-panel/reading/clinical-data'
import { getToken } from '@/utils/auth'
import OncologyReview from '@/views/trials/trials-panel/reading/oncology-review'
export default {
  name: 'NoneDicomReading',
  components: {
    VisitReview,
    AdReview,
    GlobalReview,
    OncologyReview,
    ClinicalData
  },
  data() {
    return {
      loading: false,
      readingCategory: null,
      subjectId: '',
      visitTaskId: '',
      trialId: '',
      subjectCode: '',
      taskBlindName: '',
      isReadingShowSubjectInfo: false,
      isReadingShowPreviousResults: false,
      isReadingTaskViewInOrder: false,
      isExistsClinicalData: false,
      isNeedReadClinicalData: false,
      isReadClinicalData: false,
      criterionType: null,
      readingTool: null,
      isNewSubject: null,
      dialogVisible: false,
      dialogH: 0,
      isShow: false
    }
  },
  mounted() {
    DicomEvent.$on('getNextTask', () => {
      this.getTaskInfo()
    })
    this.trialId = this.$router.currentRoute.query.trialId
    this.subjectCode = this.$router.currentRoute.query.subjectCode
    this.subjectId = this.$router.currentRoute.query.subjectId
    this.visitTaskId = this.$router.currentRoute.query.visitTaskId
    this.isReadingShowSubjectInfo = this.$router.currentRoute.query.isReadingShowSubjectInfo
    this.isReadingShowPreviousResults = this.$router.currentRoute.query.isReadingShowPreviousResults
    this.isReadingTaskViewInOrder = JSON.parse(this.$router.currentRoute.query.isReadingTaskViewInOrder)
    this.criterionType = this.$router.currentRoute.query.criterionType
    this.readingTool = this.$router.currentRoute.query.readingTool
    this.isNewSubject = this.$router.currentRoute.query.isNewSubject
    if (this.isNewSubject && this.isReadingTaskViewInOrder) {
      this.$message.success(`已开始受试者${this.subjectCode}阅片任务`)
      changeURLStatic('isNewSubject', '')
    }
    if (this.$router.currentRoute.query.TokenKey) {
      store.dispatch('user/setToken', this.$router.currentRoute.query.TokenKey)
      changeURLStatic('TokenKey', '')
    }
    this.getTaskInfo()
    this.dialogH = this.$refs['container'].clientHeight - 250 + 'px'
    window.addEventListener('resize', () => { this.dialogH = this.$refs['container'].clientHeight - 250 + 'px' })
  },
  beforeDestroy() {
    DicomEvent.$off('getNextTask')
  },
  methods: {
    getTaskInfo() {
      this.loading = true
      var param = {
        subjectId: this.subjectId,
        trialId: this.trialId,
        subjectCode: this.subjectCode,
        visitTaskId: this.$router.currentRoute.query.visitTaskId,
        trialReadingCriterionId: this.$router.currentRoute.query.TrialReadingCriterionId
      }
      this.isShow = false
      getNextTask(param).then(res => {
        this.readingCategory = res.Result.ReadingCategory
        if (this.subjectId !== res.Result.SubjectId && this.isReadingTaskViewInOrder) {
          store.dispatch('reading/resetVisitTasks')
          var token = getToken()
          window.location.href = `/noneDicomReading?trialId=${this.trialId}&subjectCode=${res.Result.SubjectCode}&subjectId=${res.Result.SubjectId}&isReadingShowPreviousResults=${this.isReadingShowPreviousResults}&isReadingShowSubjectInfo=${this.isReadingShowSubjectInfo}&criterionType=${this.criterionType}&readingTool=${this.readingTool}&isNewSubject=1&isReadingTaskViewInOrder=${res.Result.IsReadingTaskViewInOrder}&TokenKey=${token}`
        }
        this.subjectId = res.Result.SubjectId
        this.visitTaskId = res.Result.VisitTaskId
        this.subjectCode = res.Result.SubjectCode
        this.taskBlindName = res.Result.TaskBlindName
        this.isExistsClinicalData = res.Result.IsExistsClinicalData
        this.isReadClinicalData = res.Result.IsReadClinicalData
        this.isNeedReadClinicalData = res.Result.IsNeedReadClinicalData

        this.isReadingShowSubjectInfo = res.Result.IsReadingShowSubjectInfo
        this.isReadingShowPreviousResults = res.Result.IsReadingShowPreviousResults
        this.digitPlaces = res.Result.DigitPlaces
        localStorage.setItem('digitPlaces', 2)
        this.$nextTick(() => {
          if (this.isExistsClinicalData && this.isNeedReadClinicalData && !this.isReadClinicalData) {
            this.dialogVisible = true
          }
        })
        this.isShow = true
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleConfirmCD() {
      this.loading = true
      var visitTaskId = this.visitTaskId
      readClinicalData({ visitTaskId }).then(res => {
        this.loading = false
        this.dialogVisible = false
        this.isReadClinicalData = true
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style lang="scss" scoped>
.none-dicom-reading-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .el-dialog{
    margin-top: 0px !important;
    /deep/ .el-dialog__wrapper{
      margin-top: 0px !important;
    }
    /deep/ .el-dialog__body{
      padding: 10px;
    }
  }

/deep/ .dialog-container{
    margin-top: 50px !important;

  }
  /deep/ .el-dialog__body{
      padding: 10px;
    }
}

</style>
