<template>
  <div v-loading="loading" class="study-wrapper">
    <div class="study-info">
      <div
        v-if="taskInfo && taskInfo.IsReadingShowSubjectInfo"
        :title="taskInfo.SubjectCode"
      >
        {{ taskInfo.SubjectCode }}
      </div>
      <div
        v-if="taskInfo && taskInfo.IsReadingShowSubjectInfo"
        :title="visitTaskInfo.TaskBlindName"
      >
        {{ visitTaskInfo.TaskBlindName }}
      </div>
    </div>
    <div class="ps">
      <el-collapse v-model="activeNames">
        <el-collapse-item v-for="(study, index) in studyList" :key="`${study.StudyId}`" :name="`${study.StudyId}`">
          <template slot="title">
            <div
              v-if="!study.IsCriticalSequence"
              class="dicom-desc"
            >
              <template v-if="taskInfo && taskInfo.IsShowStudyName">
                <div style="text-overflow: ellipsis;overflow: hidden;">
                  <span :title="study.StudyCode">{{ study.StudyCode }}</span>
                  <span v-if="study.StudyName" :title="study.StudyName" style="margin-left: 5px;">{{ study.StudyName }}</span>
                  <span v-else :title="study.Modalities" style="margin-left: 5px;">{{ `${study.Modalities} (${study.SeriesCount})` }}</span>
                </div>
                <div v-if="study.StudyName" style="text-overflow: ellipsis;overflow: hidden;">
                  <span :title="study.Modalities">{{ `${study.Modalities} (${study.SeriesCount})` }}</span>
                </div>
              </template>
              <template v-else-if="taskInfo && !taskInfo.IsShowStudyName">
                <div style="text-overflow: ellipsis;overflow: hidden;">
                  <span :title="study.StudyCode">{{ study.StudyCode }}</span>
                  <span :title="study.Modalities">{{ `${study.Modalities} (${study.SeriesCount})` }}</span>
                </div>
              </template>

            </div>
            <div v-else>
              <!-- 关键序列 -->
              {{ $t('trials:reading:title:keySeries') }}
            </div>
          </template>
          <div class="dicom-list-container">
            <div
              v-for="(series, i) in study.SeriesList"
              :key="series.Id"
              style="position:relative;margin-top:1px;"
              @click="activeSeries(series, i, index)"
            >
              <div
                :class="{'series-active': index === activeStudyIndex && i === activeSeriesIndex}"
                class="series-wrapper"
              >
                <div class="series-image">
                  <el-image
                    style="width: 100%;height: 100%;"
                    :src="`${OSSclientConfig.basePath}${series.ImageResizePath || series.NoneDicomFileFirstFile}`"
                    fit="fill"
                    crossorigin="anonymous"
                  />
                </div>
                <div class="series-text">
                  <div v-if="series.IsExistMutiFrames && series.InstanceCount > 1" 
                  style="position: absolute;right: 0;top: 0;">
                    <el-popover
                      placement="right"
                      trigger="hover"
                      popper-class="instance_frame_wrapper"
                    >
                      <div class="frame_list">
                        <div
                          v-for="(instance, idx) in series.InstanceInfoList"
                          :key="instance.Id"
                          class="frame_content"
                          :style="{'margin-bottom':idx<series.InstanceInfoList.length-1? '5px':'0px'}"
                          @click.stop="showMultiFrames(index,series, i, instance)"
                        >
                          <div>
                            <div>{{ instance.InstanceNumber }}</div>
                            <div>{{ `${instance.NumberOfFrames > 0 ? instance.KeyFramesList.length > 0 ? instance.KeyFramesList.length : instance.NumberOfFrames : 1} frame` }}</div>
                          </div>

                        </div>
                      </div>
                      <i slot="reference" class="el-icon-connection" style="font-size: 15px;cursor: pointer;color: #ffeb3b;" />
                    </el-popover>
                  </div>
                  <div v-if="!study.IsCriticalSequence" class="text-desc" :title="series.SeriesNumber">
                    #{{ series.SeriesNumber }}
                    
                  </div>
                  <div v-if="series.Description" class="text-desc" :title="series.Description">
                    {{ series.Description }}
                  </div>
                  <div v-if="series.SliceThickness && !study.IsCriticalSequence" class="text-desc">
                    T: {{ parseFloat(series.SliceThickness).toFixed(digitPlaces) }}
                  </div>
                  <div class="text-desc">
                    <span v-show="series.LoadedImageCount < series.InstanceCount">
                      {{ series.Modality }}: {{ series.LoadedImageCount }}/{{ series.InstanceCount }} image
                    </span>
                    <span v-show="series.LoadedImageCount >= series.InstanceCount">{{ series.Modality }}: {{ series.InstanceCount }} image</span>
                  </div>
                  <div style="line-height: 12px;">
                    <i v-show="series.IsBeMark || markedSeriesIds.includes(series.Id)" class="el-icon-star-on" style="font-size: 12px;color: #ff5722;" />
                  </div>
                </div>
              </div>
              <div v-if="series.LoadedImageCount > 0 && series.LoadedImageCount < series.InstanceCount" style="width: 100%;">
                <el-progress
                  :percentage="parseInt((series.LoadedImageProgress / series.InstanceCount).toFixed(2))"
                />
              </div>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>
