<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" @change="handleChange">
        <el-collapse-item v-for="(study, index) in studyList" :key="`${study.Id}`" :name="`${study.Id}`">
          <template slot="title">
            <div
              v-if="!study.IsCriticalSequence"
              class="dicom-desc"
            >
              <!-- <div v-if="taskInfo && taskInfo.IsShowStudyName">{{ study.StudyName }}</div> -->
              <div style="text-overflow: ellipsis;overflow: hidden;">
                <span :title="study.CodeView">{{ study.CodeView }}</span>
                <span v-if="taskInfo && taskInfo.IsShowStudyName" :title="study.StudyName" style="margin-left: 5px;">{{ study.StudyName }}</span>
              </div>
              <div style="text-overflow: ellipsis;overflow: hidden;">
                <span :title="study.BodyPart">{{ study.BodyPart }}</span>
                <span style="margin-left: 5px;" :title="study.Modality">{{ study.Modality }}</span>
              </div>
            </div>
            <div v-else>
              <!-- 关键文件 -->
              {{ $t('trials:noneDicom:title:keyFiles') }}
            </div>
          </template>
          <div class="file-list-container">
            <div
              v-for="(k, i) in study.NoneDicomStudyFileList"
              :key="i"
              style="position:relative;margin-top:1px;"
              @click="selectFile(study, index, i)"
            >
              <div
                :class="{'file-active': index === activeStudyIndex && i === activeFileIndex}"
                class="file-wrapper"
              >
                <div class="file-image">
                  <el-image
                    v-if="k.FileType === 'image/jpeg' || k.FileType === 'image/jpg' || k.FileType === 'image/bmp' || k.FileType === 'image/png'"
                    style="width: 100%;height: 100%;"
                    :src="`${OSSclientConfig.basePath}${k.Path}?x-oss-process=image/resize,w_50,h_50/format,png`"
                    fit="contain"
                    crossorigin="anonymous"
                  />
                  <el-image
                    v-else-if="k.FileType === 'application/pdf'"
                    style="width: 100%;height: 100%;"
                    :src="pdf"
                    fit="contain"
                    crossorigin="anonymous"
                  />
                </div>
                <div class="file-text" :title="k.FileName">
                  {{ k.FileName }}
                </div>

              </div>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>
<script>
import pdf from '@/assets/pdf.png'
export default {
  name: 'StudyList',
  props: {
    visitTaskInfo: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      activeNames: [],
      activeStudyIndex: -1,
      activeFileIndex: -1,
      taskInfo: null,
      studyList: [],
      pdf
    }
  },
  mounted() {
    this.taskInfo = JSON.parse(localStorage.getItem('taskInfo'))
    this.studyList = this.visitTaskInfo.StudyList
    if (this.studyList.length === 0) return
    this.$nextTick(() => {
      this.activeStudy(this.studyList[0].Id)
    })
  },
  methods: {
    // 设置初始化激活文件
    setInitActiveFile() {
      if (this.studyList.length === 0) return
      this.$nextTick(() => {
        this.activeStudy(this.studyList[0].Id)
        this.selectFile(this.studyList[0], 0, 0)
      })
    },
    // 切换文件
    selectFile(study, studyIndex, fileIndex) {
      this.activeStudyIndex = studyIndex
      this.activeFileIndex = fileIndex
      const fileList = study.NoneDicomStudyFileList
      this.$emit('selectFile', { fileInfo: fileList[fileIndex], fileList, visitTaskInfo: this.visitTaskInfo, studyId: study.Id })
    },
    activeImage(obj) {
      if (this.studyList.length === 0) return
      const i = this.studyList.findIndex(i => i.Id === obj.studyId)
      if (i === -1) return
      this.activeStudy(obj.studyId)
      this.activeStudyIndex = i
      const fileList = this.studyList[i].NoneDicomStudyFileList
      const fIndex = fileList.findIndex(f => f.Path === obj.path)
      if (fIndex === -1) return
      this.activeFileIndex = fIndex
    },
    activeStudy(id) {
      if (this.activeNames.indexOf(id) > -1) return
      this.activeNames.push(id)
    },
    handleChange(v) {
      console.log(v)
    }
  }
}
</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;
  }
  .file-active {
    background-color: #607d8b!important;
    border: 1px solid #607d8b!important;
  }
  ::v-deep.el-progress__text{
    color: #ccc;
    font-size: 12px;
  }
  .file-list-container{
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    .file-wrapper {
      width: 100%;
      padding: 5px;
      display: flex;
      flex-direction: row;
      align-items: center;
      cursor: pointer;
      background-color: #3a3a3a;
      // color: #ddd;
      // white-space: nowrap;
      // overflow: hidden;
      // text-overflow: ellipsis;
      .el-progress__text{
        display: none;
      }
      .el-progress-bar{
        padding-right:0px;
      }
      .file-image {
        width: 50px;
        height: 50px;
      }
      .file-text {
        flex: 1;
        padding-left: 5px;
        color: #ddd;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

    }
  }
  ::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: 60px;
      line-height: 20px;
    }
  }
  ::v-deep .el-progress-bar__inner{
    transition: width 0s ease;
  }
}
</style>
