<template>
  <div class="dicom-container">
    <div class="dicom-list">
      <div class="container">
        <div class="related-study-wrapper">
          <div class="left">
            <div class="visit-name-wrapper">

              <div v-if="(visitTaskList.length > 0)" style="display: flex;flex-direction: row;">
                <div
                  v-for="s in visitTaskList"
                  :key="s.VisitTaskId"
                  class="visit-item"
                  :class="{'visit-item-active': activeTaskVisitId==s.VisitTaskId}"

                  @click.prevent="handleClick(s)"
                >{{ s.TaskBlindName }}</div>

              </div>

            </div>

          </div>
          <div class="right">
            <div
              v-for="s in visitTaskList"
              v-show="activeTaskVisitId === s.VisitTaskId"
              :key="s.VisitTaskId"
              class="study-wrapper"
            >
              <StudyList
                v-if="selectArr.includes(s.VisitTaskId)"
                :ref="s.VisitTaskId"
                :visit-task-id="s.VisitTaskId"
                :trial-id="trialId"
                :subject-visit-id="s.VisitId"
                :task-blind-name="s.TaskBlindName"
                @loadImageStack="loadImageStack"
                @previewNoneDicoms="previewNoneDicoms"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="dicom-viewer">
      <div class="container">
        <DicomViewer
          v-if="activeTaskVisitId"
          ref="dicomViewer"
          :is-exists-clinical-data="isExistsClinicalData"
          :is-exists-no-dicom-file="isExistsNoDicomFile"
          :IseCRFShowInDicomReading="IseCRFShowInDicomReading"
          :isReadingShowSubjectInfo="isReadingShowSubjectInfo"
          @previewCD="previewCD"
        />
      </div>

    </div>
  </div>