<script>
export default {
  name: 'StudyList',
  props: {
    visitTaskInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    markedSeriesIds: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      activeNames: [],
      activeStudyIndex: -1,
      activeSeriesIndex: -1,
      taskInfo: null,
      studyList: [],
      annotations: [],
      digitPlaces: 2
    }
  },
  mounted() {
    this.taskInfo = JSON.parse(localStorage.getItem('taskInfo'))
    const digitPlaces = Number(localStorage.getItem('digitPlaces'))
    this.digitPlaces = digitPlaces === -1 ? this.digitPlaces : digitPlaces
    this.studyList = this.visitTaskInfo.StudyList
    this.annotations = this.visitTaskInfo.Annotations
    if (this.studyList.length === 0) return
    this.$nextTick(() => {
      this.activeStudy(this.studyList[0].StudyId)
    })
  },
  methods: {
    activeSeries(series, seriesIndex, studyIndex) {
      this.activeStudyIndex = studyIndex
      this.activeSeriesIndex = seriesIndex
      this.$emit('activeSeries', series)
    },
    activeStudy(id) {
      if (this.activeNames.indexOf(id) > -1) return
      this.activeNames.push(id)
    },
    setSeriesActive(studyIndex, seriesIndex) {
      this.activeStudyIndex = studyIndex
      this.activeSeriesIndex = seriesIndex
      const studyId = this.studyList[studyIndex].StudyId
      if (!studyId) return
      this.activeStudy(studyId)
    },
    showMultiFrames(studyIndex, series, seriesIndex, instance) {
      let obj = Object.assign({}, series)
      this.activeStudyIndex = studyIndex
      this.activeSeriesIndex = seriesIndex
      let taskId = this.visitTaskInfo.VisitTaskId
      const nFrames = instance.NumberOfFrames || 0
      let imageIds = []
      const stack = []
      if (nFrames === 0) {
        // 单帧
        stack.push(`wadouri:${this.OSSclientConfig.basePath}${instance.Path}?instanceId=${instance.Id}`)
      } else {
        // 多帧
        for (let i = 0; i < nFrames; i++) {
          const newImageId = `wadouri:${this.OSSclientConfig.basePath}${instance.Path}?instanceId=${instance.Id}&frame=${i + 1}`
          stack.push(newImageId)
        }
      }
      imageIds.push(`wadouri:${this.OSSclientConfig.basePath}${instance.Path}?instanceId=${instance.Id}`)
      obj.Stack = stack
      obj.ImageIds = imageIds
      obj.SliceIndex = 0
      this.$emit('showMultiFrame', obj)
      
    },
    getPreviousOrNextSeries(type, series) {
      const seriseList = this.studyList.map(s => s.SeriesList).flat()
      const i = seriseList.findIndex(i => i.Id === series.Id && i.StudyId === series.StudyId)
      if (i === -1) return
      let newIndex = null
      if (type === -1) {
        newIndex = i === 0 ? i : i - 1
      } else {
        newIndex = i >= seriseList.length - 1 ? i : i + 1
      }
      const studyIndex = seriseList[newIndex].StudyIndex
      const seriesIndex = seriseList[newIndex].SeriesIndex
      this.setSeriesActive(studyIndex, seriesIndex)
      this.activeSeries(seriseList[newIndex], seriesIndex, studyIndex)
    }
  }
}
</script>
<style lang="scss" scoped>
.study-wrapper{
  width:100%;
  height: 100%;
  overflow-y: hidden;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  .study-info {
    font-size: 16px;
    font-weight: bold;
    color: #ddd;
    padding: 5px 0px;
    margin: 0;
    text-align: center;
    background-color: #4c4c4c;
    height: 50px;
  }
  .dicom-desc{
    font-weight: bold;
    font-size: 13px;
    text-align: left;
    color: #d0d0d0;
    padding: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .ps {
    flex: 1;
    overflow-anchor: none;
    touch-action: auto;
    overflow-y: auto;
  }
  .series-active {
    background-color: #607d8b!important;
    border: 1px solid #607d8b!important;
  }
  ::v-deep.el-progress__text{
    color: #ccc;
    font-size: 12px;
  }
  .dicom-list-container{
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    .series-wrapper {
      width: 100%;
      padding: 5px;
      display: flex;
      flex-direction: row;
      align-items: center;
      cursor: pointer;
      background-color: #3a3a3a;
      .el-progress__text{
        display: none;
      }
      .el-progress-bar{
        padding-right:0px;
      }
      .series-image {
        width: 50px;
        height: 50px;
      }
      .series-text {
        flex: 1;
        padding-left: 5px;
        color: #ddd;
        position: relative;
        .text-desc {
          width: 100px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 12px;
          line-height: 16px;
        }
      }

    }
  }
  ::v-deep.el-collapse{
    border: none;
    .el-collapse-item{
      background-color: #000!important;
      color: #ddd;

    }
    .el-collapse-item__content{
      padding-bottom:0px;
      background-color: #000!important;
    }
    .el-collapse-item__header{
      background-color: #000!important;
      color: #ddd;
      border-bottom-color:#5a5a5a;
      padding-left: 5px;
      // height: 50px;
      line-height: 20px;
    }
  }
  ::v-deep .el-progress-bar__inner{
    transition: width 0s ease;
  }
}
</style>