</template>
<script>
import StudyList from './CustomizeStudyList'
import DicomViewer from './CustomizeDicomViewer'
import DicomEvent from '../components/DicomEvent'
import store from '@/store'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
export default {
  name: 'CustomizeReadPage',
  components: {
    DicomViewer,
    StudyList
  },
  props: {
    isReadingShowSubjectInfo: {
      type: Boolean,
      required: true
    },
    trialId: {
      type: String,
      required: true
    },
    subjectId: {
      type: String,
      required: true
    },
    subjectCode: {
      type: String,
      required: true
    },
    visitTaskId: {
      type: String,
      required: true
    },
    isExistsClinicalData: {
      type: Boolean,
      required: true
    },
    isExistsNoDicomFile: {
      type: Boolean,
      required: true
    },
    IseCRFShowInDicomReading: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      activeName: 'relation-study',
      tabs: ['relation-study'],
      taskList: [],
      loading: false,
      selectArr: [],
      activeTaskIndex: -1,
      activeTaskVisitId: '',
      measureData: [],
      activeTaskIsCurrentTask: false,
      activeTaskIsBaseline: false,
      activeTaskReadingTaskState: 2,
      readingTaskState: 2,
      open: null
    }
  },
  computed: {
    ...mapGetters(['visitTaskList'])
  },

  mounted() {
    this.getVisitInfo()
    DicomEvent.$on('getNextVisitInfo', () => {
      this.getNextVisitInfo()
    })
    DicomEvent.$on('toggleVisitList', async data => {
      var index = this.visitTaskList.findIndex(i => i.VisitTaskId === data.visitTaskId)
      this.activeTaskVisitId = data.visitTaskId
      this.activeTaskIndex = index
      this.activeTaskIsCurrentTask = this.visitTaskList[index].IsCurrentTask
      this.activeTaskIsBaseline = this.visitTaskList[index].IsBaseLineTask
      this.activeTaskReadingTaskState = this.visitTaskList[index].ReadingTaskState
      if (!this.visitTaskList[index].IsInit) {
        const loading = this.$loading({ fullscreen: true })
        if (!this.visitTaskList[index].measureDataInit) {
          await store.dispatch('reading/getMeasuredData', this.visitTaskList[index].VisitTaskId)
        }
        if (!this.visitTaskList[index].studyListInit) {
          await store.dispatch('reading/getStudyInfo', { trialId: this.trialId, subjectVisitId: this.visitTaskList[index].VisitId, visitTaskId: this.visitTaskList[index].VisitTaskId, taskBlindName: this.visitTaskList[index].TaskBlindName })
        }
        if (!this.visitTaskList[index].readingQuestionsInit) {
          await store.dispatch('reading/getReadingQuestionAndAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        }
        if (!this.visitTaskList[index].questionsInit) {
          await store.dispatch('reading/getDicomReadingQuestionAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        }

        await store.dispatch('reading/setStatus', { visitTaskId: this.visitTaskList[index].VisitTaskId })
        loading.close()
      }
      if (!this.selectArr.includes(this.visitTaskList[index].VisitTaskId)) {
        this.selectArr.push(this.visitTaskList[index].VisitTaskId)
      }
      this.$nextTick(() => {
        this.$refs[this.activeTaskVisitId][0].toggleStudy(data)
      })
    })

    DicomEvent.$on('isCanActiveNoneDicomTool', data => {
      this.open.postMessage({ type: 'isCanActiveNoneDicomTool', data: data }, window.location)
    })
    DicomEvent.$on('removeNoneDicomMeasureData', data => {
      this.open.postMessage({ type: 'removeNoneDicomMeasureData', data: data }, window.location)
    })
    DicomEvent.$on('addNoneDicomMeasureData', data => {
      this.open.postMessage({ type: 'addNoneDicomMeasureData', data: data }, window.location)
    })
  },
  beforeDestroy() {
    DicomEvent.$off('toggleVisitList')
    DicomEvent.$off('getNextVisitInfo')
    DicomEvent.$off('isCanActiveNoneDicomTool')
    DicomEvent.$off('removeNoneDicomMeasureData')
    DicomEvent.$off('addNoneDicomMeasureData')
  },
  methods: {
    async getVisitInfo() {
      const loading = this.$loading({ fullscreen: true })

      await store.dispatch('reading/getOrganInfo', this.visitTaskId)
      await store.dispatch('reading/getVisitTasks', this.visitTaskId)
      var index = this.visitTaskList.findIndex(i => i.IsCurrentTask)
      if (index > -1) {
        await store.dispatch('reading/getMeasuredData', this.visitTaskList[index].VisitTaskId)
        await store.dispatch('reading/getStudyInfo', { trialId: this.trialId, subjectVisitId: this.visitTaskList[index].VisitId, visitTaskId: this.visitTaskList[index].VisitTaskId, taskBlindName: this.visitTaskList[index].TaskBlindName })
        await store.dispatch('reading/getReadingQuestionAndAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        await store.dispatch('reading/getDicomReadingQuestionAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })

        await store.dispatch('reading/setStatus', { visitTaskId: this.visitTaskList[index].VisitTaskId })
        this.activeTaskVisitId = this.visitTaskList[index].VisitTaskId
        this.activeTaskIndex = index
        this.activeTaskIsCurrentTask = this.visitTaskList[index].IsCurrentTask
        this.activeTaskIsBaseline = this.visitTaskList[index].IsBaseLineTask
        this.activeTaskReadingTaskState = this.visitTaskList[index].ReadingTaskState
        this.selectArr.push(this.visitTaskList[index].VisitTaskId)
        this.$nextTick(() => {
          DicomEvent.$emit('getCustomTableQuestionAnswer', this.visitTaskList[index].VisitTaskId)
          this.$refs[this.activeTaskVisitId][0].getStudyInfo(true)
        })
      }
      loading.close()
    },
    async getNextVisitInfo() {
      const loading = this.$loading({ fullscreen: true })
      // await store.dispatch('reading/getMasterSeries', { trialId: this.trialId })

      await store.dispatch('reading/getNextVisitTask', { visitTaskId: this.visitTaskId })
      var index = this.visitTaskList.findIndex(i => i.IsCurrentTask)
      if (index > -1) {
        await store.dispatch('reading/getMeasuredData', this.visitTaskList[index].VisitTaskId)
        await store.dispatch('reading/getStudyInfo', { trialId: this.trialId, subjectVisitId: this.visitTaskList[index].VisitId, visitTaskId: this.visitTaskList[index].VisitTaskId, taskBlindName: this.visitTaskList[index].TaskBlindName })
        await store.dispatch('reading/getReadingQuestionAndAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        await store.dispatch('reading/getDicomReadingQuestionAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })

        await store.dispatch('reading/setStatus', { visitTaskId: this.visitTaskList[index].VisitTaskId })
        this.activeTaskVisitId = this.visitTaskList[index].VisitTaskId
        this.activeTaskIndex = index
        this.activeTaskIsCurrentTask = this.visitTaskList[index].IsCurrentTask
        this.activeTaskIsBaseline = this.visitTaskList[index].IsBaseLineTask
        this.activeTaskReadingTaskState = this.visitTaskList[index].ReadingTaskState
        this.selectArr.push(this.visitTaskList[index].VisitTaskId)
        this.$nextTick(() => {
          DicomEvent.$emit('getCustomTableQuestionAnswer', this.visitTaskList[index].VisitTaskId)
          // DicomEvent.$emit('getCustomTableQuestionAnswer')
          this.$refs[this.activeTaskVisitId][0].getStudyInfo()
        })
      }
      loading.close()
    },
    async handleClick(visitTaskInfo) {
      if (this.activeTaskVisitId === visitTaskInfo.VisitTaskId) return

      var index = this.visitTaskList.findIndex(i => i.VisitTaskId === visitTaskInfo.VisitTaskId)
      if (!this.visitTaskList[index].IsInit) {
        const loading = this.$loading({ fullscreen: true })
        await store.dispatch('reading/getMeasuredData', this.visitTaskList[index].VisitTaskId)
        await store.dispatch('reading/getStudyInfo', { trialId: this.trialId, subjectVisitId: this.visitTaskList[index].VisitId, visitTaskId: this.visitTaskList[index].VisitTaskId, taskBlindName: this.visitTaskList[index].TaskBlindName })
        await store.dispatch('reading/getReadingQuestionAndAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        await store.dispatch('reading/getDicomReadingQuestionAnswer', { trialId: this.trialId, visitTaskId: this.visitTaskList[index].VisitTaskId })
        await store.dispatch('reading/setStatus', { visitTaskId: this.visitTaskList[index].VisitTaskId })
        DicomEvent.$emit('getCustomTableQuestionAnswer', this.visitTaskList[index].VisitTaskId)
        loading.close()
      }

      this.activeTaskVisitId = this.visitTaskList[index].VisitTaskId
      this.activeTaskIndex = index
      this.activeTaskIsCurrentTask = this.visitTaskList[index].IsCurrentTask
      this.activeTaskIsBaseline = this.visitTaskList[index].IsBaseLineTask
      this.activeTaskReadingTaskState = this.visitTaskList[index].ReadingTaskState
      if (!this.selectArr.includes(this.visitTaskList[index].VisitTaskId)) {
        this.selectArr.push(this.visitTaskList[index].VisitTaskId)
      }
      this.$nextTick(() => {
        this.$refs[this.activeTaskVisitId][0].initStudyList()
      })
    },
    loadImageStack(seriesInfo) {
      this.$nextTick(() => {
        this.$refs.dicomViewer.loadImageStack(seriesInfo)
      })
    },
    previewCD() {
      var token = getToken()
      const routeData = this.$router.resolve({
        path: `/clinicalData?subjectId=${this.subjectId}&trialId=${this.trialId}&visitTaskId=${this.visitTaskId}&TokenKey=${token}`
      })
      window.open(routeData.href, '_blank')
    },
    previewNoneDicoms(obj) {
      var index = this.visitTaskList.findIndex(i => i.VisitTaskId === obj.visitTaskId)
      var taskBlindName = this.visitTaskList[index].TaskBlindName
      var token = getToken()
      const routeData = this.$router.resolve({
        path: `/nonedicoms?subjectId=${this.subjectId}&trialId=${this.trialId}&visitTaskId=${obj.visitTaskId}&taskBlindName=${taskBlindName}&readingTaskState=${this.visitTaskList[index].ReadingTaskState}&TokenKey=${token}`
      })
      this.open = window.open(routeData.href, '_blank')
    }
  }
}
</script>
<style lang="scss" scoped>
.dicom-container{

  height: 100%;
  // background-color: #fff;
  background-color: #000;
  ::-webkit-scrollbar {
    width: 7px;
    height: 7px;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #d0d0d0;
  }
  .report-header{
    display: flex;
  }
  .el-card{
    background-color: #000;
    color: #ffffff;
    border:none;
  }
  >>>.el-table, .el-table__expanded-cell {
    background-color: #000;
    color: #fff;
    border-color:#444444;
  }

  >>>.el-table th, .el-table tr {
    background-color: #000;
    color: #fff;
    border-color:#444444;
  }
  >>>.el-table__body tr > td{
    background-color:#000 !important;
    color: #fff;
    border-color:#444444;
  }
  >>>.el-table__body tr:hover > td{
    background-color:#858282 !important;
    color: #fff;
    border-color:#444444;
  }
  >>>.el-table--border th.gutter:last-of-type{
    border: none;
  }
  >>>.el-card__header{
    border: none;
    padding: 10px;
  }

  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  background-color: #000;
  box-sizing: border-box;
  ::-webkit-scrollbar {
    width: 5px;
    height: 5px;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #d0d0d0;
  }

  .dicom-viewer{
    flex: 1;
    width: 100%;
    box-sizing: border-box;

    .container{
      width: 100%;
      height: 100%;
      box-sizing: border-box;
    }
  }
  .dicom-list{
     width: 200px;
     padding: 5px 0px;
    box-sizing: border-box;

    .container{
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      border: 1px solid #727272;
      // border: 1px solid #ccc;

    }
    .el-tabs{
      box-sizing: border-box;
      padding: 0 5px;
      height: 100%;
      display: flex;
      flex-direction: column;
      // justify-content: flex-start;
      border: 1px solid #727272;
      .el-tabs__item{
        color: #fff;
      }
      >>>.el-tabs__header{
        height: 55px;
        margin:0px;
        box-sizing: border-box;
      }
      >>>.el-tabs__content{
        flex: 1;
        margin:0px;
        overflow-y: auto;
        box-sizing: border-box;
      }
      >>>.el-tabs__item{
        color: #fff;
      }

    }
    .dicom-desc{
      font-weight: bold;
      font-size: 13px;
      text-align: center;
      // background-color: rgb(88 84 83);
      color: #d0d0d0;
      padding: 2px;
    }
    .ps {
      overflow-anchor: none;
      touch-action: auto;
    }
    .series-active {
      background-color: #607d8b!important;
      border: 1px solid #607d8b!important;
    }
    >>>.el-progress__text{
      color: #ccc;
      font-size: 12px;
    }
    .series{
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      .series-wrapper {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        height: 85px;
        // padding: 1px 2px 1px 8px;
        // margin: 10px 0px;
        border-radius: 2px;
        border: 1px solid #404040;
        background-color: #3a3a3a;
        .el-progress__text{
          display: none;
        }
        .el-progress-bar{
          padding-right:0px;
        }

        .image-preview {
          height: 75px;
          width: 75px;
          border: 2px solid #252525;
          cursor: pointer;
        }
        .image-desc {
          vertical-align: top;
          p{
            width: 100px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 12px;
            color: #ddd;
            padding: 1px;
            margin: 0px;
          }
        }
      }
    }
    .related-study-wrapper{
      box-sizing: border-box;
      height: 100%;
      padding-bottom: 5px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;

      .left{
        position: relative;
        width: 25px;
        height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
        box-sizing: border-box;
        .visit-name-wrapper{
          position: absolute;
          top: 5px;

          transform-origin: right top;
          transform: rotate(-90deg);
          right: 19px;
          // top: 5px;
          // right: 20px;
          // transform-origin: right top;
          // transform: rotate(-90deg);
          // display: flex;
          // flex-direction: row;
          // align-content: flex-start;
        }
        .visit-item{
          margin-left: 10px;
          white-space: nowrap;
          padding: 0px  4px;
          border: 1px solid #999999;
          border-bottom:none ;
          text-align: center;
          background-color: #4e4e4e;
          color: #d5d5d5;
          cursor: pointer;
          // margin-left: 10px;
        }
        .visit-item-active{
          background-color: #607d8b;
          border: 1px solid #607d8b;
        }
      }
      .right{
        width:170px;
        flex:1;
        height: 100%;
        border-left: 1px solid #4a4a4a;
        color: #d5d5d5;
        .study-wrapper{
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>
